Source file src/cmd/doc/main.go

     1  // Copyright 2015 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  // Doc (usually run as go doc) accepts zero, one or two arguments.
     6  //
     7  // Zero arguments:
     8  //
     9  //	go doc
    10  //
    11  // Show the documentation for the package in the current directory.
    12  //
    13  // One argument:
    14  //
    15  //	go doc <pkg>
    16  //	go doc <sym>[.<methodOrField>]
    17  //	go doc [<pkg>.]<sym>[.<methodOrField>]
    18  //	go doc [<pkg>.][<sym>.]<methodOrField>
    19  //
    20  // The first item in this list that succeeds is the one whose documentation
    21  // is printed. If there is a symbol but no package, the package in the current
    22  // directory is chosen. However, if the argument begins with a capital
    23  // letter it is always assumed to be a symbol in the current directory.
    24  //
    25  // Two arguments:
    26  //
    27  //	go doc <pkg> <sym>[.<methodOrField>]
    28  //
    29  // Show the documentation for the package, symbol, and method or field. The
    30  // first argument must be a full package path. This is similar to the
    31  // command-line usage for the godoc command.
    32  //
    33  // For commands, unless the -cmd flag is present "go doc command"
    34  // shows only the package-level docs for the package.
    35  //
    36  // The -src flag causes doc to print the full source code for the symbol, such
    37  // as the body of a struct, function or method.
    38  //
    39  // The -all flag causes doc to print all documentation for the package and
    40  // all its visible symbols. The argument must identify a package.
    41  //
    42  // For complete documentation, run "go help doc".
    43  package main
    44  
    45  import (
    46  	"bytes"
    47  	"flag"
    48  	"fmt"
    49  	"go/build"
    50  	"go/token"
    51  	"io"
    52  	"log"
    53  	"os"
    54  	"path"
    55  	"path/filepath"
    56  	"strings"
    57  )
    58  
    59  var (
    60  	unexported bool   // -u flag
    61  	matchCase  bool   // -c flag
    62  	chdir      string // -C flag
    63  	showAll    bool   // -all flag
    64  	showCmd    bool   // -cmd flag
    65  	showSrc    bool   // -src flag
    66  	short      bool   // -short flag
    67  )
    68  
    69  // usage is a replacement usage function for the flags package.
    70  func usage() {
    71  	fmt.Fprintf(os.Stderr, "Usage of [go] doc:\n")
    72  	fmt.Fprintf(os.Stderr, "\tgo doc\n")
    73  	fmt.Fprintf(os.Stderr, "\tgo doc <pkg>\n")
    74  	fmt.Fprintf(os.Stderr, "\tgo doc <sym>[.<methodOrField>]\n")
    75  	fmt.Fprintf(os.Stderr, "\tgo doc [<pkg>.]<sym>[.<methodOrField>]\n")
    76  	fmt.Fprintf(os.Stderr, "\tgo doc [<pkg>.][<sym>.]<methodOrField>\n")
    77  	fmt.Fprintf(os.Stderr, "\tgo doc <pkg> <sym>[.<methodOrField>]\n")
    78  	fmt.Fprintf(os.Stderr, "For more information run\n")
    79  	fmt.Fprintf(os.Stderr, "\tgo help doc\n\n")
    80  	fmt.Fprintf(os.Stderr, "Flags:\n")
    81  	flag.PrintDefaults()
    82  	os.Exit(2)
    83  }
    84  
    85  func main() {
    86  	log.SetFlags(0)
    87  	log.SetPrefix("doc: ")
    88  	dirsInit()
    89  	err := do(os.Stdout, flag.CommandLine, os.Args[1:])
    90  	if err != nil {
    91  		log.Fatal(err)
    92  	}
    93  }
    94  
    95  // do is the workhorse, broken out of main to make testing easier.
    96  func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
    97  	flagSet.Usage = usage
    98  	unexported = false
    99  	matchCase = false
   100  	flagSet.StringVar(&chdir, "C", "", "change to `dir` before running command")
   101  	flagSet.BoolVar(&unexported, "u", false, "show unexported symbols as well as exported")
   102  	flagSet.BoolVar(&matchCase, "c", false, "symbol matching honors case (paths not affected)")
   103  	flagSet.BoolVar(&showAll, "all", false, "show all documentation for package")
   104  	flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command")
   105  	flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol")
   106  	flagSet.BoolVar(&short, "short", false, "one-line representation for each symbol")
   107  	flagSet.Parse(args)
   108  	if chdir != "" {
   109  		if err := os.Chdir(chdir); err != nil {
   110  			return err
   111  		}
   112  	}
   113  	var paths []string
   114  	var symbol, method string
   115  	// Loop until something is printed.
   116  	dirs.Reset()
   117  	for i := 0; ; i++ {
   118  		buildPackage, userPath, sym, more := parseArgs(flagSet.Args())
   119  		if i > 0 && !more { // Ignore the "more" bit on the first iteration.
   120  			return failMessage(paths, symbol, method)
   121  		}
   122  		if buildPackage == nil {
   123  			return fmt.Errorf("no such package: %s", userPath)
   124  		}
   125  
   126  		// The builtin package needs special treatment: its symbols are lower
   127  		// case but we want to see them, always.
   128  		if buildPackage.ImportPath == "builtin" {
   129  			unexported = true
   130  		}
   131  
   132  		symbol, method = parseSymbol(sym)
   133  		pkg := parsePackage(writer, buildPackage, userPath)
   134  		paths = append(paths, pkg.prettyPath())
   135  
   136  		defer func() {
   137  			pkg.flush()
   138  			e := recover()
   139  			if e == nil {
   140  				return
   141  			}
   142  			pkgError, ok := e.(PackageError)
   143  			if ok {
   144  				err = pkgError
   145  				return
   146  			}
   147  			panic(e)
   148  		}()
   149  
   150  		switch {
   151  		case symbol == "":
   152  			pkg.packageDoc() // The package exists, so we got some output.
   153  			return
   154  		case method == "":
   155  			if pkg.symbolDoc(symbol) {
   156  				return
   157  			}
   158  		case pkg.printMethodDoc(symbol, method):
   159  			return
   160  		case pkg.printFieldDoc(symbol, method):
   161  			return
   162  		}
   163  	}
   164  }
   165  
   166  // failMessage creates a nicely formatted error message when there is no result to show.
   167  func failMessage(paths []string, symbol, method string) error {
   168  	var b bytes.Buffer
   169  	if len(paths) > 1 {
   170  		b.WriteString("s")
   171  	}
   172  	b.WriteString(" ")
   173  	for i, path := range paths {
   174  		if i > 0 {
   175  			b.WriteString(", ")
   176  		}
   177  		b.WriteString(path)
   178  	}
   179  	if method == "" {
   180  		return fmt.Errorf("no symbol %s in package%s", symbol, &b)
   181  	}
   182  	return fmt.Errorf("no method or field %s.%s in package%s", symbol, method, &b)
   183  }
   184  
   185  // parseArgs analyzes the arguments (if any) and returns the package
   186  // it represents, the part of the argument the user used to identify
   187  // the path (or "" if it's the current package) and the symbol
   188  // (possibly with a .method) within that package.
   189  // parseSymbol is used to analyze the symbol itself.
   190  // The boolean final argument reports whether it is possible that
   191  // there may be more directories worth looking at. It will only
   192  // be true if the package path is a partial match for some directory
   193  // and there may be more matches. For example, if the argument
   194  // is rand.Float64, we must scan both crypto/rand and math/rand
   195  // to find the symbol, and the first call will return crypto/rand, true.
   196  func parseArgs(args []string) (pkg *build.Package, path, symbol string, more bool) {
   197  	wd, err := os.Getwd()
   198  	if err != nil {
   199  		log.Fatal(err)
   200  	}
   201  	if len(args) == 0 {
   202  		// Easy: current directory.
   203  		return importDir(wd), "", "", false
   204  	}
   205  	arg := args[0]
   206  	// We have an argument. If it is a directory name beginning with . or ..,
   207  	// use the absolute path name. This discriminates "./errors" from "errors"
   208  	// if the current directory contains a non-standard errors package.
   209  	if isDotSlash(arg) {
   210  		arg = filepath.Join(wd, arg)
   211  	}
   212  	switch len(args) {
   213  	default:
   214  		usage()
   215  	case 1:
   216  		// Done below.
   217  	case 2:
   218  		// Package must be findable and importable.
   219  		pkg, err := build.Import(args[0], wd, build.ImportComment)
   220  		if err == nil {
   221  			return pkg, args[0], args[1], false
   222  		}
   223  		for {
   224  			packagePath, ok := findNextPackage(arg)
   225  			if !ok {
   226  				break
   227  			}
   228  			if pkg, err := build.ImportDir(packagePath, build.ImportComment); err == nil {
   229  				return pkg, arg, args[1], true
   230  			}
   231  		}
   232  		return nil, args[0], args[1], false
   233  	}
   234  	// Usual case: one argument.
   235  	// If it contains slashes, it begins with either a package path
   236  	// or an absolute directory.
   237  	// First, is it a complete package path as it is? If so, we are done.
   238  	// This avoids confusion over package paths that have other
   239  	// package paths as their prefix.
   240  	var importErr error
   241  	if filepath.IsAbs(arg) {
   242  		pkg, importErr = build.ImportDir(arg, build.ImportComment)
   243  		if importErr == nil {
   244  			return pkg, arg, "", false
   245  		}
   246  	} else {
   247  		pkg, importErr = build.Import(arg, wd, build.ImportComment)
   248  		if importErr == nil {
   249  			return pkg, arg, "", false
   250  		}
   251  	}
   252  	// Another disambiguator: If the argument starts with an upper
   253  	// case letter, it can only be a symbol in the current directory.
   254  	// Kills the problem caused by case-insensitive file systems
   255  	// matching an upper case name as a package name.
   256  	if !strings.ContainsAny(arg, `/\`) && token.IsExported(arg) {
   257  		pkg, err := build.ImportDir(".", build.ImportComment)
   258  		if err == nil {
   259  			return pkg, "", arg, false
   260  		}
   261  	}
   262  	// If it has a slash, it must be a package path but there is a symbol.
   263  	// It's the last package path we care about.
   264  	slash := strings.LastIndex(arg, "/")
   265  	// There may be periods in the package path before or after the slash
   266  	// and between a symbol and method.
   267  	// Split the string at various periods to see what we find.
   268  	// In general there may be ambiguities but this should almost always
   269  	// work.
   270  	var period int
   271  	// slash+1: if there's no slash, the value is -1 and start is 0; otherwise
   272  	// start is the byte after the slash.
   273  	for start := slash + 1; start < len(arg); start = period + 1 {
   274  		period = strings.Index(arg[start:], ".")
   275  		symbol := ""
   276  		if period < 0 {
   277  			period = len(arg)
   278  		} else {
   279  			period += start
   280  			symbol = arg[period+1:]
   281  		}
   282  		// Have we identified a package already?
   283  		pkg, err := build.Import(arg[0:period], wd, build.ImportComment)
   284  		if err == nil {
   285  			return pkg, arg[0:period], symbol, false
   286  		}
   287  		// See if we have the basename or tail of a package, as in json for encoding/json
   288  		// or ivy/value for robpike.io/ivy/value.
   289  		pkgName := arg[:period]
   290  		for {
   291  			path, ok := findNextPackage(pkgName)
   292  			if !ok {
   293  				break
   294  			}
   295  			if pkg, err = build.ImportDir(path, build.ImportComment); err == nil {
   296  				return pkg, arg[0:period], symbol, true
   297  			}
   298  		}
   299  		dirs.Reset() // Next iteration of for loop must scan all the directories again.
   300  	}
   301  	// If it has a slash, we've failed.
   302  	if slash >= 0 {
   303  		// build.Import should always include the path in its error message,
   304  		// and we should avoid repeating it. Unfortunately, build.Import doesn't
   305  		// return a structured error. That can't easily be fixed, since it
   306  		// invokes 'go list' and returns the error text from the loaded package.
   307  		// TODO(golang.org/issue/34750): load using golang.org/x/tools/go/packages
   308  		// instead of go/build.
   309  		importErrStr := importErr.Error()
   310  		if strings.Contains(importErrStr, arg[:period]) {
   311  			log.Fatal(importErrStr)
   312  		} else {
   313  			log.Fatalf("no such package %s: %s", arg[:period], importErrStr)
   314  		}
   315  	}
   316  	// Guess it's a symbol in the current directory.
   317  	return importDir(wd), "", arg, false
   318  }
   319  
   320  // dotPaths lists all the dotted paths legal on Unix-like and
   321  // Windows-like file systems. We check them all, as the chance
   322  // of error is minute and even on Windows people will use ./
   323  // sometimes.
   324  var dotPaths = []string{
   325  	`./`,
   326  	`../`,
   327  	`.\`,
   328  	`..\`,
   329  }
   330  
   331  // isDotSlash reports whether the path begins with a reference
   332  // to the local . or .. directory.
   333  func isDotSlash(arg string) bool {
   334  	if arg == "." || arg == ".." {
   335  		return true
   336  	}
   337  	for _, dotPath := range dotPaths {
   338  		if strings.HasPrefix(arg, dotPath) {
   339  			return true
   340  		}
   341  	}
   342  	return false
   343  }
   344  
   345  // importDir is just an error-catching wrapper for build.ImportDir.
   346  func importDir(dir string) *build.Package {
   347  	pkg, err := build.ImportDir(dir, build.ImportComment)
   348  	if err != nil {
   349  		log.Fatal(err)
   350  	}
   351  	return pkg
   352  }
   353  
   354  // parseSymbol breaks str apart into a symbol and method.
   355  // Both may be missing or the method may be missing.
   356  // If present, each must be a valid Go identifier.
   357  func parseSymbol(str string) (symbol, method string) {
   358  	if str == "" {
   359  		return
   360  	}
   361  	elem := strings.Split(str, ".")
   362  	switch len(elem) {
   363  	case 1:
   364  	case 2:
   365  		method = elem[1]
   366  	default:
   367  		log.Printf("too many periods in symbol specification")
   368  		usage()
   369  	}
   370  	symbol = elem[0]
   371  	return
   372  }
   373  
   374  // isExported reports whether the name is an exported identifier.
   375  // If the unexported flag (-u) is true, isExported returns true because
   376  // it means that we treat the name as if it is exported.
   377  func isExported(name string) bool {
   378  	return unexported || token.IsExported(name)
   379  }
   380  
   381  // findNextPackage returns the next full file name path that matches the
   382  // (perhaps partial) package path pkg. The boolean reports if any match was found.
   383  func findNextPackage(pkg string) (string, bool) {
   384  	if filepath.IsAbs(pkg) {
   385  		if dirs.offset == 0 {
   386  			dirs.offset = -1
   387  			return pkg, true
   388  		}
   389  		return "", false
   390  	}
   391  	if pkg == "" || token.IsExported(pkg) { // Upper case symbol cannot be a package name.
   392  		return "", false
   393  	}
   394  	pkg = path.Clean(pkg)
   395  	pkgSuffix := "/" + pkg
   396  	for {
   397  		d, ok := dirs.Next()
   398  		if !ok {
   399  			return "", false
   400  		}
   401  		if d.importPath == pkg || strings.HasSuffix(d.importPath, pkgSuffix) {
   402  			return d.dir, true
   403  		}
   404  	}
   405  }
   406  
   407  var buildCtx = build.Default
   408  
   409  // splitGopath splits $GOPATH into a list of roots.
   410  func splitGopath() []string {
   411  	return filepath.SplitList(buildCtx.GOPATH)
   412  }
   413  

View as plain text