-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (122 loc) · 3.33 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
package main
import (
"fmt"
"html/template"
"os"
"path"
"strings"
)
func fatal(format string, a ...string) {
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fmt.Fprintf(os.Stderr, format, a)
os.Exit(1)
}
func main() {
templatesDir := "templates"
templateFiles, err := os.ReadDir(templatesDir)
if err != nil {
fatal("Could not read templates dir: %s", err.Error())
}
templates := []Template{}
availableTemplates := [][]string{}
for _, t := range templateFiles {
if t.IsDir() {
continue
}
filePath := path.Join(templatesDir, t.Name())
contents, err := os.ReadFile(filePath)
if err != nil {
fatal("Could not read template %s: %s", filePath, err.Error())
}
id := strings.TrimSuffix(t.Name(), ".tmpl")
template := parseTemplate(id, string(contents))
templates = append(templates, template)
availableTemplates = append(availableTemplates, []string{template.Id, template.Name})
}
values := []Value{}
for _, t := range templates {
values = append(values, Value{
Template: t,
AvailableTemplates: availableTemplates,
})
}
outputDir := "public"
err = os.MkdirAll(outputDir, 0755)
if err != nil {
fatal("Could not create %s dir: %s", outputDir, err.Error())
}
indexTemplate := template.Must(template.ParseFiles("index.tmpl"))
indexPath := path.Join(outputDir, "index.html")
indexOutput, err := os.Create(indexPath)
if err != nil {
fatal("Could not create file %s: %s", indexPath, err.Error())
}
indexTemplate.Execute(indexOutput, availableTemplates)
templateTemplate := template.Must(template.ParseFiles("template.tmpl"))
for _, t := range values {
filePath := path.Join(outputDir, t.Id+".html")
output, err := os.Create(filePath)
if err != nil {
fatal("Could not create file %s: %s", filePath, err.Error())
}
templateTemplate.Execute(output, t)
// Store the template on its own for curling
standaloneFilePath := path.Join(outputDir, t.Id+".tmpl")
if err = os.WriteFile(standaloneFilePath, []byte(t.Text), 0755); err != nil {
fatal("Could not write to file %s: %s", standaloneFilePath, err.Error())
}
}
if err = copyFile("index.js", path.Join(outputDir, "index.js")); err != nil {
fatal("Failed to copy index.js: %s", err.Error())
}
if err = copyFile("favicon.ico", path.Join(outputDir, "favicon.ico")); err != nil {
fatal("Failed to copy favicon.ico: %s", err.Error())
}
}
type Template struct {
Id string
Name string
Inputs [][]string
Text string
}
type Value struct {
Template
AvailableTemplates [][]string
}
func parseTemplate(id, contents string) Template {
lines := strings.Split(contents, "\n")
t := Template{
Id: id,
Name: lines[0],
Inputs: [][]string{},
Text: "",
}
lines = lines[1:]
for i, line := range lines {
if line == "" {
lines = lines[i+1:]
break
}
split := strings.Split(line, "=")
key := strings.TrimSpace(split[0])
defaultVal := ""
if len(split) == 2 {
defaultVal = strings.TrimSpace(split[1])
}
t.Inputs = append(t.Inputs, []string{key, defaultVal})
}
t.Text = strings.Join(lines, "\n")
return t
}
func copyFile(from, to string) error {
contents, err := os.ReadFile(from)
if err != nil {
return fmt.Errorf("Could not read %s: %s", from, err.Error())
}
if err = os.WriteFile(to, contents, 0644); err != nil {
return fmt.Errorf("Could not write to file %s: %s", to, err.Error())
}
return nil
}