-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (52 loc) · 1.18 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
package main
import (
"fmt"
"html/template"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
if err := do(); err != nil {
log.Fatal(err)
}
}
func do() error {
return filepath.Walk(".", func(path string, info fs.FileInfo, err error) error {
if err != nil {
log.Printf("failed to traverse %s: %s", path, err)
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".tmpl") {
return nil
}
source, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("reading source: %w", err)
}
output := strings.TrimSuffix(path, ".tmpl")
tmp := template.New("hoge").Funcs(map[string]any{
"Foreground": Foreground,
"Background": Background,
"Emphasis": Emphasis,
})
tmp, err = tmp.Parse(string(source))
if err != nil {
return fmt.Errorf("parsing source file: %w", err)
}
writer, err := os.OpenFile(output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("opening output file: %w", err)
}
defer writer.Close()
if err := tmp.Execute(writer, Palette); err != nil {
return fmt.Errorf("executing template: %w", err)
}
log.Printf("generated %s from %s", output, path)
return nil
})
}