Ajout de gotenv work in progres
This commit is contained in:
parent
9c00d80322
commit
22cffe21b4
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.env
|
||||||
@ -2,19 +2,22 @@ package function
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadExcel(path string,host string) {
|
func ReadExcel(path string)[]map[string]string {
|
||||||
|
|
||||||
// var sql []string
|
// var sql []string
|
||||||
// var pathSite []string
|
// var pathSite []string
|
||||||
// var pathBackupSite[]string
|
// var pathBackupSite[]string
|
||||||
// var pathBackupSiteMysql[]string
|
// var pathBackupSiteMysql[]string
|
||||||
|
var headers []string
|
||||||
f, err := excelize.OpenFile(path)
|
f, err := excelize.OpenFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
defer func() {
|
defer func() {
|
||||||
// Close the spreadsheet.
|
// Close the spreadsheet.
|
||||||
@ -26,22 +29,47 @@ func ReadExcel(path string,host string) {
|
|||||||
cell, err := f.GetCellValue("julien", "A2")
|
cell, err := f.GetCellValue("julien", "A2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
fmt.Println("-------------------------------------")
|
fmt.Println("-------------------------------------")
|
||||||
fmt.Println(cell)
|
fmt.Println(cell)
|
||||||
fmt.Println("-------------------------------------")
|
fmt.Println("-------------------------------------")
|
||||||
// Get all the rows in the Sheet1.
|
// Get all the rows in the Sheet1.
|
||||||
rows, err := f.GetRows("julien")
|
// Nom de la feuille
|
||||||
|
sheetName := "julien"
|
||||||
|
|
||||||
|
// Obtenir toutes les lignes
|
||||||
|
rows, err := f.GetRows(sheetName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Fatalf("Erreur lors de la lecture des lignes: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
for _, row := range rows {
|
|
||||||
//fmt.Print(row)
|
// Vérifier qu'il y a des lignes
|
||||||
for _, colCell := range row {
|
if len(rows) == 0 {
|
||||||
fmt.Print(colCell)
|
log.Fatal("La feuille est vide ou introuvable")
|
||||||
}
|
}
|
||||||
fmt.Println()
|
|
||||||
|
// La première ligne est utilisée pour les en-têtes
|
||||||
|
headers = rows[0]
|
||||||
|
|
||||||
|
// Tableau pour stocker les données
|
||||||
|
var data []map[string]string
|
||||||
|
|
||||||
|
// Parcourir les lignes en ignorant la première
|
||||||
|
for _, row := range rows[1:] { // rows[1:] commence à la deuxième ligne
|
||||||
|
rowMap := make(map[string]string)
|
||||||
|
for i, cell := range row {
|
||||||
|
if i < len(headers) {
|
||||||
|
rowMap[headers[i]] = cell
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
data = append(data, rowMap)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Afficher les résultats
|
||||||
|
fmt.Println("Données associées :")
|
||||||
|
for _, row := range data {
|
||||||
|
fmt.Println(row)
|
||||||
|
}
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
1
go.mod
1
go.mod
@ -5,6 +5,7 @@ go 1.23.2
|
|||||||
require github.com/TwiN/go-choice v1.2.0
|
require github.com/TwiN/go-choice v1.2.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/joho/godotenv v1.5.1 // indirect
|
||||||
github.com/kr/fs v0.1.0 // indirect
|
github.com/kr/fs v0.1.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@ -15,6 +15,8 @@ github.com/gdamore/tcell/v2 v2.5.0 h1:/LA5f/wqTP5mWT79czngibKVVx5wOgdFTIXPQ68fMO
|
|||||||
github.com/gdamore/tcell/v2 v2.5.0/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
|
github.com/gdamore/tcell/v2 v2.5.0/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
|
||||||
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
|
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU=
|
||||||
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
|
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg=
|
||||||
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
|
||||||
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
|
|||||||
67
main.go
67
main.go
@ -4,27 +4,36 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"canguidev.fr/pvnet/function"
|
"canguidev.fr/pvnet/function"
|
||||||
simplyssh "canguidev.fr/pvnet/simply-ssh"
|
simplyssh "canguidev.fr/pvnet/simply-ssh"
|
||||||
"github.com/TwiN/go-choice"
|
"github.com/TwiN/go-choice"
|
||||||
"github.com/fatih/color"
|
"github.com/joho/godotenv"
|
||||||
"github.com/pkg/sftp"
|
"github.com/pkg/sftp"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main(){
|
func main(){
|
||||||
//var path string="/Users/cangui/.ssh/julien_pvnet"
|
//var path string="/Users/cangui/.ssh/julien_pvnet"
|
||||||
function.ReadExcel("Z:/_backup/src_golang_exe.xlsx","vps1")
|
///Users/cangui/Documents/Dev peso/Golang dev/test.xlsx
|
||||||
color.Blue("Enter path ssh key: ")
|
err := godotenv.Load()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error loading .env file")
|
||||||
|
}
|
||||||
|
|
||||||
|
var pathSsh string = os.Getenv("PATH_SSH")
|
||||||
|
var pathConfigSsh = os.Getenv("PATH_SSH_CONF")
|
||||||
|
var pathExcel string= os.Getenv("PATH_FOLDER")
|
||||||
|
var mysqlUser string
|
||||||
|
var mysqlPswd string
|
||||||
|
var pathSaveSql string
|
||||||
|
|
||||||
var path string
|
|
||||||
// fonction pour recupére la donnée saisie
|
|
||||||
fmt.Scanln(&path)
|
|
||||||
// Chargement de la liste des hosts du fichier config ssh
|
// Chargement de la liste des hosts du fichier config ssh
|
||||||
d:=simplyssh.GetHost()
|
d:=simplyssh.GetHost(pathConfigSsh)
|
||||||
var s []string
|
var s []string
|
||||||
for _, host := range d {
|
for _, host := range d {
|
||||||
|
fmt.Println(host)
|
||||||
s=append(s,string(host.Id)+" "+host.Host)
|
s=append(s,string(host.Id)+" "+host.Host)
|
||||||
}
|
}
|
||||||
// selectionne du host
|
// selectionne du host
|
||||||
@ -32,15 +41,16 @@ func main(){
|
|||||||
"What do you want to do?\nPick:",
|
"What do you want to do?\nPick:",
|
||||||
s,
|
s,
|
||||||
)
|
)
|
||||||
|
selectHost:=choice
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("You didn't select anything!")
|
fmt.Println("You didn't select anything!",err)
|
||||||
} else {
|
} else {
|
||||||
for _, host := range d {
|
for _, host := range d {
|
||||||
// for sur les hosts et break sur le host selectionner
|
// for sur les hosts et break sur le host selectionner
|
||||||
if(host.Id == uint(index)){
|
if(host.Id == uint(index)){
|
||||||
fmt.Println("debut de connection")
|
fmt.Println("debut de connection")
|
||||||
// connection en ssh avec la clée et les informations du fichier config
|
// connection en ssh avec la clée et les informations du fichier config
|
||||||
client:=simplyssh.Connect(path,host.HostName,host.Port,host.User)
|
client:=simplyssh.Connect(pathSsh,host.HostName,host.Port,host.User)
|
||||||
command := "mkdir backup"
|
command := "mkdir backup"
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
|
|
||||||
@ -83,11 +93,46 @@ func main(){
|
|||||||
fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
|
fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index)
|
||||||
|
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
|
|
||||||
|
//var pathSaveSite string
|
||||||
|
orderedKeys := []string{"host","mysql_login", "mysql_pasw", "path_save_sql", "path_save_site"}
|
||||||
|
|
||||||
|
excel:=function.ReadExcel(pathExcel)
|
||||||
|
key:="host"
|
||||||
|
// Parcourir les résultats d'Excel
|
||||||
|
for _, value := range excel {
|
||||||
|
// Lire les clés dans l'ordre spécifié
|
||||||
|
for _, k := range orderedKeys {
|
||||||
|
if v, exists := value[k]; exists && k == key {
|
||||||
|
fmt.Println("Key:", k, "Value:", v)
|
||||||
|
if(k==selectHost){
|
||||||
|
switch k {
|
||||||
|
case "path_save_sql":
|
||||||
|
pathSaveSql = v
|
||||||
|
case "path_save_site":
|
||||||
|
// pathSaveSite = v
|
||||||
|
case "mysql_login":
|
||||||
|
mysqlUser = v
|
||||||
|
case "mysql_pasw":
|
||||||
|
mysqlPswd = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Switch pour affecter les variables
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("MySQL User:", mysqlUser)
|
||||||
|
fmt.Println("MySQL Password:", mysqlPswd)
|
||||||
|
fmt.Println("Path Save SQL:", pathSaveSql)
|
||||||
|
log.Fatal("stop julien")
|
||||||
|
|
||||||
currentTime := time.Now()
|
currentTime := time.Now()
|
||||||
formattedTime := currentTime.Format("20060102_150405")
|
formattedTime := currentTime.Format("20060102_150405")
|
||||||
|
|
||||||
filename := "backup/test_" + formattedTime + ".sql"
|
filename := "backup/test_" + formattedTime + ".sql"
|
||||||
commandBackup := "mysqldump -u cangui@localhost -p'GHT30k7!' test > " + filename
|
commandBackup := "mysqldump -u "+mysqlUser+" -p'"+mysqlPswd+"' test > " + filename
|
||||||
|
|
||||||
// Maintenant on utilise la sessionBackup pour exécuter la commande du backup
|
// Maintenant on utilise la sessionBackup pour exécuter la commande du backup
|
||||||
if err := sessionBackup.Run(commandBackup); err != nil {
|
if err := sessionBackup.Run(commandBackup); err != nil {
|
||||||
@ -108,7 +153,7 @@ func main(){
|
|||||||
formattedTime2:= currentTime2.Format("20060102_150405")
|
formattedTime2:= currentTime2.Format("20060102_150405")
|
||||||
|
|
||||||
filename2 := "test_" + formattedTime2 + ".sql"
|
filename2 := "test_" + formattedTime2 + ".sql"
|
||||||
err = simplyssh.DownloadFile(sftpClient, filename, "/Users/cangui/Documents/Dev peso/Golang dev/"+filename2)
|
err = simplyssh.DownloadFile(sftpClient, filename, pathSaveSql+filename2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Erreur lors du téléchargement: ", err)
|
log.Fatal("Erreur lors du téléchargement: ", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,8 +21,8 @@ type hostConnection struct {
|
|||||||
Port string
|
Port string
|
||||||
User string
|
User string
|
||||||
}
|
}
|
||||||
func GetHost() []hostConnection {
|
func GetHost(path string) []hostConnection {
|
||||||
fileData, err := os.ReadFile("/Users/cangui/.ssh/config")
|
fileData, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading file:", err)
|
fmt.Println("Error reading file:", err)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user