| 1 | // Copyright 2017 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | //go:build !plan9 |
| 6 | // +build !plan9 |
| 7 | |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "context" |
| 12 | "fmt" |
| 13 | "os" |
| 14 | "path/filepath" |
| 15 | "runtime" |
| 16 | "strings" |
| 17 | ) |
| 18 | |
| 19 | type step func(context.Context) error |
| 20 | |
| 21 | func welcome(ctx context.Context) error { |
| 22 | fmt.Println("Welcome to the Go installer!") |
| 23 | answer, err := prompt(ctx, "Would you like to install Go? Y/n", "Y") |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | if strings.ToLower(answer) != "y" { |
| 28 | fmt.Println("Exiting install.") |
| 29 | return errExitCleanly |
| 30 | } |
| 31 | |
| 32 | return nil |
| 33 | } |
| 34 | |
| 35 | func checkOthers(ctx context.Context) error { |
| 36 | // TODO: if go is currently installed install new version over that |
| 37 | path, err := whichGo(ctx) |
| 38 | if err != nil { |
| 39 | fmt.Printf("Cannot check if Go is already installed:\n%v\n", err) |
| 40 | } |
| 41 | if path == "" { |
| 42 | return nil |
| 43 | } |
| 44 | if path != installPath { |
| 45 | fmt.Printf("Go is already installed at %v; remove it from your PATH.\n", path) |
| 46 | } |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | func chooseVersion(ctx context.Context) error { |
| 51 | if *goVersion != "" { |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | var err error |
| 56 | *goVersion, err = getLatestGoVersion() |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | answer, err := prompt(ctx, fmt.Sprintf("The latest Go version is %s, install that? Y/n", *goVersion), "Y") |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | |
| 66 | if strings.ToLower(answer) != "y" { |
| 67 | // TODO: handle passing a version |
| 68 | fmt.Println("Aborting install.") |
| 69 | return errExitCleanly |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | func downloadGo(ctx context.Context) error { |
| 76 | answer, err := prompt(ctx, fmt.Sprintf("Download Go version %s to %s? Y/n", *goVersion, installPath), "Y") |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | if strings.ToLower(answer) != "y" { |
| 82 | fmt.Println("Aborting install.") |
| 83 | return errExitCleanly |
| 84 | } |
| 85 | |
| 86 | fmt.Printf("Downloading Go version %s to %s\n", *goVersion, installPath) |
| 87 | fmt.Println("This may take a bit of time...") |
| 88 | |
| 89 | if err := downloadGoVersion(*goVersion, runtime.GOOS, arch, installPath); err != nil { |
| 90 | return err |
| 91 | } |
| 92 | |
| 93 | if err := appendToPATH(filepath.Join(installPath, "bin")); err != nil { |
| 94 | return err |
| 95 | } |
| 96 | |
| 97 | fmt.Println("Downloaded!") |
| 98 | return nil |
| 99 | } |
| 100 | |
| 101 | func setupGOPATH(ctx context.Context) error { |
| 102 | answer, err := prompt(ctx, "Would you like us to setup your GOPATH? Y/n", "Y") |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | if strings.ToLower(answer) != "y" { |
| 108 | fmt.Println("Exiting and not setting up GOPATH.") |
| 109 | return errExitCleanly |
| 110 | } |
| 111 | |
| 112 | fmt.Println("Setting up GOPATH") |
| 113 | home, err := getHomeDir() |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | |
| 118 | gopath := os.Getenv("GOPATH") |
| 119 | if gopath == "" { |
| 120 | // set $GOPATH |
| 121 | gopath = filepath.Join(home, "go") |
| 122 | if err := persistEnvVar("GOPATH", gopath); err != nil { |
| 123 | return err |
| 124 | } |
| 125 | fmt.Println("GOPATH has been set up!") |
| 126 | } else { |
| 127 | verbosef("GOPATH is already set to %s", gopath) |
| 128 | } |
| 129 | |
| 130 | if err := appendToPATH(filepath.Join(gopath, "bin")); err != nil { |
| 131 | return err |
| 132 | } |
| 133 | return persistEnvChangesForSession() |
| 134 | } |
| 135 |
Members