2009-10-30 11:58:53 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// godoc: Go Documentation Server
|
|
|
|
|
|
|
|
// Web server tree:
|
|
|
|
//
|
|
|
|
// http://godoc/ main landing page
|
|
|
|
// http://godoc/doc/ serve from $GOROOT/doc - spec, mem, tutorial, etc.
|
|
|
|
// http://godoc/src/ serve files from $GOROOT/src; .go gets pretty-printed
|
2010-03-29 19:06:53 -06:00
|
|
|
// http://godoc/cmd/ serve documentation about commands
|
2009-10-30 11:58:53 -06:00
|
|
|
// http://godoc/pkg/ serve documentation about packages
|
|
|
|
// (idea is if you say import "compress/zlib", you go to
|
|
|
|
// http://godoc/pkg/compress/zlib)
|
|
|
|
//
|
|
|
|
// Command-line interface:
|
|
|
|
//
|
|
|
|
// godoc packagepath [name ...]
|
|
|
|
//
|
|
|
|
// godoc compress/zlib
|
|
|
|
// - prints doc for package compress/zlib
|
|
|
|
// godoc crypto/block Cipher NewCMAC
|
|
|
|
// - prints doc for Cipher and NewCMAC in package crypto/block
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2011-07-14 12:34:53 -06:00
|
|
|
"archive/zip"
|
2009-12-15 16:33:31 -07:00
|
|
|
"bytes"
|
2011-11-01 20:06:05 -06:00
|
|
|
"errors"
|
2010-03-29 19:06:53 -06:00
|
|
|
_ "expvar" // to serve /debug/vars
|
2009-12-15 16:33:31 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2010-03-29 19:06:53 -06:00
|
|
|
"go/ast"
|
2011-07-01 22:07:06 -06:00
|
|
|
"go/build"
|
2009-12-15 16:33:31 -07:00
|
|
|
"io"
|
|
|
|
"log"
|
2011-11-08 16:43:02 -07:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof" // to serve /debug/pprof/*
|
2009-12-15 16:33:31 -07:00
|
|
|
"os"
|
2011-07-27 16:14:12 -06:00
|
|
|
"path"
|
2011-03-06 15:33:23 -07:00
|
|
|
"path/filepath"
|
2011-09-26 16:33:13 -06:00
|
|
|
"regexp"
|
2010-03-19 16:20:20 -06:00
|
|
|
"runtime"
|
2010-03-29 19:06:53 -06:00
|
|
|
"strings"
|
2009-12-15 16:33:31 -07:00
|
|
|
"time"
|
2009-10-30 11:58:53 -06:00
|
|
|
)
|
|
|
|
|
2010-03-24 15:51:55 -06:00
|
|
|
const defaultAddr = ":6060" // default webserver address
|
2010-03-19 13:46:43 -06:00
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
var (
|
2011-07-14 12:34:53 -06:00
|
|
|
// file system to serve
|
2011-07-19 09:22:20 -06:00
|
|
|
// (with e.g.: zip -r go.zip $GOROOT -i \*.go -i \*.html -i \*.css -i \*.js -i \*.txt -i \*.c -i \*.h -i \*.s -i \*.png -i \*.jpg -i \*.sh -i favicon.ico)
|
2011-07-14 12:34:53 -06:00
|
|
|
zipfile = flag.String("zip", "", "zip file providing the file system to serve; disabled if empty")
|
|
|
|
|
2011-08-29 18:22:20 -06:00
|
|
|
// file-based index
|
|
|
|
writeIndex = flag.Bool("write_index", false, "write index to a file; the file name must be specified with -index_files")
|
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
// periodic sync
|
2010-03-16 17:45:54 -06:00
|
|
|
syncCmd = flag.String("sync", "", "sync command; disabled if empty")
|
|
|
|
syncMin = flag.Int("sync_minutes", 0, "sync interval in minutes; disabled if <= 0")
|
2010-09-14 12:16:36 -06:00
|
|
|
syncDelay delayTime // actual sync interval in minutes; usually syncDelay == syncMin, but syncDelay may back off exponentially
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
// network
|
|
|
|
httpAddr = flag.String("http", "", "HTTP service address (e.g., '"+defaultAddr+"')")
|
|
|
|
serverAddr = flag.String("server", "", "webserver address for command line searches")
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
// layout control
|
2010-03-29 19:06:53 -06:00
|
|
|
html = flag.Bool("html", false, "print HTML in command-line mode")
|
|
|
|
srcMode = flag.Bool("src", false, "print (exported) source in command-line mode")
|
2010-03-19 13:46:43 -06:00
|
|
|
|
|
|
|
// command-line searches
|
|
|
|
query = flag.Bool("q", false, "arguments are considered search queries")
|
2009-10-30 11:58:53 -06:00
|
|
|
)
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func serveError(w http.ResponseWriter, r *http.Request, relpath string, err error) {
|
2010-02-18 18:40:50 -07:00
|
|
|
contents := applyTemplate(errorHTML, "errorHTML", err) // err may contain an absolute path!
|
2010-09-28 22:30:12 -06:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
servePage(w, "File "+relpath, "", "", contents)
|
2010-02-18 18:40:50 -07:00
|
|
|
}
|
|
|
|
|
2010-09-28 22:30:12 -06:00
|
|
|
func exec(rw http.ResponseWriter, args []string) (status int) {
|
2009-12-15 16:33:31 -07:00
|
|
|
r, w, err := os.Pipe()
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2011-01-10 16:34:29 -07:00
|
|
|
log.Printf("os.Pipe(): %v", err)
|
2009-12-15 16:33:31 -07:00
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
bin := args[0]
|
|
|
|
fds := []*os.File{nil, w, w}
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Printf("executing %v", args)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2011-03-15 12:41:19 -06:00
|
|
|
p, err := os.StartProcess(bin, args, &os.ProcAttr{Files: fds, Dir: *goroot})
|
2009-12-15 16:33:31 -07:00
|
|
|
defer r.Close()
|
|
|
|
w.Close()
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2011-02-03 20:41:26 -07:00
|
|
|
log.Printf("os.StartProcess(%q): %v", bin, err)
|
2009-12-15 16:33:31 -07:00
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2011-02-03 20:41:26 -07:00
|
|
|
defer p.Release()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, r)
|
2012-02-19 21:36:08 -07:00
|
|
|
wait, err := p.Wait()
|
2009-10-30 11:58:53 -06:00
|
|
|
if err != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
2011-02-03 20:41:26 -07:00
|
|
|
log.Printf("os.Wait(%d, 0): %v", p.Pid, err)
|
2009-12-15 16:33:31 -07:00
|
|
|
return 2
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2012-02-20 20:10:34 -07:00
|
|
|
if !wait.Success() {
|
2009-12-15 16:33:31 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
2012-02-20 20:10:34 -07:00
|
|
|
log.Printf("executing %v failed", args)
|
|
|
|
status = 1 // See comment in default case in dosync.
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if *verbose {
|
2009-11-09 13:07:39 -07:00
|
|
|
os.Stderr.Write(buf.Bytes())
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2010-09-28 22:30:12 -06:00
|
|
|
if rw != nil {
|
2011-03-09 10:41:01 -07:00
|
|
|
rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
2010-09-28 22:30:12 -06:00
|
|
|
rw.Write(buf.Bytes())
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2010-09-28 22:30:12 -06:00
|
|
|
func dosync(w http.ResponseWriter, r *http.Request) {
|
2009-12-15 16:33:31 -07:00
|
|
|
args := []string{"/bin/sh", "-c", *syncCmd}
|
2010-09-28 22:30:12 -06:00
|
|
|
switch exec(w, args) {
|
2009-11-01 11:33:16 -07:00
|
|
|
case 0:
|
2009-11-02 10:25:39 -07:00
|
|
|
// sync succeeded and some files have changed;
|
2009-11-02 23:44:01 -07:00
|
|
|
// update package tree.
|
|
|
|
// TODO(gri): The directory tree may be temporarily out-of-sync.
|
|
|
|
// Consider keeping separate time stamps so the web-
|
|
|
|
// page can indicate this discrepancy.
|
2011-01-10 16:34:29 -07:00
|
|
|
initFSTree()
|
2009-12-15 16:33:31 -07:00
|
|
|
fallthrough
|
2009-11-01 11:33:16 -07:00
|
|
|
case 1:
|
2009-11-02 10:25:39 -07:00
|
|
|
// sync failed because no files changed;
|
|
|
|
// don't change the package tree
|
2011-11-30 10:01:46 -07:00
|
|
|
syncDelay.set(time.Duration(*syncMin) * time.Minute) // revert to regular sync schedule
|
2009-11-01 11:33:16 -07:00
|
|
|
default:
|
2012-02-20 20:10:34 -07:00
|
|
|
// TODO(r): this cannot happen now, since Wait has a boolean exit condition,
|
|
|
|
// not an integer.
|
2009-11-01 11:33:16 -07:00
|
|
|
// sync failed because of an error - back off exponentially, but try at least once a day
|
2011-11-30 10:01:46 -07:00
|
|
|
syncDelay.backoff(24 * time.Hour)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr,
|
2009-12-09 17:54:07 -07:00
|
|
|
"usage: godoc package [name ...]\n"+
|
2010-03-19 13:46:43 -06:00
|
|
|
" godoc -http="+defaultAddr+"\n")
|
2009-12-15 16:33:31 -07:00
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func loggingHandler(h http.Handler) http.Handler {
|
2010-09-28 22:30:12 -06:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2011-03-10 09:17:22 -07:00
|
|
|
log.Printf("%s\t%s", req.RemoteAddr, req.URL)
|
2010-09-28 22:30:12 -06:00
|
|
|
h.ServeHTTP(w, req)
|
2009-11-09 13:07:39 -07:00
|
|
|
})
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func remoteSearch(query string) (res *http.Response, err error) {
|
2010-03-24 15:51:55 -06:00
|
|
|
// list of addresses to try
|
|
|
|
var addrs []string
|
2010-03-19 13:46:43 -06:00
|
|
|
if *serverAddr != "" {
|
2010-03-24 15:51:55 -06:00
|
|
|
// explicit server address - only try this one
|
|
|
|
addrs = []string{*serverAddr}
|
2010-03-19 13:46:43 -06:00
|
|
|
} else {
|
2010-03-24 15:51:55 -06:00
|
|
|
addrs = []string{
|
|
|
|
defaultAddr,
|
|
|
|
"golang.org",
|
2010-03-19 13:46:43 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-24 15:51:55 -06:00
|
|
|
// remote search
|
2011-11-11 17:59:47 -07:00
|
|
|
search := remoteSearchURL(query, *html)
|
2010-03-24 15:51:55 -06:00
|
|
|
for _, addr := range addrs {
|
|
|
|
url := "http://" + addr + search
|
2011-05-13 08:31:24 -06:00
|
|
|
res, err = http.Get(url)
|
2010-03-24 15:51:55 -06:00
|
|
|
if err == nil && res.StatusCode == http.StatusOK {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && res.StatusCode != http.StatusOK {
|
2011-11-01 20:06:05 -06:00
|
|
|
err = errors.New(res.Status)
|
2010-03-19 13:46:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-03-29 19:06:53 -06:00
|
|
|
// Does s look like a regular expression?
|
|
|
|
func isRegexp(s string) bool {
|
|
|
|
return strings.IndexAny(s, ".(|)*+?^$[]") >= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a regular expression of the form
|
|
|
|
// names[0]|names[1]|...names[len(names)-1].
|
|
|
|
// Returns nil if the regular expression is illegal.
|
|
|
|
func makeRx(names []string) (rx *regexp.Regexp) {
|
|
|
|
if len(names) > 0 {
|
|
|
|
s := ""
|
|
|
|
for i, name := range names {
|
|
|
|
if i > 0 {
|
|
|
|
s += "|"
|
|
|
|
}
|
|
|
|
if isRegexp(name) {
|
|
|
|
s += name
|
|
|
|
} else {
|
|
|
|
s += "^" + name + "$" // must match exactly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rx, _ = regexp.Compile(s) // rx is nil if there's a compilation error
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2009-10-30 11:58:53 -06:00
|
|
|
func main() {
|
2009-12-15 16:33:31 -07:00
|
|
|
flag.Usage = usage
|
|
|
|
flag.Parse()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2011-08-29 18:22:20 -06:00
|
|
|
// Check usage: either server and no args, command line and args, or index creation mode
|
|
|
|
if (*httpAddr != "") != (flag.NArg() == 0) && !*writeIndex {
|
2011-07-19 09:22:20 -06:00
|
|
|
usage()
|
|
|
|
}
|
|
|
|
|
|
|
|
if *tabwidth < 0 {
|
|
|
|
log.Fatalf("negative tabwidth %d", *tabwidth)
|
|
|
|
}
|
|
|
|
|
2011-07-14 12:34:53 -06:00
|
|
|
// Determine file system to use.
|
2011-07-19 09:22:20 -06:00
|
|
|
// TODO(gri) - fs and fsHttp should really be the same. Try to unify.
|
|
|
|
// - fsHttp doesn't need to be set up in command-line mode,
|
|
|
|
// same is true for the http handlers in initHandlers.
|
|
|
|
if *zipfile == "" {
|
|
|
|
// use file system of underlying OS
|
2011-07-27 16:14:12 -06:00
|
|
|
*goroot = filepath.Clean(*goroot) // normalize path separator
|
2011-07-19 09:22:20 -06:00
|
|
|
fs = OS
|
|
|
|
fsHttp = http.Dir(*goroot)
|
|
|
|
} else {
|
2011-07-27 16:14:12 -06:00
|
|
|
// use file system specified via .zip file (path separator must be '/')
|
2011-07-14 12:34:53 -06:00
|
|
|
rc, err := zip.OpenReader(*zipfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("%s: %s\n", *zipfile, err)
|
|
|
|
}
|
2011-09-02 11:07:29 -06:00
|
|
|
defer rc.Close() // be nice (e.g., -writeIndex mode)
|
2011-07-27 16:14:12 -06:00
|
|
|
*goroot = path.Join("/", *goroot) // fsHttp paths are relative to '/'
|
2011-07-14 12:34:53 -06:00
|
|
|
fs = NewZipFS(rc)
|
2011-07-19 09:22:20 -06:00
|
|
|
fsHttp = NewHttpZipFS(rc, *goroot)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
readTemplates()
|
2011-08-20 13:39:38 -06:00
|
|
|
initHandlers()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2011-08-29 18:22:20 -06:00
|
|
|
if *writeIndex {
|
2011-09-02 11:07:29 -06:00
|
|
|
// Write search index and exit.
|
2011-08-29 18:22:20 -06:00
|
|
|
if *indexFiles == "" {
|
2011-09-02 11:07:29 -06:00
|
|
|
log.Fatal("no index file specified")
|
2011-08-29 18:22:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("initialize file systems")
|
|
|
|
*verbose = true // want to see what happens
|
|
|
|
initFSTree()
|
|
|
|
initDirTrees()
|
|
|
|
|
|
|
|
*indexThrottle = 1
|
|
|
|
updateIndex()
|
|
|
|
|
|
|
|
log.Println("writing index file", *indexFiles)
|
|
|
|
f, err := os.Create(*indexFiles)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
index, _ := searchIndex.get()
|
|
|
|
err = index.(*Index).Write(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-03-19 13:46:43 -06:00
|
|
|
if *httpAddr != "" {
|
2009-11-08 22:46:20 -07:00
|
|
|
// HTTP server mode.
|
2009-12-15 16:33:31 -07:00
|
|
|
var handler http.Handler = http.DefaultServeMux
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
2011-01-10 16:34:29 -07:00
|
|
|
log.Printf("Go Documentation Server")
|
|
|
|
log.Printf("version = %s", runtime.Version())
|
|
|
|
log.Printf("address = %s", *httpAddr)
|
|
|
|
log.Printf("goroot = %s", *goroot)
|
|
|
|
log.Printf("tabwidth = %d", *tabwidth)
|
2011-05-02 14:28:02 -06:00
|
|
|
switch {
|
|
|
|
case !*indexEnabled:
|
|
|
|
log.Print("search index disabled")
|
|
|
|
case *maxResults > 0:
|
|
|
|
log.Printf("full text index enabled (maxresults = %d)", *maxResults)
|
|
|
|
default:
|
|
|
|
log.Print("identifier search index enabled")
|
2011-01-10 16:34:29 -07:00
|
|
|
}
|
2010-02-16 12:20:55 -07:00
|
|
|
if !fsMap.IsEmpty() {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Print("user-defined mapping:")
|
2010-02-16 12:20:55 -07:00
|
|
|
fsMap.Fprint(os.Stderr)
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
handler = loggingHandler(handler)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
registerPublicHandlers(http.DefaultServeMux)
|
2009-10-30 11:58:53 -06:00
|
|
|
if *syncCmd != "" {
|
2009-11-09 13:07:39 -07:00
|
|
|
http.Handle("/debug/sync", http.HandlerFunc(dosync))
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2010-09-14 12:16:36 -06:00
|
|
|
// Initialize default directory tree with corresponding timestamp.
|
2010-11-18 20:55:38 -07:00
|
|
|
// (Do it in a goroutine so that launch is quick.)
|
2011-01-10 16:34:29 -07:00
|
|
|
go initFSTree()
|
2010-09-14 12:16:36 -06:00
|
|
|
|
|
|
|
// Initialize directory trees for user-defined file systems (-path flag).
|
|
|
|
initDirTrees()
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
// Start sync goroutine, if enabled.
|
|
|
|
if *syncCmd != "" && *syncMin > 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
syncDelay.set(*syncMin) // initial sync delay
|
2009-10-30 11:58:53 -06:00
|
|
|
go func() {
|
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
dosync(nil, nil)
|
|
|
|
delay, _ := syncDelay.get()
|
2011-11-30 10:01:46 -07:00
|
|
|
dt := delay.(time.Duration)
|
2009-10-30 11:58:53 -06:00
|
|
|
if *verbose {
|
2011-11-30 10:01:46 -07:00
|
|
|
log.Printf("next sync in %s", dt)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2011-11-30 10:01:46 -07:00
|
|
|
time.Sleep(dt)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
}()
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2012-01-24 17:56:31 -07:00
|
|
|
// Immediately update metadata.
|
|
|
|
updateMetadata()
|
2012-01-19 13:37:36 -07:00
|
|
|
// Periodically refresh metadata.
|
|
|
|
go refreshMetadataLoop()
|
|
|
|
|
2011-08-29 18:22:20 -06:00
|
|
|
// Initialize search index.
|
2011-05-02 14:28:02 -06:00
|
|
|
if *indexEnabled {
|
2011-09-02 11:07:29 -06:00
|
|
|
go indexer()
|
2011-05-02 14:28:02 -06:00
|
|
|
}
|
2009-10-30 11:58:53 -06:00
|
|
|
|
|
|
|
// Start http server.
|
2010-03-19 13:46:43 -06:00
|
|
|
if err := http.ListenAndServe(*httpAddr, handler); err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("ListenAndServe %s: %v", *httpAddr, err)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
2010-03-19 13:46:43 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2009-11-02 10:25:39 -07:00
|
|
|
// Command line mode.
|
2009-10-30 11:58:53 -06:00
|
|
|
if *html {
|
2009-12-01 10:15:05 -07:00
|
|
|
packageText = packageHTML
|
2010-03-19 13:46:43 -06:00
|
|
|
searchText = packageHTML
|
|
|
|
}
|
|
|
|
|
|
|
|
if *query {
|
|
|
|
// Command-line queries.
|
|
|
|
for i := 0; i < flag.NArg(); i++ {
|
2010-03-24 15:51:55 -06:00
|
|
|
res, err := remoteSearch(flag.Arg(i))
|
2010-03-19 13:46:43 -06:00
|
|
|
if err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("remoteSearch: %s", err)
|
2010-03-19 13:46:43 -06:00
|
|
|
}
|
2010-03-24 15:51:55 -06:00
|
|
|
io.Copy(os.Stdout, res.Body)
|
2010-03-19 13:46:43 -06:00
|
|
|
}
|
|
|
|
return
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2010-02-19 17:23:19 -07:00
|
|
|
// determine paths
|
2012-02-14 14:57:21 -07:00
|
|
|
const cmdPrefix = "cmd/"
|
2010-02-19 17:23:19 -07:00
|
|
|
path := flag.Arg(0)
|
2012-02-14 14:57:21 -07:00
|
|
|
var forceCmd bool
|
|
|
|
if strings.HasPrefix(path, ".") {
|
2010-02-19 17:23:19 -07:00
|
|
|
// assume cwd; don't assume -goroot
|
|
|
|
cwd, _ := os.Getwd() // ignore errors
|
2011-03-06 15:33:23 -07:00
|
|
|
path = filepath.Join(cwd, path)
|
2012-02-14 14:57:21 -07:00
|
|
|
} else if strings.HasPrefix(path, cmdPrefix) {
|
|
|
|
path = path[len(cmdPrefix):]
|
|
|
|
forceCmd = true
|
2010-02-19 17:23:19 -07:00
|
|
|
}
|
|
|
|
relpath := path
|
|
|
|
abspath := path
|
2011-07-01 22:07:06 -06:00
|
|
|
if t, pkg, err := build.FindTree(path); err == nil {
|
|
|
|
relpath = pkg
|
|
|
|
abspath = filepath.Join(t.SrcDir(), pkg)
|
|
|
|
} else if !filepath.IsAbs(path) {
|
2010-02-19 17:23:19 -07:00
|
|
|
abspath = absolutePath(path, pkgHandler.fsRoot)
|
|
|
|
} else {
|
2011-03-06 15:33:23 -07:00
|
|
|
relpath = relativeURL(path)
|
2010-02-19 17:23:19 -07:00
|
|
|
}
|
|
|
|
|
2010-03-29 19:06:53 -06:00
|
|
|
var mode PageInfoMode
|
2011-10-07 13:45:19 -06:00
|
|
|
if relpath == builtinPkgPath {
|
2012-02-14 14:57:21 -07:00
|
|
|
// the fake built-in package contains unexported identifiers
|
2011-10-07 13:45:19 -06:00
|
|
|
mode = noFiltering
|
|
|
|
}
|
2010-03-29 19:06:53 -06:00
|
|
|
if *srcMode {
|
|
|
|
// only filter exports if we don't have explicit command-line filter arguments
|
2011-10-06 17:06:23 -06:00
|
|
|
if flag.NArg() > 1 {
|
|
|
|
mode |= noFiltering
|
2010-03-29 19:06:53 -06:00
|
|
|
}
|
2011-10-06 17:06:23 -06:00
|
|
|
mode |= showSource
|
2010-03-29 19:06:53 -06:00
|
|
|
}
|
2010-03-12 19:16:21 -07:00
|
|
|
// TODO(gri): Provide a mechanism (flag?) to select a package
|
|
|
|
// if there are multiple packages in a directory.
|
2009-11-03 20:40:26 -07:00
|
|
|
|
2012-02-14 14:57:21 -07:00
|
|
|
// first, try as package unless forced as command
|
|
|
|
var info PageInfo
|
|
|
|
if !forceCmd {
|
|
|
|
info = pkgHandler.getPageInfo(abspath, relpath, "", mode)
|
|
|
|
}
|
|
|
|
|
2012-02-17 12:01:16 -07:00
|
|
|
// second, try as command unless the path is absolute
|
|
|
|
// (the go command invokes godoc w/ absolute paths; don't override)
|
|
|
|
var cinfo PageInfo
|
2012-02-14 14:57:21 -07:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
abspath = absolutePath(path, cmdHandler.fsRoot)
|
2012-02-17 12:01:16 -07:00
|
|
|
cinfo = cmdHandler.getPageInfo(abspath, relpath, "", mode)
|
2012-02-14 14:57:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// determine what to use
|
2011-02-14 18:41:47 -07:00
|
|
|
if info.IsEmpty() {
|
2012-02-14 14:57:21 -07:00
|
|
|
if !cinfo.IsEmpty() {
|
|
|
|
// only cinfo exists - switch to cinfo
|
|
|
|
info = cinfo
|
2010-02-19 17:23:19 -07:00
|
|
|
}
|
2012-02-14 14:57:21 -07:00
|
|
|
} else if !cinfo.IsEmpty() {
|
|
|
|
// both info and cinfo exist - use cinfo if info
|
|
|
|
// contains only subdirectory information
|
|
|
|
if info.PAst == nil && info.PDoc == nil {
|
|
|
|
info = cinfo
|
|
|
|
} else {
|
|
|
|
fmt.Printf("use 'godoc %s%s' for documentation on the %s command \n\n", cmdPrefix, relpath, relpath)
|
2011-02-14 18:41:47 -07:00
|
|
|
}
|
2009-11-03 20:40:26 -07:00
|
|
|
}
|
2012-02-14 14:57:21 -07:00
|
|
|
|
2010-08-10 10:52:02 -06:00
|
|
|
if info.Err != nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("%v", info.Err)
|
2010-08-10 10:52:02 -06:00
|
|
|
}
|
2009-10-30 11:58:53 -06:00
|
|
|
|
2010-03-29 19:06:53 -06:00
|
|
|
// If we have more than one argument, use the remaining arguments for filtering
|
|
|
|
if flag.NArg() > 1 {
|
|
|
|
args := flag.Args()[1:]
|
|
|
|
rx := makeRx(args)
|
|
|
|
if rx == nil {
|
2011-02-01 13:47:35 -07:00
|
|
|
log.Fatalf("illegal regular expression from %v", args)
|
2010-03-29 19:06:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
filter := func(s string) bool { return rx.MatchString(s) }
|
|
|
|
switch {
|
|
|
|
case info.PAst != nil:
|
|
|
|
ast.FilterFile(info.PAst, filter)
|
2010-03-30 12:19:58 -06:00
|
|
|
// Special case: Don't use templates for printing
|
|
|
|
// so we only get the filtered declarations without
|
|
|
|
// package clause or extra whitespace.
|
|
|
|
for i, d := range info.PAst.Decls {
|
|
|
|
if i > 0 {
|
|
|
|
fmt.Println()
|
|
|
|
}
|
2011-02-09 10:52:32 -07:00
|
|
|
if *html {
|
2011-08-12 11:06:32 -06:00
|
|
|
var buf bytes.Buffer
|
|
|
|
writeNode(&buf, info.FSet, d)
|
|
|
|
FormatText(os.Stdout, buf.Bytes(), -1, true, "", nil)
|
2011-02-09 10:52:32 -07:00
|
|
|
} else {
|
2011-08-12 11:06:32 -06:00
|
|
|
writeNode(os.Stdout, info.FSet, d)
|
2011-02-09 10:52:32 -07:00
|
|
|
}
|
2010-03-30 12:19:58 -06:00
|
|
|
fmt.Println()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
2010-03-29 19:06:53 -06:00
|
|
|
case info.PDoc != nil:
|
|
|
|
info.PDoc.Filter(filter)
|
|
|
|
}
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
|
2011-02-09 15:23:01 -07:00
|
|
|
if err := packageText.Execute(os.Stdout, info); err != nil {
|
log: new interface
New logging interface simplifies and generalizes.
1) Loggers now have only one output.
2) log.Stdout, Stderr, Crash and friends are gone.
Logging is now always to standard error by default.
3) log.Panic* replaces log.Crash*.
4) Exiting and panicking are not part of the logger's state; instead
the functions Exit* and Panic* simply call Exit or panic after
printing.
5) There is now one 'standard logger'. Instead of calling Stderr,
use Print etc. There are now triples, by analogy with fmt:
Print, Println, Printf
What was log.Stderr is now best represented by log.Println,
since there are now separate Print and Println functions
(and methods).
6) New functions SetOutput, SetFlags, and SetPrefix allow global
editing of the standard logger's properties. This is new
functionality. For instance, one can call
log.SetFlags(log.Lshortfile|log.Ltime|log.Lmicroseconds)
to get all logging output to show file name, line number, and
time stamp.
In short, for most purposes
log.Stderr -> log.Println or log.Print
log.Stderrf -> log.Printf
log.Crash -> log.Panicln or log.Panic
log.Crashf -> log.Panicf
log.Exit -> log.Exitln or log.Exit
log.Exitf -> log.Exitf (no change)
This has a slight breakage: since loggers now write only to one
output, existing calls to log.New() need to delete the second argument.
Also, custom loggers with exit or panic properties will need to be
reworked.
All package code updated to new interface.
The test has been reworked somewhat.
The old interface will be removed after the new release.
For now, its elements are marked 'deprecated' in their comments.
Fixes #1184.
R=rsc
CC=golang-dev
https://golang.org/cl/2419042
2010-10-12 13:59:18 -06:00
|
|
|
log.Printf("packageText.Execute: %s", err)
|
2009-10-30 11:58:53 -06:00
|
|
|
}
|
|
|
|
}
|