1*4947cdc7SCole Faust//+build ignore 2*4947cdc7SCole Faust 3*4947cdc7SCole Faust// The update command creates/updates the <html><head> elements of 4*4947cdc7SCole Faust// each subpackage beneath docs so that "go get" requests redirect 5*4947cdc7SCole Faust// to GitHub and other HTTP requests redirect to godoc.corp. 6*4947cdc7SCole Faust// 7*4947cdc7SCole Faust// Usage: 8*4947cdc7SCole Faust// 9*4947cdc7SCole Faust// $ cd $GOPATH/src/go.starlark.net 10*4947cdc7SCole Faust// $ go run docs/update.go 11*4947cdc7SCole Faust// 12*4947cdc7SCole Faustpackage main 13*4947cdc7SCole Faust 14*4947cdc7SCole Faustimport ( 15*4947cdc7SCole Faust "bytes" 16*4947cdc7SCole Faust "fmt" 17*4947cdc7SCole Faust "io/ioutil" 18*4947cdc7SCole Faust "log" 19*4947cdc7SCole Faust "os" 20*4947cdc7SCole Faust "os/exec" 21*4947cdc7SCole Faust "path/filepath" 22*4947cdc7SCole Faust "strings" 23*4947cdc7SCole Faust) 24*4947cdc7SCole Faust 25*4947cdc7SCole Faustfunc main() { 26*4947cdc7SCole Faust log.SetFlags(0) 27*4947cdc7SCole Faust log.SetPrefix("update: ") 28*4947cdc7SCole Faust 29*4947cdc7SCole Faust cwd, err := os.Getwd() 30*4947cdc7SCole Faust if err != nil { 31*4947cdc7SCole Faust log.Fatal(err) 32*4947cdc7SCole Faust } 33*4947cdc7SCole Faust if filepath.Base(cwd) != "go.starlark.net" { 34*4947cdc7SCole Faust log.Fatalf("must run from the go.starlark.net directory") 35*4947cdc7SCole Faust } 36*4947cdc7SCole Faust 37*4947cdc7SCole Faust cmd := exec.Command("go", "list", "./...") 38*4947cdc7SCole Faust cmd.Stdout = new(bytes.Buffer) 39*4947cdc7SCole Faust cmd.Stderr = os.Stderr 40*4947cdc7SCole Faust if err := cmd.Run(); err != nil { 41*4947cdc7SCole Faust log.Fatal(err) 42*4947cdc7SCole Faust } 43*4947cdc7SCole Faust for _, pkg := range strings.Split(strings.TrimSpace(fmt.Sprint(cmd.Stdout)), "\n") { 44*4947cdc7SCole Faust rel := strings.TrimPrefix(pkg, "go.starlark.net/") // e.g. "cmd/starlark" 45*4947cdc7SCole Faust subdir := filepath.Join("docs", rel) 46*4947cdc7SCole Faust if err := os.MkdirAll(subdir, 0777); err != nil { 47*4947cdc7SCole Faust log.Fatal(err) 48*4947cdc7SCole Faust } 49*4947cdc7SCole Faust 50*4947cdc7SCole Faust // Create missing docs/$rel/index.html files. 51*4947cdc7SCole Faust html := filepath.Join(subdir, "index.html") 52*4947cdc7SCole Faust if _, err := os.Stat(html); os.IsNotExist(err) { 53*4947cdc7SCole Faust data := strings.Replace(defaultHTML, "$PKG", pkg, -1) 54*4947cdc7SCole Faust if err := ioutil.WriteFile(html, []byte(data), 0666); err != nil { 55*4947cdc7SCole Faust log.Fatal(err) 56*4947cdc7SCole Faust } 57*4947cdc7SCole Faust log.Printf("created %s", html) 58*4947cdc7SCole Faust } 59*4947cdc7SCole Faust } 60*4947cdc7SCole Faust} 61*4947cdc7SCole Faust 62*4947cdc7SCole Faustconst defaultHTML = `<html> 63*4947cdc7SCole Faust<head> 64*4947cdc7SCole Faust <meta name="go-import" content="go.starlark.net git https://github.com/google/starlark-go"></meta> 65*4947cdc7SCole Faust <meta http-equiv="refresh" content="0;URL='http://godoc.org/$PKG'" /></meta> 66*4947cdc7SCole Faust</head> 67*4947cdc7SCole Faust<body> 68*4947cdc7SCole Faust Redirecting to godoc.org page for $PKG... 69*4947cdc7SCole Faust</body> 70*4947cdc7SCole Faust</html> 71*4947cdc7SCole Faust` 72