Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows - Fix bug during download and copy #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path/filepath"
"runtime"
"strings"
"fmt"
"io"
)

// Command : type string
Expand Down Expand Up @@ -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
}
4 changes: 3 additions & 1 deletion lib/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...")

Expand Down
46 changes: 29 additions & 17 deletions lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}