diff --git a/cmd/getgo/main.go b/cmd/getgo/main.go index 58cfbdf169..8d9fbd41bb 100644 --- a/cmd/getgo/main.go +++ b/cmd/getgo/main.go @@ -50,6 +50,7 @@ func main() { if !*setupOnly { runStep(welcome) + runStep(checkOthers) runStep(chooseVersion) runStep(downloadGo) } diff --git a/cmd/getgo/steps.go b/cmd/getgo/steps.go index b23f1b7791..eac6517d6b 100644 --- a/cmd/getgo/steps.go +++ b/cmd/getgo/steps.go @@ -29,10 +29,22 @@ func welcome(ctx context.Context) error { return nil } -func chooseVersion(ctx context.Context) error { - // TODO: check if go is currently installed +func checkOthers(ctx context.Context) error { // TODO: if go is currently installed install new version over that + path, err := whichGo(ctx) + if err != nil { + fmt.Printf("Cannot check if Go is already installed:\n%v\n", err) + } + if path == "" { + return nil + } + if path != installPath { + fmt.Printf("Go is already installed at %v; remove it from your PATH.\n", path) + } + return nil +} +func chooseVersion(ctx context.Context) error { if *goVersion != "" { return nil } diff --git a/cmd/getgo/system.go b/cmd/getgo/system.go index d424dda9e5..8b5b024d27 100644 --- a/cmd/getgo/system.go +++ b/cmd/getgo/system.go @@ -6,8 +6,10 @@ package main import ( "bytes" + "context" "os/exec" "runtime" + "strings" ) // arch contains either amd64 or 386. @@ -27,3 +29,8 @@ var arch = func() string { } return "386" }() + +func findGo(ctx context.Context, cmd string) (string, error) { + out, err := exec.CommandContext(ctx, cmd, "go").CombinedOutput() + return strings.TrimSpace(string(out)), err +} diff --git a/cmd/getgo/system_unix.go b/cmd/getgo/system_unix.go index a3f5957848..d2405c5c08 100644 --- a/cmd/getgo/system_unix.go +++ b/cmd/getgo/system_unix.go @@ -7,6 +7,7 @@ package main import ( + "context" "fmt" "os" "path/filepath" @@ -28,6 +29,10 @@ var installPath = func() string { return filepath.Join(home, ".go") }() +func whichGo(ctx context.Context) (string, error) { + return findGo(ctx, "which") +} + func isWindowsXP() bool { return false } diff --git a/cmd/getgo/system_windows.go b/cmd/getgo/system_windows.go index 45ce92b92b..d8a61917d8 100644 --- a/cmd/getgo/system_windows.go +++ b/cmd/getgo/system_windows.go @@ -7,6 +7,7 @@ package main import ( + "context" "log" "os" "syscall" @@ -31,6 +32,10 @@ func isWindowsXP() bool { return major < 6 } +func whichGo(ctx context.Context) (string, error) { + return findGo(ctx, "where") +} + // currentShell reports the current shell. // It might be "powershell.exe", "cmd.exe" or any of the *nix shells. //