forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
145 lines (123 loc) · 3.13 KB
/
Copy pathdoc.go
File metadata and controls
145 lines (123 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
//go:generate go run ./doc.go
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"sort"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
func clean(base string) error {
// Delete every Markdown file that we find, and track the directories that
// exist.
dirs := []string{}
if err := filepath.Walk(base, func(fp string, info fs.FileInfo, err error) error {
if info.IsDir() {
dirs = append(dirs, fp)
} else if path.Ext(fp) == ".md" {
return os.Remove(fp)
}
return nil
}); err != nil {
return errors.Wrap(err, "error walking Markdown files")
}
// Now iterate over the directories depth-first, removing the ones that are
// empty.
sort.Slice(dirs, func(i, j int) bool {
return len(dirs[j]) < len(dirs[i])
})
for _, dir := range dirs {
d, err := os.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
if len(d) == 0 {
if err := os.Remove(dir); err != nil {
return errors.Wrapf(err, "error removing directory %q", dir)
}
}
}
return nil
}
func get(url string, v any) error {
resp, err := http.Get(url)
if err != nil {
return errors.Wrapf(err, "http get: %s", url)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Wrapf(err, "http read: %s", url)
}
err = json.Unmarshal(b, v)
if err != nil {
return errors.Wrapf(err, "http json unmarshal: %s", url)
}
return nil
}
func build() error {
dir, err := os.MkdirTemp("", "src-cli-doc-gen")
if err != nil {
return errors.Wrap(err, "creating temporary directory")
}
defer os.RemoveAll(dir)
release := struct {
Name string
Assets []struct {
Name string
URL string `json:"browser_download_url"`
}
}{}
if err := get("https://api.github.com/repos/sourcegraph/src-cli/releases/latest", &release); err != nil {
return errors.Wrap(err, "src-cli release metadata")
}
bin := fmt.Sprintf("src_%s_%s", runtime.GOOS, runtime.GOARCH)
url := ""
for _, asset := range release.Assets {
if bin == asset.Name {
url = asset.URL
break
}
}
if url == "" {
return errors.Newf("failed to find %s for src-cli release %s", bin, release.Name)
}
// more succinct to use curl than pipe http.Get into file
src := filepath.Join(dir, bin)
srcGet := exec.Command("curl", "-L", "-o", src, url)
if _, err := srcGet.Output(); err != nil {
return errors.Wrap(err, "src-cli download")
}
if err := os.Chmod(src, 0700); err != nil {
return errors.Wrap(err, "src-cli mark executable")
}
srcDoc := exec.Command(src, "doc", "-o", ".")
srcDoc.Env = os.Environ()
// Always set this to 8 so the docs don't change when generated on
// different machines.
srcDoc.Env = append(srcDoc.Env, "GOMAXPROCS=8")
if out, err := srcDoc.CombinedOutput(); err != nil {
return errors.Wrapf(err, "running src doc:\n%s\n", string(out))
}
return nil
}
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("error getting working directory: %v", err)
}
if err := clean(wd); err != nil {
log.Fatalf("error cleaning working directory: %v", err)
}
if err := build(); err != nil {
log.Fatalf("error building documentation: %v", err)
}
}