-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
53 lines (43 loc) · 859 Bytes
/
builder.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
package plan
import (
"fmt"
"os"
"path/filepath"
"text/template"
"time"
"github.com/Masterminds/sprig/v3"
)
type Builder struct {
plan Plan
}
func NewBuilder(plan Plan) *Builder {
return &Builder{
plan: plan,
}
}
// Build builds a plan
func (p *Builder) Build() error {
err := p.plan.Run()
if err != nil {
return err
}
return p.generate()
}
func (p *Builder) generate() error {
now := time.Now()
filePath := now.Format("plans/0601")
if _, err := os.Stat(filePath); os.IsNotExist(err) {
if err := os.MkdirAll(filePath, 0755); err != nil {
return err
}
}
f, err := os.Create(filepath.Join(filePath, fmt.Sprintf("%d.md", now.Day())))
if err != nil {
return err
}
t, err := template.New("plan.md").Funcs(sprig.TxtFuncMap()).ParseFiles("pkg/plan/plan.md")
if err != nil {
return err
}
return t.Execute(f, p.plan)
}