From ba8ee152fcdf89e04700e1261608e2dd56df0524 Mon Sep 17 00:00:00 2001 From: Emeric Quervet <55890627+equervet@users.noreply.github.com> Date: Tue, 4 Feb 2025 20:17:39 +0000 Subject: [PATCH] Fix bug downloading with windows + replace symlink with copy file --- lib/command.go | 30 ++++++++++++++++++++++++++++++ lib/download.go | 4 +++- lib/install.go | 46 +++++++++++++++++++++++++++++----------------- 3 files changed, 62 insertions(+), 18 deletions(-) diff --git a/lib/command.go b/lib/command.go index fa00e5a..6dfe47e 100644 --- a/lib/command.go +++ b/lib/command.go @@ -6,6 +6,8 @@ import ( "path/filepath" "runtime" "strings" + "fmt" + "io" ) // Command : type string @@ -80,3 +82,31 @@ func (cmd *Command) Find() func() string { return <-pathChan } } + +func copyFile(sourceFile string, destinationFile string) bool { + // Open the source file + src, err := os.Open(sourceFile) + if err != nil { + fmt.Println("Error opening source file:", err) + return false + } + defer src.Close() + + // Create the destination file + dst, err := os.Create(destinationFile) + if err != nil { + fmt.Println("Error creating destination file:", err) + return false + } + defer dst.Close() + + // Copy the contents + _, err = io.Copy(dst, src) + if err != nil { + fmt.Println("Error copying file:", err) + return false + } + + fmt.Println("File copied successfully: ", sourceFile, "-", destinationFile) + return true +} \ No newline at end of file diff --git a/lib/download.go b/lib/download.go index 600522f..1973626 100644 --- a/lib/download.go +++ b/lib/download.go @@ -12,7 +12,9 @@ import ( func DownloadFromURL(installLocation string, url string) (string, error) { tokens := strings.Split(url, "/") - fileName := tokens[len(tokens)-1] + fileName := ConvertExecutableExt(tokens[len(tokens)-1]) + url = ConvertExecutableExt(url) + fmt.Println("Downloading", url, "to", fileName) fmt.Println("Downloading ...") diff --git a/lib/install.go b/lib/install.go index e71741c..9bdec26 100644 --- a/lib/install.go +++ b/lib/install.go @@ -225,19 +225,31 @@ func Install(tgversion string, usrBinPath string, mirrorURL string) string { /* rename unzipped file to terragrunt version name - terraform_x.x.x */ RenameFile(downloadedFile, installFileVersionPath) - err := os.Chmod(installFileVersionPath, 0755) - if err != nil { - log.Println(err) - } - /* remove current symlink if exist*/ - symlinkExist := CheckSymlink(binPath) + switch runtime.GOOS { + case "windows": + errCopy := copyFile(installFileVersionPath, ConvertExecutableExt(binPath)); + if errCopy != true { + fmt.Println("Error while copying file") + return "" + } - if symlinkExist { - RemoveSymlink(binPath) - } + default: + err := os.Chmod(installFileVersionPath, 0755) + if err != nil { + log.Println(err) + } + + /* remove current symlink if exist*/ + symlinkExist := CheckSymlink(binPath) + + if symlinkExist { + RemoveSymlink(binPath) + } - /* set symlink to desired version */ - CreateSymlink(installFileVersionPath, binPath) + /* set symlink to desired version */ + CreateSymlink(installFileVersionPath, binPath) + } + fmt.Printf("Switched terragrunt to version %q \n", tgversion) AddRecent(tgversion) //add to recent file for faster lookup os.Exit(0) @@ -297,12 +309,12 @@ func PrintCreateDirStmt(unableDir string, writable string) { //ConvertExecutableExt : convert excutable with local OS extension func ConvertExecutableExt(fpath string) string { switch runtime.GOOS { - case "windows": - if filepath.Ext(fpath) == ".exe" { + case "windows": + if filepath.Ext(fpath) == ".exe" { + return fpath + } + return fpath + ".exe" + default: return fpath - } - return fpath + ".exe" - default: - return fpath } }