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