From 9a6ae37d4e8489946c162432b091610a0fb664af Mon Sep 17 00:00:00 2001 From: Larz Conwell Date: Thu, 11 Dec 2014 10:10:04 -0500 Subject: [PATCH] cmd/godoc: accept GOOS and GOARCH URL parameters in -http mode This allows users to change the GOOS/GOARCH without running their own godoc server (or restarting with appropriate env vars). Change-Id: I0b54ef1b2dd93cf2c965ca584d8df74119ed1be6 Reviewed-on: https://go-review.googlesource.com/1371 Reviewed-by: Andrew Gerrand --- cmd/godoc/doc.go | 5 +++++ godoc/pres.go | 4 ++-- godoc/server.go | 16 +++++++++++----- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/godoc/doc.go b/cmd/godoc/doc.go index 17cf23e5..275423a1 100644 --- a/cmd/godoc/doc.go +++ b/cmd/godoc/doc.go @@ -109,6 +109,11 @@ via regular expressions). The maximum number of full text search results shown can be set with the -maxresults flag; if set to 0, no full text results are shown, and only an identifier index but no full text search index is created. +By default, godoc uses the system's GOOS/GOARCH; in command-line mode you can +set the GOOS/GOARCH environment variables to get output for the system specified. +If -http was specified you can provide the URL parameters "GOOS" and "GOARCH" +to set the output on the web page. + The presentation mode of web pages served by godoc can be controlled with the "m" URL parameter; it accepts a comma-separated list of flag names as value: diff --git a/godoc/pres.go b/godoc/pres.go index 11e0dd3e..85511776 100644 --- a/godoc/pres.go +++ b/godoc/pres.go @@ -156,11 +156,11 @@ func (p *Presentation) CmdFSRoot() string { // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now, // but this doesn't feel right. func (p *Presentation) GetPkgPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo { - return p.pkgHandler.GetPageInfo(abspath, relpath, mode) + return p.pkgHandler.GetPageInfo(abspath, relpath, mode, "", "") } // TODO(bradfitz): move this to be a method on Corpus. Just moving code around for now, // but this doesn't feel right. func (p *Presentation) GetCmdPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo { - return p.cmdHandler.GetPageInfo(abspath, relpath, mode) + return p.cmdHandler.GetPageInfo(abspath, relpath, mode, "", "") } diff --git a/godoc/server.go b/godoc/server.go index 6906df8a..18f110a3 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -54,16 +54,15 @@ func (s *handlerServer) registerWithMux(mux *http.ServeMux) { // directories, PageInfo.Dirs is nil. If an error occurred, PageInfo.Err is // set to the respective error but the error is not logged. // -func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode) *PageInfo { +func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode, goos, goarch string) *PageInfo { info := &PageInfo{Dirname: abspath} // Restrict to the package files that would be used when building // the package on this system. This makes sure that if there are // separate implementations for, say, Windows vs Unix, we don't // jumble them all together. - // Note: Uses current binary's GOOS/GOARCH. - // To use different pair, such as if we allowed the user to choose, - // set ctxt.GOOS and ctxt.GOARCH before calling ctxt.ImportDir. + // Note: If goos/goarch aren't set, the current binary's GOOS/GOARCH + // are used. ctxt := build.Default ctxt.IsAbsPath = pathpkg.IsAbs ctxt.ReadDir = func(dir string) ([]os.FileInfo, error) { @@ -84,6 +83,13 @@ func (h *handlerServer) GetPageInfo(abspath, relpath string, mode PageInfoMode) return ioutil.NopCloser(bytes.NewReader(data)), nil } + if goos != "" { + ctxt.GOOS = goos + } + if goarch != "" { + ctxt.GOARCH = goarch + } + pkginfo, err := ctxt.ImportDir(abspath, 0) // continue if there are no Go source files; we still want the directory info if _, nogo := err.(*build.NoGoError); err != nil && !nogo { @@ -242,7 +248,7 @@ func (h *handlerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { if relpath == builtinPkgPath { mode = NoFiltering | NoTypeAssoc } - info := h.GetPageInfo(abspath, relpath, mode) + info := h.GetPageInfo(abspath, relpath, mode, r.FormValue("GOOS"), r.FormValue("GOARCH")) if info.Err != nil { log.Print(info.Err) h.p.ServeError(w, r, relpath, info.Err)