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.
28 // A Command is an implementation of a go command
29 // like go build or go fix.
31 // Run runs the command.
32 // The args are the arguments after the command name.
33 Run func(cmd *Command, args []string)
35 // UsageLine is the one-line usage message.
36 // The first word in the line is taken to be the command name.
39 // Short is the short description shown in the 'go help' output.
42 // Long is the long message shown in the 'go help <this-command>' output.
45 // Flag is a set of flags specific to this command.
48 // CustomFlags indicates that the command will do its own
53 // Name returns the command's name: the first word in the usage line.
54 func (c *Command) Name() string {
56 i := strings.Index(name, " ")
63 func (c *Command) Usage() {
64 fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
65 fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
69 // Runnable reports whether the command can be run; otherwise
70 // it is a documentation pseudo-command such as importpath.
71 func (c *Command) Runnable() bool {
75 // Commands lists the available commands and help topics.
76 // The order here is the order in which they are printed by 'go help'.
77 var commands = []*Command{
103 var exitMu sync.Mutex
105 func setExitStatus(n int) {
124 if args[0] == "help" {
129 // Diagnose common mistake: GOPATH==GOROOT.
130 // This setting is equivalent to not setting GOPATH at all,
131 // which is not what most people want when they do it.
132 if gopath := os.Getenv("GOPATH"); gopath == runtime.GOROOT() {
133 fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath)
135 for _, p := range filepath.SplitList(gopath) {
136 // Note: using HasPrefix instead of Contains because a ~ can appear
137 // in the middle of directory elements, such as /tmp/git-1.8.2~rc3
138 // or C:\PROGRA~1. Only ~ as a path prefix has meaning to the shell.
139 if strings.HasPrefix(p, "~") {
140 fmt.Fprintf(os.Stderr, "go: GOPATH entry cannot start with shell metacharacter '~': %q\n", p)
143 if build.IsLocalImport(p) {
144 fmt.Fprintf(os.Stderr, "go: GOPATH entry is relative; must be absolute path: %q.\nRun 'go help gopath' for usage.\n", p)
150 if fi, err := os.Stat(goroot); err != nil || !fi.IsDir() {
151 fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", goroot)
155 for _, cmd := range commands {
156 if cmd.Name() == args[0] && cmd.Runnable() {
157 cmd.Flag.Usage = func() { cmd.Usage() }
161 cmd.Flag.Parse(args[1:])
162 args = cmd.Flag.Args()
170 fmt.Fprintf(os.Stderr, "go: unknown subcommand %q\nRun 'go help' for usage.\n", args[0])
175 var usageTemplate = `Go is a tool for managing Go source code.
179 go command [arguments]
182 {{range .}}{{if .Runnable}}
183 {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
185 Use "go help [command]" for more information about a command.
187 Additional help topics:
188 {{range .}}{{if not .Runnable}}
189 {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
191 Use "go help [topic]" for more information about that topic.
195 var helpTemplate = `{{if .Runnable}}usage: go {{.UsageLine}}
197 {{end}}{{.Long | trim}}
200 var documentationTemplate = `// Copyright 2011 The Go Authors. All rights reserved.
201 // Use of this source code is governed by a BSD-style
202 // license that can be found in the LICENSE file.
204 // DO NOT EDIT THIS FILE. GENERATED BY mkdoc.sh.
205 // Edit the documentation in other files and rerun mkdoc.sh to generate this one.
208 {{range .}}{{if .Short}}{{.Short | capitalize}}
210 {{end}}{{if .Runnable}}Usage:
214 {{end}}{{.Long | trim}}
221 // tmpl executes the given template text on data, writing the result to w.
222 func tmpl(w io.Writer, text string, data interface{}) {
223 t := template.New("top")
224 t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
225 template.Must(t.Parse(text))
226 if err := t.Execute(w, data); err != nil {
231 func capitalize(s string) string {
235 r, n := utf8.DecodeRuneInString(s)
236 return string(unicode.ToTitle(r)) + s[n:]
239 func printUsage(w io.Writer) {
240 bw := bufio.NewWriter(w)
241 tmpl(bw, usageTemplate, commands)
246 // special case "go test -h"
247 if len(os.Args) > 1 && os.Args[1] == "test" {
248 help([]string{"testflag"})
251 printUsage(os.Stderr)
255 // help implements the 'help' command.
256 func help(args []string) {
258 printUsage(os.Stdout)
259 // not exit 2: succeeded at 'go help'.
263 fmt.Fprintf(os.Stderr, "usage: go help command\n\nToo many arguments given.\n")
264 os.Exit(2) // failed at 'go help'
269 // 'go help documentation' generates doc.go.
270 if arg == "documentation" {
271 buf := new(bytes.Buffer)
273 usage := &Command{Long: buf.String()}
274 tmpl(os.Stdout, documentationTemplate, append([]*Command{usage}, commands...))
278 for _, cmd := range commands {
279 if cmd.Name() == arg {
280 tmpl(os.Stdout, helpTemplate, cmd)
281 // not exit 2: succeeded at 'go help cmd'.
286 fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'go help'.\n", arg)
287 os.Exit(2) // failed at 'go help cmd'
290 // importPathsNoDotExpansion returns the import paths to use for the given
291 // command line, but it does no ... expansion.
292 func importPathsNoDotExpansion(args []string) []string {
297 for _, a := range args {
298 // Arguments are supposed to be import paths, but
299 // as a courtesy to Windows developers, rewrite \ to /
300 // in command-line arguments. Handles .\... and so on.
301 if filepath.Separator == '\\' {
302 a = strings.Replace(a, `\`, `/`, -1)
305 // Put argument in canonical form, but preserve leading ./.
306 if strings.HasPrefix(a, "./") {
307 a = "./" + path.Clean(a)
314 if a == "all" || a == "std" {
315 out = append(out, allPackages(a)...)
323 // importPaths returns the import paths to use for the given command line.
324 func importPaths(args []string) []string {
325 args = importPathsNoDotExpansion(args)
327 for _, a := range args {
328 if strings.Contains(a, "...") {
329 if build.IsLocalImport(a) {
330 out = append(out, allPackagesInFS(a)...)
332 out = append(out, allPackages(a)...)
341 var atexitFuncs []func()
343 func atexit(f func()) {
344 atexitFuncs = append(atexitFuncs, f)
348 for _, f := range atexitFuncs {
354 func fatalf(format string, args ...interface{}) {
355 errorf(format, args...)
359 func errorf(format string, args ...interface{}) {
360 log.Printf(format, args...)
364 var logf = log.Printf
366 func exitIfErrors() {
372 func run(cmdargs ...interface{}) {
373 cmdline := stringList(cmdargs...)
374 if buildN || buildX {
375 fmt.Printf("%s\n", strings.Join(cmdline, " "))
381 cmd := exec.Command(cmdline[0], cmdline[1:]...)
382 cmd.Stdout = os.Stdout
383 cmd.Stderr = os.Stderr
384 if err := cmd.Run(); err != nil {
389 func runOut(dir string, cmdargs ...interface{}) []byte {
390 cmdline := stringList(cmdargs...)
391 cmd := exec.Command(cmdline[0], cmdline[1:]...)
393 out, err := cmd.CombinedOutput()
402 // envForDir returns a copy of the environment
403 // suitable for running in the given directory.
404 // The environment is the current process's environment
405 // but with an updated $PWD, so that an os.Getwd in the
406 // child will be faster.
407 func envForDir(dir string) []string {
409 // Internally we only use rooted paths, so dir is rooted.
410 // Even if dir is not rooted, no harm done.
411 return mergeEnvLists([]string{"PWD=" + dir}, env)
414 // mergeEnvLists merges the two environment lists such that
415 // variables with the same name in "in" replace those in "out".
416 func mergeEnvLists(in, out []string) []string {
418 for _, inkv := range in {
419 k := strings.SplitAfterN(inkv, "=", 2)[0]
420 for i, outkv := range out {
421 if strings.HasPrefix(outkv, k) {
426 out = append(out, inkv)
431 // matchPattern(pattern)(name) reports whether
432 // name matches pattern. Pattern is a limited glob
433 // pattern in which '...' means 'any string' and there
434 // is no other special syntax.
435 func matchPattern(pattern string) func(name string) bool {
436 re := regexp.QuoteMeta(pattern)
437 re = strings.Replace(re, `\.\.\.`, `.*`, -1)
438 // Special case: foo/... matches foo too.
439 if strings.HasSuffix(re, `/.*`) {
440 re = re[:len(re)-len(`/.*`)] + `(/.*)?`
442 reg := regexp.MustCompile(`^` + re + `$`)
443 return func(name string) bool {
444 return reg.MatchString(name)
448 // hasPathPrefix reports whether the path s begins with the
449 // elements in prefix.
450 func hasPathPrefix(s, prefix string) bool {
454 case len(s) == len(prefix):
456 case len(s) > len(prefix):
457 if prefix != "" && prefix[len(prefix)-1] == '/' {
458 return strings.HasPrefix(s, prefix)
460 return s[len(prefix)] == '/' && s[:len(prefix)] == prefix
464 // treeCanMatchPattern(pattern)(name) reports whether
465 // name or children of name can possibly match pattern.
466 // Pattern is the same limited glob accepted by matchPattern.
467 func treeCanMatchPattern(pattern string) func(name string) bool {
469 if i := strings.Index(pattern, "..."); i >= 0 {
471 pattern = pattern[:i]
473 return func(name string) bool {
474 return len(name) <= len(pattern) && hasPathPrefix(pattern, name) ||
475 wildCard && strings.HasPrefix(name, pattern)
479 // allPackages returns all the packages that can be found
480 // under the $GOPATH directories and $GOROOT matching pattern.
481 // The pattern is either "all" (all packages), "std" (standard packages)
482 // or a path including "...".
483 func allPackages(pattern string) []string {
484 pkgs := matchPackages(pattern)
486 fmt.Fprintf(os.Stderr, "warning: %q matched no packages\n", pattern)
491 func matchPackages(pattern string) []string {
492 match := func(string) bool { return true }
493 treeCanMatch := func(string) bool { return true }
494 if pattern != "all" && pattern != "std" {
495 match = matchPattern(pattern)
496 treeCanMatch = treeCanMatchPattern(pattern)
499 have := map[string]bool{
500 "builtin": true, // ignore pseudo-package that exists only for documentation
502 if !buildContext.CgoEnabled {
503 have["runtime/cgo"] = true // ignore during walk
508 cmd := filepath.Join(goroot, "src/cmd") + string(filepath.Separator)
509 filepath.Walk(cmd, func(path string, fi os.FileInfo, err error) error {
510 if err != nil || !fi.IsDir() || path == cmd {
513 name := path[len(cmd):]
514 if !treeCanMatch(name) {
515 return filepath.SkipDir
517 // Commands are all in cmd/, not in subdirectories.
518 if strings.Contains(name, string(filepath.Separator)) {
519 return filepath.SkipDir
522 // We use, e.g., cmd/gofmt as the pseudo import path for gofmt.
531 _, err = buildContext.ImportDir(path, 0)
533 if _, noGo := err.(*build.NoGoError); !noGo {
538 pkgs = append(pkgs, name)
542 for _, src := range buildContext.SrcDirs() {
543 if pattern == "std" && src != gorootSrc {
546 src = filepath.Clean(src) + string(filepath.Separator)
547 filepath.Walk(src, func(path string, fi os.FileInfo, err error) error {
548 if err != nil || !fi.IsDir() || path == src {
552 // Avoid .foo, _foo, and testdata directory trees.
553 _, elem := filepath.Split(path)
554 if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
555 return filepath.SkipDir
558 name := filepath.ToSlash(path[len(src):])
559 if pattern == "std" && strings.Contains(name, ".") {
560 return filepath.SkipDir
562 if !treeCanMatch(name) {
563 return filepath.SkipDir
572 _, err = buildContext.ImportDir(path, 0)
574 if _, noGo := err.(*build.NoGoError); noGo {
578 pkgs = append(pkgs, name)
585 // allPackagesInFS is like allPackages but is passed a pattern
586 // beginning ./ or ../, meaning it should scan the tree rooted
587 // at the given directory. There are ... in the pattern too.
588 func allPackagesInFS(pattern string) []string {
589 pkgs := matchPackagesInFS(pattern)
591 fmt.Fprintf(os.Stderr, "warning: %q matched no packages\n", pattern)
596 func matchPackagesInFS(pattern string) []string {
597 // Find directory to begin the scan.
598 // Could be smarter but this one optimization
599 // is enough for now, since ... is usually at the
601 i := strings.Index(pattern, "...")
602 dir, _ := path.Split(pattern[:i])
604 // pattern begins with ./ or ../.
605 // path.Clean will discard the ./ but not the ../.
606 // We need to preserve the ./ for pattern matching
607 // and in the returned import paths.
609 if strings.HasPrefix(pattern, "./") {
612 match := matchPattern(pattern)
615 filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
616 if err != nil || !fi.IsDir() {
620 // filepath.Walk starts at dir and recurses. For the recursive case,
621 // the path is the result of filepath.Join, which calls filepath.Clean.
622 // The initial case is not Cleaned, though, so we do this explicitly.
624 // This converts a path like "./io/" to "io". Without this step, running
625 // "cd $GOROOT/src; go list ./io/..." would incorrectly skip the io
626 // package, because prepending the prefix "./" to the unclean path would
627 // result in "././io", and match("././io") returns false.
628 path = filepath.Clean(path)
631 // Avoid .foo, _foo, and testdata directory trees, but do not avoid "." or "..".
632 _, elem := filepath.Split(path)
633 dot := strings.HasPrefix(elem, ".") && elem != "." && elem != ".."
634 if dot || strings.HasPrefix(elem, "_") || elem == "testdata" {
635 return filepath.SkipDir
638 name := prefix + filepath.ToSlash(path)
642 if _, err = build.ImportDir(path, 0); err != nil {
643 if _, noGo := err.(*build.NoGoError); !noGo {
648 pkgs = append(pkgs, name)
654 // stringList's arguments should be a sequence of string or []string values.
655 // stringList flattens them into a single []string.
656 func stringList(args ...interface{}) []string {
658 for _, arg := range args {
659 switch arg := arg.(type) {
661 x = append(x, arg...)
665 panic("stringList: invalid argument of type " + fmt.Sprintf("%T", arg))
671 // toFold returns a string with the property that
672 // strings.EqualFold(s, t) iff toFold(s) == toFold(t)
673 // This lets us test a large set of strings for fold-equivalent
674 // duplicates without making a quadratic number of calls
675 // to EqualFold. Note that strings.ToUpper and strings.ToLower
676 // have the desired property in some corner cases.
677 func toFold(s string) string {
678 // Fast path: all ASCII, no upper case.
679 // Most paths look like this already.
680 for i := 0; i < len(s); i++ {
682 if c >= utf8.RuneSelf || 'A' <= c && c <= 'Z' {
690 for _, r := range s {
691 // SimpleFold(x) cycles to the next equivalent rune > x
692 // or wraps around to smaller values. Iterate until it wraps,
693 // and we've found the minimum value.
696 r = unicode.SimpleFold(r0)
701 // Exception to allow fast path above: A-Z => a-z
702 if 'A' <= r && r <= 'Z' {
710 // foldDup reports a pair of strings from the list that are
711 // equal according to strings.EqualFold.
712 // It returns "", "" if there are no such strings.
713 func foldDup(list []string) (string, string) {
714 clash := map[string]string{}
715 for _, s := range list {
717 if t := clash[fold]; t != "" {