You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.8 KiB

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