go.starlark.net@v0.0.0-20231101134539-556fd59b42f6/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 "log" 18 "os" 19 "os/exec" 20 "path/filepath" 21 "strings" 22 ) 23 24 func main() { 25 log.SetFlags(0) 26 log.SetPrefix("update: ") 27 28 cwd, err := os.Getwd() 29 if err != nil { 30 log.Fatal(err) 31 } 32 if filepath.Base(cwd) != "go.starlark.net" { 33 log.Fatalf("must run from the go.starlark.net directory") 34 } 35 36 cmd := exec.Command("go", "list", "./...") 37 cmd.Stdout = new(bytes.Buffer) 38 cmd.Stderr = os.Stderr 39 if err := cmd.Run(); err != nil { 40 log.Fatal(err) 41 } 42 for _, pkg := range strings.Split(strings.TrimSpace(fmt.Sprint(cmd.Stdout)), "\n") { 43 rel := strings.TrimPrefix(pkg, "go.starlark.net/") // e.g. "cmd/starlark" 44 subdir := filepath.Join("docs", rel) 45 if err := os.MkdirAll(subdir, 0777); err != nil { 46 log.Fatal(err) 47 } 48 49 // Create missing docs/$rel/index.html files. 50 html := filepath.Join(subdir, "index.html") 51 if _, err := os.Stat(html); os.IsNotExist(err) { 52 data := strings.Replace(defaultHTML, "$PKG", pkg, -1) 53 if err := os.WriteFile(html, []byte(data), 0666); err != nil { 54 log.Fatal(err) 55 } 56 log.Printf("created %s", html) 57 } 58 } 59 } 60 61 const defaultHTML = `<html> 62 <head> 63 <meta name="go-import" content="go.starlark.net git https://github.com/google/starlark-go"></meta> 64 <meta http-equiv="refresh" content="0;URL='http://godoc.org/$PKG'" /></meta> 65 </head> 66 <body> 67 Redirecting to godoc.org page for $PKG... 68 </body> 69 </html> 70 `