...
Run Format

Source file src/cmd/go/main.go

Documentation: cmd/go

     1  // Copyright 2011 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:generate ./mkalldocs.sh
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"log"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  	"strings"
    17  
    18  	"cmd/go/internal/base"
    19  	"cmd/go/internal/bug"
    20  	"cmd/go/internal/cfg"
    21  	"cmd/go/internal/clean"
    22  	"cmd/go/internal/doc"
    23  	"cmd/go/internal/envcmd"
    24  	"cmd/go/internal/fix"
    25  	"cmd/go/internal/fmtcmd"
    26  	"cmd/go/internal/generate"
    27  	"cmd/go/internal/get"
    28  	"cmd/go/internal/help"
    29  	"cmd/go/internal/list"
    30  	"cmd/go/internal/modcmd"
    31  	"cmd/go/internal/modfetch"
    32  	"cmd/go/internal/modget"
    33  	"cmd/go/internal/modload"
    34  	"cmd/go/internal/run"
    35  	"cmd/go/internal/test"
    36  	"cmd/go/internal/tool"
    37  	"cmd/go/internal/version"
    38  	"cmd/go/internal/vet"
    39  	"cmd/go/internal/work"
    40  )
    41  
    42  func init() {
    43  	base.Go.Commands = []*base.Command{
    44  		bug.CmdBug,
    45  		work.CmdBuild,
    46  		clean.CmdClean,
    47  		doc.CmdDoc,
    48  		envcmd.CmdEnv,
    49  		fix.CmdFix,
    50  		fmtcmd.CmdFmt,
    51  		generate.CmdGenerate,
    52  		get.CmdGet,
    53  		work.CmdInstall,
    54  		list.CmdList,
    55  		modcmd.CmdMod,
    56  		run.CmdRun,
    57  		test.CmdTest,
    58  		tool.CmdTool,
    59  		version.CmdVersion,
    60  		vet.CmdVet,
    61  
    62  		help.HelpBuildmode,
    63  		help.HelpC,
    64  		help.HelpCache,
    65  		help.HelpEnvironment,
    66  		help.HelpFileType,
    67  		modload.HelpGoMod,
    68  		help.HelpGopath,
    69  		get.HelpGopathGet,
    70  		modfetch.HelpGoproxy,
    71  		help.HelpImportPath,
    72  		modload.HelpModules,
    73  		modget.HelpModuleGet,
    74  		help.HelpPackages,
    75  		test.HelpTestflag,
    76  		test.HelpTestfunc,
    77  	}
    78  }
    79  
    80  func main() {
    81  	_ = go11tag
    82  	flag.Usage = base.Usage
    83  	flag.Parse()
    84  	log.SetFlags(0)
    85  
    86  	args := flag.Args()
    87  	if len(args) < 1 {
    88  		base.Usage()
    89  	}
    90  
    91  	if modload.MustUseModules {
    92  		// If running with modules force-enabled, change get now to change help message.
    93  		*get.CmdGet = *modget.CmdGet
    94  	}
    95  
    96  	cfg.CmdName = args[0] // for error messages
    97  	if args[0] == "help" {
    98  		help.Help(args[1:])
    99  		return
   100  	}
   101  
   102  	// Diagnose common mistake: GOPATH==GOROOT.
   103  	// This setting is equivalent to not setting GOPATH at all,
   104  	// which is not what most people want when they do it.
   105  	if gopath := cfg.BuildContext.GOPATH; filepath.Clean(gopath) == filepath.Clean(runtime.GOROOT()) {
   106  		fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath)
   107  	} else {
   108  		for _, p := range filepath.SplitList(gopath) {
   109  			// Some GOPATHs have empty directory elements - ignore them.
   110  			// See issue 21928 for details.
   111  			if p == "" {
   112  				continue
   113  			}
   114  			// Note: using HasPrefix instead of Contains because a ~ can appear
   115  			// in the middle of directory elements, such as /tmp/git-1.8.2~rc3
   116  			// or C:\PROGRA~1. Only ~ as a path prefix has meaning to the shell.
   117  			if strings.HasPrefix(p, "~") {
   118  				fmt.Fprintf(os.Stderr, "go: GOPATH entry cannot start with shell metacharacter '~': %q\n", p)
   119  				os.Exit(2)
   120  			}
   121  			if !filepath.IsAbs(p) {
   122  				fmt.Fprintf(os.Stderr, "go: GOPATH entry is relative; must be absolute path: %q.\nFor more details see: 'go help gopath'\n", p)
   123  				os.Exit(2)
   124  			}
   125  		}
   126  	}
   127  
   128  	if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
   129  		fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
   130  		os.Exit(2)
   131  	}
   132  
   133  	// TODO(rsc): Remove all these helper prints in Go 1.12.
   134  	switch args[0] {
   135  	case "mod":
   136  		if len(args) >= 2 {
   137  			flag := args[1]
   138  			if strings.HasPrefix(flag, "--") {
   139  				flag = flag[1:]
   140  			}
   141  			if i := strings.Index(flag, "="); i >= 0 {
   142  				flag = flag[:i]
   143  			}
   144  			switch flag {
   145  			case "-sync":
   146  				fmt.Fprintf(os.Stderr, "go: go mod -sync is now go mod tidy\n")
   147  				os.Exit(2)
   148  			case "-init", "-fix", "-graph", "-vendor", "-verify":
   149  				fmt.Fprintf(os.Stderr, "go: go mod %s is now go mod %s\n", flag, flag[1:])
   150  				os.Exit(2)
   151  			case "-fmt", "-json", "-module", "-require", "-droprequire", "-replace", "-dropreplace", "-exclude", "-dropexclude":
   152  				fmt.Fprintf(os.Stderr, "go: go mod %s is now go mod edit %s\n", flag, flag)
   153  				os.Exit(2)
   154  			}
   155  		}
   156  	case "vendor":
   157  		fmt.Fprintf(os.Stderr, "go: vgo vendor is now go mod vendor\n")
   158  		os.Exit(2)
   159  	case "verify":
   160  		fmt.Fprintf(os.Stderr, "go: vgo verify is now go mod verify\n")
   161  		os.Exit(2)
   162  	}
   163  
   164  	if args[0] == "get" {
   165  		// Replace get with module-aware get if appropriate.
   166  		// Note that if MustUseModules is true, this happened already above,
   167  		// but no harm in doing it again.
   168  		if modload.Init(); modload.Enabled() {
   169  			*get.CmdGet = *modget.CmdGet
   170  		}
   171  	}
   172  
   173  	// Set environment (GOOS, GOARCH, etc) explicitly.
   174  	// In theory all the commands we invoke should have
   175  	// the same default computation of these as we do,
   176  	// but in practice there might be skew
   177  	// This makes sure we all agree.
   178  	cfg.OrigEnv = os.Environ()
   179  	cfg.CmdEnv = envcmd.MkEnv()
   180  	for _, env := range cfg.CmdEnv {
   181  		if os.Getenv(env.Name) != env.Value {
   182  			os.Setenv(env.Name, env.Value)
   183  		}
   184  	}
   185  
   186  BigCmdLoop:
   187  	for bigCmd := base.Go; ; {
   188  		for _, cmd := range bigCmd.Commands {
   189  			if cmd.Name() != args[0] {
   190  				continue
   191  			}
   192  			if len(cmd.Commands) > 0 {
   193  				bigCmd = cmd
   194  				args = args[1:]
   195  				if len(args) == 0 {
   196  					help.PrintUsage(os.Stderr, bigCmd)
   197  					base.SetExitStatus(2)
   198  					base.Exit()
   199  				}
   200  				if args[0] == "help" {
   201  					// Accept 'go mod help' and 'go mod help foo' for 'go help mod' and 'go help mod foo'.
   202  					help.Help(append(strings.Split(cfg.CmdName, " "), args[1:]...))
   203  					return
   204  				}
   205  				cfg.CmdName += " " + args[0]
   206  				continue BigCmdLoop
   207  			}
   208  			if !cmd.Runnable() {
   209  				continue
   210  			}
   211  			cmd.Flag.Usage = func() { cmd.Usage() }
   212  			if cmd.CustomFlags {
   213  				args = args[1:]
   214  			} else {
   215  				base.SetFromGOFLAGS(cmd.Flag)
   216  				cmd.Flag.Parse(args[1:])
   217  				args = cmd.Flag.Args()
   218  			}
   219  			cmd.Run(cmd, args)
   220  			base.Exit()
   221  			return
   222  		}
   223  		helpArg := ""
   224  		if i := strings.LastIndex(cfg.CmdName, " "); i >= 0 {
   225  			helpArg = " " + cfg.CmdName[:i]
   226  		}
   227  		fmt.Fprintf(os.Stderr, "go %s: unknown command\nRun 'go help%s' for usage.\n", cfg.CmdName, helpArg)
   228  		base.SetExitStatus(2)
   229  		base.Exit()
   230  	}
   231  }
   232  
   233  func init() {
   234  	base.Usage = mainUsage
   235  }
   236  
   237  func mainUsage() {
   238  	// special case "go test -h"
   239  	if len(os.Args) > 1 && os.Args[1] == "test" {
   240  		test.Usage()
   241  	}
   242  	help.PrintUsage(os.Stderr, base.Go)
   243  	os.Exit(2)
   244  }
   245  

View as plain text