From 22cffe21b437b210d85bb58f61647a7dfc0c9f26 Mon Sep 17 00:00:00 2001 From: cangui Date: Wed, 11 Dec 2024 20:59:13 +0100 Subject: [PATCH] Ajout de gotenv work in progres --- .gitignore | 1 + function/main.go | 52 ++++++++++++++++++++++++++-------- go.mod | 1 + go.sum | 2 ++ main.go | 69 ++++++++++++++++++++++++++++++++++++++-------- simply-ssh/main.go | 4 +-- 6 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/function/main.go b/function/main.go index 63bcb97..b6960cf 100644 --- a/function/main.go +++ b/function/main.go @@ -2,19 +2,22 @@ package function import ( "fmt" + "log" + "github.com/xuri/excelize/v2" ) -func ReadExcel(path string,host string) { +func ReadExcel(path string)[]map[string]string { // var sql []string // var pathSite []string // var pathBackupSite[]string // var pathBackupSiteMysql[]string + var headers []string f, err := excelize.OpenFile(path) if err != nil { fmt.Println(err) - return + return nil } defer func() { // Close the spreadsheet. @@ -26,22 +29,47 @@ func ReadExcel(path string,host string) { cell, err := f.GetCellValue("julien", "A2") if err != nil { fmt.Println(err) - return + return nil } fmt.Println("-------------------------------------") fmt.Println(cell) fmt.Println("-------------------------------------") // 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 { - fmt.Println(err) - return + log.Fatalf("Erreur lors de la lecture des lignes: %v", err) } - for _, row := range rows { - //fmt.Print(row) - for _, colCell := range row { - fmt.Print(colCell) - } - fmt.Println() + + // Vérifier qu'il y a des lignes + if len(rows) == 0 { + log.Fatal("La feuille est vide ou introuvable") } + + // 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 } \ No newline at end of file diff --git a/go.mod b/go.mod index 4fb87bc..7ef41b2 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.2 require github.com/TwiN/go-choice v1.2.0 require ( + github.com/joho/godotenv v1.5.1 // indirect github.com/kr/fs v0.1.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index d31ae79..ccbf709 100644 --- a/go.sum +++ b/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.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= 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/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= diff --git a/main.go b/main.go index ddefab1..b1eafdc 100644 --- a/main.go +++ b/main.go @@ -4,27 +4,36 @@ import ( "bytes" "fmt" "log" + "os" "time" "canguidev.fr/pvnet/function" simplyssh "canguidev.fr/pvnet/simply-ssh" "github.com/TwiN/go-choice" - "github.com/fatih/color" + "github.com/joho/godotenv" "github.com/pkg/sftp" ) func main(){ //var path string="/Users/cangui/.ssh/julien_pvnet" - function.ReadExcel("Z:/_backup/src_golang_exe.xlsx","vps1") - color.Blue("Enter path ssh key: ") - - var path string - // fonction pour recupére la donnée saisie - fmt.Scanln(&path) + ///Users/cangui/Documents/Dev peso/Golang dev/test.xlsx + 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 + // Chargement de la liste des hosts du fichier config ssh - d:=simplyssh.GetHost() + d:=simplyssh.GetHost(pathConfigSsh) var s []string for _, host := range d { + fmt.Println(host) s=append(s,string(host.Id)+" "+host.Host) } // selectionne du host @@ -32,15 +41,16 @@ func main(){ "What do you want to do?\nPick:", s, ) + selectHost:=choice if err != nil { - fmt.Println("You didn't select anything!") + fmt.Println("You didn't select anything!",err) } else { for _, host := range d { // for sur les hosts et break sur le host selectionner if(host.Id == uint(index)){ fmt.Println("debut de connection") // 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" defer client.Close() @@ -83,11 +93,46 @@ func main(){ fmt.Printf("You have selected: '%s', which is the index %d\n", choice, index) 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() formattedTime := currentTime.Format("20060102_150405") 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 if err := sessionBackup.Run(commandBackup); err != nil { @@ -108,7 +153,7 @@ func main(){ formattedTime2:= currentTime2.Format("20060102_150405") 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 { log.Fatal("Erreur lors du téléchargement: ", err) } diff --git a/simply-ssh/main.go b/simply-ssh/main.go index 307a7db..77f7fe5 100644 --- a/simply-ssh/main.go +++ b/simply-ssh/main.go @@ -21,8 +21,8 @@ type hostConnection struct { Port string User string } -func GetHost() []hostConnection { - fileData, err := os.ReadFile("/Users/cangui/.ssh/config") +func GetHost(path string) []hostConnection { + fileData, err := os.ReadFile(path) if err != nil { fmt.Println("Error reading file:", err) return nil