github.com/april1989/origin-go-tools@v0.0.32/cmd/getgo/steps.go (about)

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