mirror of
https://github.com/golang/go
synced 2024-11-19 01:44:40 -07:00
120 lines
2.5 KiB
Go
120 lines
2.5 KiB
Go
|
// 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.
|
||
|
|
||
|
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.")
|
||
|
return exitCleanly
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func chooseVersion(ctx context.Context) error {
|
||
|
// TODO: check if go is currently installed
|
||
|
// TODO: if go is currently installed install new version over that
|
||
|
|
||
|
if *goVersion != "" {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
var err error
|
||
|
*goVersion, err = getLatestGoVersion()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
answer, err := prompt(ctx, fmt.Sprintf("The latest go version is %s, install that? Y/n", *goVersion), "Y")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if strings.ToLower(answer) != "y" {
|
||
|
// TODO: handle passing a version
|
||
|
fmt.Println("Aborting install.")
|
||
|
return exitCleanly
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func downloadGo(ctx context.Context) error {
|
||
|
answer, err := prompt(ctx, fmt.Sprintf("Download go version %s to %s? Y/n", *goVersion, installPath), "Y")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if strings.ToLower(answer) != "y" {
|
||
|
fmt.Println("Aborting install.")
|
||
|
return exitCleanly
|
||
|
}
|
||
|
|
||
|
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.")
|
||
|
return exitCleanly
|
||
|
}
|
||
|
|
||
|
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()
|
||
|
}
|