-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
94 lines (81 loc) · 2.26 KB
/
generate.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
package main
import (
"fmt"
"html/template"
"os"
"path/filepath"
)
type entry struct {
Name string
URL string
Domain string
GitHubOrgName string
MainBranch string
}
type index struct {
GithubOrgName string
}
var templateText = `<!DOCTYPE html>
<html lang="en">
<head>
<meta name="go-import"
content="{{ .Domain }}/{{ .Name }}
git {{ .URL }}" />
<meta name="go-source"
content="{{ .Domain }}/{{ .Name }}
{{ .URL }}
{{ .URL }}/tree/{{ .MainBranch }}{/dir}
{{ .URL }}/blob/{{ .MainBranch }}{/dir}/{file}#L{line}" />
<meta http-equiv="refresh" content="0; url={{ .URL }}">
</head></html>
`
var indexFileContents = `<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="refresh" content="0; url=https://github.com/{{ .GithubOrgName }}">
</head></html>
`
func generateModulePages(cfg *config, domainName, ghOrgName, ghPagesDir string) error {
t := template.Must(template.New("html").Parse(templateText))
for name, cfgEntry := range *cfg {
if cfgEntry.MainBranch == "" {
cfgEntry.MainBranch = "main"
}
e := entry{
Name: name,
URL: cfgEntry.URL,
Domain: domainName,
GitHubOrgName: ghOrgName,
MainBranch: cfgEntry.MainBranch,
}
dir := filepath.Join(ghPagesDir, name)
if err := os.MkdirAll(dir, 0o755); err != nil {
return fmt.Errorf("failed to create dir %s (%w)", dir, err)
}
file := filepath.Join(ghPagesDir, name, "index.html")
fh, err := os.Create(file)
if err != nil {
return fmt.Errorf("failed to open %s (%w)", file, err)
}
defer fh.Close()
if err := t.Execute(fh, e); err != nil {
return fmt.Errorf("failed to render template (%w)", err)
}
}
return nil
}
func generateIndexPage(ghOrgName, ghPagesDir string) error {
file := filepath.Join(ghPagesDir, "index.html")
t := template.Must(template.New("html").Parse(indexFileContents))
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("failed to open %s (%w)", file, err)
}
if err := t.Execute(f, index{ghOrgName}); err != nil {
return fmt.Errorf("failed to write index file %s (%w)", file, err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("failed to close %s", file)
}
return nil
}