diff options
| author | Michael Matloob <matloob@golang.org> | 2025-05-21 15:19:16 -0400 |
|---|---|---|
| committer | Michael Matloob <matloob@google.com> | 2025-05-21 13:03:47 -0700 |
| commit | 4d6f49f6035b1f62c960f4ef66f41ef554de9243 (patch) | |
| tree | d8795b950e04df311afdd75a68fd2dc3e2d1b1b3 /src/cmd/doc | |
| parent | d54703c94ae906a5e851ae95909b6eb2f0314e19 (diff) | |
| download | go-4d6f49f6035b1f62c960f4ef66f41ef554de9243.tar.xz | |
cmd/doc: add more convenient behavior for go doc -http with no args
If we're in a module, go to the module's page. Outside of a module, but
in a workspace go to the home page, and outside of a module or
workspace, show the stdlib docs.
For #68106
Change-Id: I911a90a0e2b0a2bbb622f56e32827d5bdfa7f2fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/675235
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/cmd/doc')
| -rw-r--r-- | src/cmd/doc/main.go | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/cmd/doc/main.go b/src/cmd/doc/main.go index 03654e5824..ccd8512006 100644 --- a/src/cmd/doc/main.go +++ b/src/cmd/doc/main.go @@ -122,16 +122,23 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) { } } if serveHTTP { - // Special case: if there are no arguments to go doc -http, allow - // there to be no package in the current directory. We'll still try - // to open the page for the documentation of the package in the current - // directory, but if one doesn't exist, fall back to opening the home page. + // Special case: if there are no arguments, try to go to an appropriate page + // depending on whether we're in a module or workspace. The pkgsite homepage + // is often not the most useful page. if len(flagSet.Args()) == 0 { - var path string - if importPath, err := runCmd("go", "list"); err == nil { - path = importPath + mod, err := runCmd(append(os.Environ(), "GOWORK=off"), "go", "list", "-m") + if err == nil && mod != "" && mod != "command-line-arguments" { + // If there's a module, go to the module's doc page. + return doPkgsite(mod) } - return doPkgsite(path) + gowork, err := runCmd(nil, "go", "env", "GOWORK") + if err == nil && gowork != "" { + // Outside a module, but in a workspace, go to the home page + // with links to each of the modules' pages. + return doPkgsite("") + } + // Outside a module or workspace, go to the documentation for the standard library. + return doPkgsite("std") } // If args are provided, we need to figure out which page to open on the pkgsite @@ -203,9 +210,10 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) { } } -func runCmd(cmdline ...string) (string, error) { +func runCmd(env []string, cmdline ...string) (string, error) { var stdout, stderr strings.Builder cmd := exec.Command(cmdline[0], cmdline[1:]...) + cmd.Env = env cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { @@ -221,7 +229,7 @@ func objectPath(userPath string, pkg *Package, symbol, method string) (string, e // go/build couldn't determine the import path, probably // because this was a relative path into a module. Use // go list to get the import path. - path, err = runCmd("go", "list", userPath) + path, err = runCmd(nil, "go", "list", userPath) if err != nil { return "", err } |
