Skip to content

Commit

Permalink
Fix first run and upgrades on Windows
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
dshafik committed Aug 22, 2017
1 parent 73f3a25 commit 3e27057
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion akamai.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
)

const (
VERSION = "0.3.1"
VERSION = "0.3.2"
)

func main() {
Expand Down
33 changes: 23 additions & 10 deletions firstrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}
Expand Down
10 changes: 8 additions & 2 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3e27057

Please sign in to comment.