cmd/doc: show page for the requested object

This fixes a bug where we start pkgsite for every requested object,
rather than the one that we would have printed the documentation for.
To make things simple, we'll run the logic that prints the
documentation, but with an io.Discard writer. Then we can tell if the
documentation was found based on the return values of those functions.

For #68106

Change-Id: Ibf2ab1720f381d7214fc9239b9c2e915c91f7f7b
Reviewed-on: https://go-review.googlesource.com/c/go/+/674555
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Matloob <matloob@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Michael Matloob 2025-05-19 15:31:37 -04:00
parent 113b25774e
commit 1972493904
1 changed files with 16 additions and 7 deletions

View File

@ -126,6 +126,11 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
return err
}
}
if serveHTTP {
// We want to run the logic below to determine a match for a symbol, method,
// or field, but not actually print the documentation to the output.
writer = io.Discard
}
var paths []string
var symbol, method string
// Loop until something is printed.
@ -163,21 +168,25 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
panic(e)
}()
if serveHTTP {
return doPkgsite(pkg, symbol, method)
}
var found bool
switch {
case symbol == "":
pkg.packageDoc() // The package exists, so we got some output.
return
found = true
case method == "":
if pkg.symbolDoc(symbol) {
return
found = true
}
case pkg.printMethodDoc(symbol, method):
return
found = true
case pkg.printFieldDoc(symbol, method):
return
found = true
}
if found {
if serveHTTP {
return doPkgsite(pkg, symbol, method)
}
return nil
}
}
}