From 3e27057c1b3fbc7094528a9aca2c3339fb368c67 Mon Sep 17 00:00:00 2001 From: Davey Shafik Date: Mon, 21 Aug 2017 19:48:12 -0700 Subject: [PATCH] Fix first run and upgrades on Windows Fixes #15 --- akamai.go | 2 +- firstrun.go | 33 +++++++++++++++++++++++---------- upgrade.go | 10 ++++++++-- 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/akamai.go b/akamai.go index e1f025c..4575d0d 100644 --- a/akamai.go +++ b/akamai.go @@ -42,7 +42,7 @@ import ( ) const ( - VERSION = "0.3.1" + VERSION = "0.3.2" ) func main() { diff --git a/firstrun.go b/firstrun.go index 7019f6a..8a5d5e3 100644 --- a/firstrun.go +++ b/firstrun.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "os" - "path" "path/filepath" "runtime" "strconv" @@ -18,11 +17,15 @@ import ( func firstRun() error { selfPath, err := osext.Executable() - os.Args[0] = selfPath if err != nil { return err } - dirPath := path.Dir(selfPath) + os.Args[0] = selfPath + dirPath := filepath.Dir(selfPath) + + if runtime.GOOS == "windows" { + dirPath = strings.ToLower(dirPath) + } sysPath := os.Getenv("PATH") paths := filepath.SplitList(sysPath) @@ -34,9 +37,14 @@ func firstRun() error { } for _, path := range paths { - if checkAccess(path, ACCESS_W_OK) != nil { + if checkAccess(path, ACCESS_W_OK) != nil || len(strings.TrimSpace(path)) == 0 { continue } + + if runtime.GOOS == "windows" { + path = strings.ToLower(path) + } + writablePaths = append(writablePaths, path) if path == dirPath { @@ -76,19 +84,24 @@ func firstRun() error { goto choosePath } - status := getSpinner("Installing to "+writablePaths[index-1]+"/akamai...", "Installing to "+writablePaths[index-1]+"/akamai...... ["+color.GreenString("OK")+"]\n") - status.Start() - suffix := "" if runtime.GOOS == "windows" { suffix = ".exe" } - err = os.Rename(selfPath, writablePaths[index-1]+"/akamai"+suffix) - os.Args[0] = writablePaths[index-1] + "/akamai" + suffix + newPath := writablePaths[index-1]+string(os.PathSeparator)+"akamai" + suffix + + status := getSpinner( + "Installing to "+ newPath + "...", + "Installing to "+ newPath + "...... ["+color.GreenString("OK")+"]\n", + ) + status.Start() + + err = os.Rename(selfPath, newPath) + os.Args[0] = newPath if err != nil { - status.FinalMSG = "Installing to " + writablePaths[index-1] + "/akamai...... [" + color.RedString("FAIL") + "]\n" + status.FinalMSG = "Installing to "+ newPath + "...... [" + color.RedString("FAIL") + "]\n" status.Stop() color.Red(err.Error()) } diff --git a/upgrade.go b/upgrade.go index 01685ad..13c0ff4 100644 --- a/upgrade.go +++ b/upgrade.go @@ -17,7 +17,7 @@ import ( "github.com/fatih/color" "github.com/inconshreveable/go-update" - "github.com/yookoala/realpath" + "github.com/kardianos/osext" ) func checkForUpgrade(force bool) string { @@ -158,7 +158,13 @@ func upgradeCli(latestVersion string) bool { return false } - selfPath, err := realpath.Realpath(os.Args[0]) + selfPath, err := osext.Executable() + if err != nil { + status.FinalMSG = status.Prefix + "...... [" + color.RedString("FAIL") + "]\n" + status.Stop() + color.Red("Unable to determine install location") + return false + } err = update.Apply(resp.Body, update.Options{TargetPath: selfPath, Checksum: shasum}) if err != nil {