-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (54 loc) · 1.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
package main
import (
_ "embed"
"errors"
"fmt"
"html/template"
"log"
"os"
"os/exec"
"time"
)
//go:embed go.mod.tmpl
var GO_MOD_TMPL string
//go:embed main.go.tmpl
var MAIN_TMPL string
//go:embed day.go.tmpl
var DAY_TMPL string
type goModConfig struct {
Module string
}
type mainConfig struct {
Year int
}
type dayConfig struct {
Day int
}
func main() {
if len(os.Args) != 2 {
log.Fatalf("It must be used like this: %s module-name", os.Args[0])
}
runTemplate("go.mod", GO_MOD_TMPL, goModConfig{os.Args[1]})
runTemplate("main.go", MAIN_TMPL, mainConfig{Year: time.Now().Year()})
for day := 1; day <= 25; day++ {
runTemplate(fmt.Sprintf("day%02d.go", day), DAY_TMPL, dayConfig{Day: day})
}
}
func runTemplate(filename string, tmplContent string, data any) {
_, err := os.Stat(filename)
if !errors.Is(err, os.ErrNotExist) {
log.Fatalf("File %s already exist, stopping here to avoid erasing anything.", filename)
}
file, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
tmpl, _ := template.New(filename).Parse(tmplContent)
_ = tmpl.Execute(file, data)
cmd := exec.Command("go", "mod", "tidy")
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
}