-
Notifications
You must be signed in to change notification settings - Fork 0
/
attic.go
83 lines (69 loc) · 1.66 KB
/
attic.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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"path"
)
type AtticConfig struct {
Layout string
Pages []string
InputDir string
OutputDir string
}
type AtticContext struct {
ActivePage string
}
func (ctx AtticContext) IsActive(page string) bool {
return ctx.ActivePage == page
}
var configFile = flag.String("config", "", "Configuration file.")
func main() {
flag.Parse()
if *configFile == "" {
fmt.Println("Config file must be defined.")
flag.Usage()
os.Exit(1)
}
configBlob, err := ioutil.ReadFile(*configFile)
if err != nil {
fmt.Printf("Could not read configuration file \"%s\"\n", *configFile)
os.Exit(1)
}
config := AtticConfig{}
err = json.Unmarshal(configBlob, &config)
if err != nil {
fmt.Printf("Configuration file is not valid JSON \"%s\"\n", *configFile)
fmt.Println(err)
os.Exit(1)
}
ctx := AtticContext{}
files := []string{path.Join(config.InputDir, config.Layout), ""}
for _, page := range config.Pages {
files[1] = path.Join(config.InputDir, page)
tmpl, err := template.ParseFiles(files...)
if err != nil {
fmt.Printf("Could not parse template \"%s\"\n", files[1])
fmt.Println(err)
os.Exit(1)
}
outfilePath := path.Join(config.OutputDir, page)
outfile, err := os.OpenFile(outfilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
defer outfile.Close()
if err != nil {
fmt.Printf("Could not open file \"%s\" for writing\n", outfilePath)
fmt.Println(err)
os.Exit(1)
}
ctx.ActivePage = page
err = tmpl.Execute(outfile, ctx)
if err != nil {
fmt.Printf("Could not execute template \"%s\"\n", files[1])
fmt.Println(err)
os.Exit(1)
}
}
}