-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
217 lines (193 loc) · 7.19 KB
/
main.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v2"
)
const (
specsConfigFilename = "specs.yaml"
)
type (
specsConfig struct {
Specs []specsConfigSpec `yaml:"specs"`
}
specsConfigSpec struct {
Name string `yaml:"name"`
Remote string `yaml:"remote"`
Releases []specsConfigRelease `yaml:"releases"`
}
specsConfigRelease struct {
Commit string `yaml:"commit"`
Tag string `yaml:"tag"`
Branch string `yaml:"branch"`
}
)
func main() {
b, err := os.ReadFile(specsConfigFilename)
if err != nil {
log.Fatalf("ReadFile: %v", err)
}
config := specsConfig{}
if err := yaml.Unmarshal(b, &config); err != nil {
log.Fatalf("Unmarshal: %v", err)
}
processConfig(&config)
}
func absPath() string {
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("os.Getwd: %v", err)
}
currentAbsDir, err := filepath.Abs(currentDir)
if err != nil {
log.Fatalf("filepath.Abs: %v", err)
}
log.Printf("current directory: %s", currentAbsDir)
return currentAbsDir
}
func processConfig(config *specsConfig) {
currentDir := absPath()
gitWorkspace := filepath.Join(currentDir, "docs", "git-workspace")
log.Printf("ensuring git workspace directory %s", gitWorkspace)
os.MkdirAll(gitWorkspace, 0755)
s := `<html>
<head>
<title>OCI specs latest</title>
</head>
<body style="background:#e8e9ff;padding: 20px;font-family: monospace">
<div style="width:100%%px; max-width:700px;text-align:left;padding: 20px;border:1px solid #c7c2c2; background:white">
<h1>OCI specs latest</h1>
`
for _, spec := range config.Specs {
os.Chdir(gitWorkspace)
s += processSpec(&spec)
}
os.Chdir(currentDir)
now := time.Now().Format("Mon Jan 02 15:04:05 2006")
s += fmt.Sprintf(`<br/>
<i><p>generated on %s</p></i>
<i><p>generated by <a target="_blank" href="https://github.com/oci-playground/specs-latest">github.com/oci-playground/specs-latest</a></p></i>
</div>
</body>
</html>
`, now)
indexFile := filepath.Join(currentDir, "docs", "index.html")
if err := os.WriteFile(indexFile, []byte(s), 0644); err != nil {
log.Fatalf("WriteFile: %v", err)
}
}
func processSpec(spec *specsConfigSpec) string {
s := fmt.Sprintf("<hr/><h2><a target=\"_blank\" href=\"%s\">%s</a></h2>\n", spec.Remote, spec.Name)
currentDir := absPath()
log.Printf("[%s] begin processing, https git remote: %s", spec.Name, spec.Remote)
base := strings.TrimSuffix(filepath.Base(spec.Remote), ".git")
specWorkspace := filepath.Join(currentDir, base)
if _, err := os.Stat(specWorkspace); os.IsNotExist(err) {
cmd := exec.Command("git", "clone", "--depth=1000000", spec.Remote, specWorkspace)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatalf("cmd.Run: %v", err)
}
}
specOutputParentDir := filepath.Join(currentDir, "..", "specs", spec.Name)
os.MkdirAll(specOutputParentDir, 0755)
s += "<ul>"
for i := len(spec.Releases) - 1; i >= 0; i-- {
release := spec.Releases[i]
log.Printf("[%s] count:%d tag:%s branch:%s commit:%s", spec.Name, i+1, release.Commit, release.Tag, release.Branch)
checkoutTarget := release.Tag
if checkoutTarget == "" {
checkoutTarget = release.Commit
if checkoutTarget == "" {
log.Fatalf("[%s] invalid config for release at index %d", spec.Name, i)
}
}
s += fmt.Sprintf("<li><div><h3><a target=\"_blank\" href=\"%s/tree/%s\">%s</a></h3>\n", spec.Remote, checkoutTarget, checkoutTarget)
log.Printf("[%s] changing dir to %s", spec.Name, currentDir)
os.Chdir(specWorkspace)
log.Printf("[%s] running \"git checkout -f %s\"", spec.Name, checkoutTarget)
cmd := exec.Command("git", "checkout", "-f", checkoutTarget)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("cmd.Run: %v", err)
}
log.Printf("[%s] running \"git clean -f %s\"", spec.Name, checkoutTarget)
cmd = exec.Command("git", "clean", "-f", checkoutTarget)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("cmd.Run: %v", err)
}
// Get date
cmd = exec.Command("sh", "-c", "TZ=UTC0 git show --quiet --date=local --format=\"%cd\"")
out := new(bytes.Buffer)
cmd.Stdout = out
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("cmd.Run: %v", err)
}
s += fmt.Sprintf("<p><b>date:</b> %s</p>\n", out.String())
//s += "</div></li>\n"
// Get commit
cmd = exec.Command("sh", "-c", "git log --pretty=format:'%H' -n 1")
out = new(bytes.Buffer)
cmd.Stdout = out
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("cmd.Run: %v", err)
}
s += fmt.Sprintf("<p><b>commit:</b> <a target=\"_blank\" href=\"%s/commit/%s\">%s</a></p>\n", spec.Remote, out.String(), out.String())
specOutputDir := filepath.Join(specOutputParentDir, checkoutTarget)
if _, err := os.Stat(specOutputDir); !os.IsNotExist(err) {
log.Printf("[%s] output folder %s exists, skipping", spec.Name, specOutputDir)
} else {
log.Printf("[%s] running \"make docs\"", spec.Name)
// Ideally "make docs" should be all we need...
cmd = exec.Command("make", "docs")
// Various hacks for specific specs... DONT JUDGE ME
switch spec.Name {
case "image":
cmd = exec.Command("sh", "-c", fmt.Sprintf("cat Makefile | sed 's/-it/-i/g' | sed 's|-f gfm|-f gfm --metadata title=\"Open Container Initiative Image Format Specification\"|g' > Makefile.new && (make -f Makefile.new docs || (cd .tool/ && go get github.com/opencontainers/image-spec/specs-go@%s && cd ../ && make -f Makefile.new docs))", checkoutTarget))
case "distribution":
cmd = exec.Command("sh", "-c", fmt.Sprintf("cat Makefile | sed 's/-it/-i/g' | sed 's/go mod init \\&\\& \\\\/\\(rm -f go.* \\&\\& go mod init main \\)\\&\\& \\\\/g' | sed 's|-f gfm|-f gfm --metadata title=\"Open Container Initiative Distribution Specification\"|g'> Makefile.new && (make -f Makefile.new docs || (cd .tool/ && go get github.com/opencontainers/distribution-spec/specs-go@%s && cd ../ && make -f Makefile.new docs)) && rm -f output/.gitkeep", checkoutTarget))
case "runtime":
cmd = exec.Command("sh", "-c", fmt.Sprintf("cat Makefile | sed 's/-it/-i/g' | sed 's/go mod init \\\\/go mod init main \\\\/g' > Makefile.new && (make -f Makefile.new docs || (cd .tool/ && go get github.com/opencontainers/runtime-spec/specs-go@%s && cd ../ && make -f Makefile.new docs))", checkoutTarget))
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
os.Remove("Makefile.new")
if err != nil {
log.Fatalf("cmd.Run: %v", err)
}
// move the output contents
log.Printf("[%s] moving output/ to %s", spec.Name, specOutputDir)
if err := os.Rename("output/", specOutputDir); err != nil {
log.Fatalf("os.Rename: %v", err)
}
}
s += "<p><b>build assets:</b>\n"
entries, err := os.ReadDir(specOutputDir)
if err != nil {
log.Fatalf("os.ReadDir: %v", err)
}
for _, e := range entries {
s += fmt.Sprintf("<span>[<a target=\"_blank\" href=\"specs/%s/%s/%s\">%s</a>]</span> \n", spec.Name, checkoutTarget, filepath.Base(e.Name()), filepath.Base(e.Name()))
}
s += "</div></li>\n"
}
s += "</ul>"
log.Printf("[%s] changing dir to %s", spec.Name, currentDir)
os.Chdir(currentDir)
log.Printf("[%s] end processing", spec.Name)
return s
}