2017-07-25 15:46:40 -06:00
|
|
|
// Copyright 2017 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-11-29 12:51:57 -07:00
|
|
|
// +build !plan9
|
|
|
|
|
2017-07-25 15:46:40 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type step func(context.Context) error
|
|
|
|
|
|
|
|
func welcome(ctx context.Context) error {
|
|
|
|
fmt.Println("Welcome to the Go installer!")
|
|
|
|
answer, err := prompt(ctx, "Would you like to install Go? Y/n", "Y")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.ToLower(answer) != "y" {
|
|
|
|
fmt.Println("Exiting install.")
|
2019-11-20 20:43:00 -07:00
|
|
|
return errExitCleanly
|
2017-07-25 15:46:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:51:58 -06:00
|
|
|
func checkOthers(ctx context.Context) error {
|
2017-07-25 15:46:40 -06:00
|
|
|
// TODO: if go is currently installed install new version over that
|
2017-07-29 13:51:58 -06:00
|
|
|
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
|
|
|
|
}
|
2017-07-25 15:46:40 -06:00
|
|
|
|
2017-07-29 13:51:58 -06:00
|
|
|
func chooseVersion(ctx context.Context) error {
|
2017-07-25 15:46:40 -06:00
|
|
|
if *goVersion != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
*goVersion, err = getLatestGoVersion()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:25:26 -06:00
|
|
|
answer, err := prompt(ctx, fmt.Sprintf("The latest Go version is %s, install that? Y/n", *goVersion), "Y")
|
2017-07-25 15:46:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(answer) != "y" {
|
|
|
|
// TODO: handle passing a version
|
|
|
|
fmt.Println("Aborting install.")
|
2019-11-20 20:43:00 -07:00
|
|
|
return errExitCleanly
|
2017-07-25 15:46:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadGo(ctx context.Context) error {
|
2017-07-29 13:25:26 -06:00
|
|
|
answer, err := prompt(ctx, fmt.Sprintf("Download Go version %s to %s? Y/n", *goVersion, installPath), "Y")
|
2017-07-25 15:46:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(answer) != "y" {
|
|
|
|
fmt.Println("Aborting install.")
|
2019-11-20 20:43:00 -07:00
|
|
|
return errExitCleanly
|
2017-07-25 15:46:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Downloading Go version %s to %s\n", *goVersion, installPath)
|
|
|
|
fmt.Println("This may take a bit of time...")
|
|
|
|
|
|
|
|
if err := downloadGoVersion(*goVersion, runtime.GOOS, arch, installPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := appendToPATH(filepath.Join(installPath, "bin")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Downloaded!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupGOPATH(ctx context.Context) error {
|
|
|
|
answer, err := prompt(ctx, "Would you like us to setup your GOPATH? Y/n", "Y")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.ToLower(answer) != "y" {
|
|
|
|
fmt.Println("Exiting and not setting up GOPATH.")
|
2019-11-20 20:43:00 -07:00
|
|
|
return errExitCleanly
|
2017-07-25 15:46:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Setting up GOPATH")
|
|
|
|
home, err := getHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
gopath := os.Getenv("GOPATH")
|
|
|
|
if gopath == "" {
|
|
|
|
// set $GOPATH
|
|
|
|
gopath = filepath.Join(home, "go")
|
|
|
|
if err := persistEnvVar("GOPATH", gopath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println("GOPATH has been set up!")
|
|
|
|
} else {
|
|
|
|
verbosef("GOPATH is already set to %s", gopath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := appendToPATH(filepath.Join(gopath, "bin")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return persistEnvChangesForSession()
|
|
|
|
}
|