forked from izumin5210/grapi
/
generator.go
208 lines (168 loc) 路 4.46 KB
/
generator.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package gencmd
import (
"net/http"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/rakyll/statik/fs"
"github.com/spf13/afero"
"go.uber.org/zap"
"github.com/Ryo-not-rio/grapi/pkg/cli"
"github.com/izumin5210/clig/pkg/clib"
)
type Generator interface {
Generate(params interface{}) error
Destroy(params interface{}) error
}
func NewGenerator(
fs afero.Fs,
ui cli.UI,
outDir clib.Path,
templateFS http.FileSystem,
shouldRunFunc ShouldRunFunc,
) Generator {
return &generatorImpl{
fs: fs,
ui: ui,
outDir: outDir,
templateFS: templateFS,
shouldRunFunc: shouldRunFunc,
}
}
type generatorImpl struct {
fs afero.Fs
ui cli.UI
outDir clib.Path
templateFS http.FileSystem
shouldRunFunc ShouldRunFunc
}
func (g *generatorImpl) Generate(params interface{}) error {
entries, err := g.listEntries(params)
if err != nil {
return errors.WithStack(err)
}
for _, e := range entries {
if ok, err := g.shouldRun(e); err != nil {
g.ui.ItemFailure(e.Path[1:])
return errors.WithStack(err)
} else if !ok {
g.ui.ItemSkipped(e.Path[1:])
continue
}
err := g.writeFile(e)
if err != nil {
g.ui.ItemFailure(e.Path[1:])
return errors.WithStack(err)
}
g.ui.ItemSuccess(e.Path[1:])
}
return nil
}
func (g *generatorImpl) Destroy(params interface{}) error {
tmplPaths, err := g.listPathTemplates()
if err != nil {
return errors.WithStack(err)
}
for _, tmplPath := range tmplPaths {
path, err := TemplateString(strings.TrimSuffix(tmplPath, ".tmpl")).Compile(params)
if err != nil {
return errors.Wrapf(err, "failed to parse path: %s", tmplPath)
}
absPath := g.outDir.Join(path).String()
if ok, err := afero.Exists(g.fs, absPath); err != nil {
g.ui.ItemFailure(path)
return errors.WithStack(err)
} else if !ok {
g.ui.ItemSkipped(path)
continue
}
err = g.fs.Remove(absPath)
if err != nil {
g.ui.ItemFailure(path)
return errors.WithStack(err)
}
g.ui.ItemSuccess(path)
}
return nil
}
func (g *generatorImpl) listEntries(params interface{}) ([]*Entry, error) {
tmplPaths, err := g.listPathTemplates()
if err != nil {
return nil, errors.WithStack(err)
}
entries := make([]*Entry, 0, len(tmplPaths))
for _, tmplPath := range tmplPaths {
path, err := TemplateString(strings.TrimSuffix(tmplPath, ".tmpl")).Compile(params)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse path: %s", tmplPath)
}
data, err := fs.ReadFile(g.templateFS, tmplPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to read template: %s", tmplPath)
}
body, err := TemplateString(string(data)).Compile(params)
if err != nil {
return nil, errors.Wrapf(err, "failed to compile temlpate: %s, %v", tmplPath, params)
}
entries = append(entries, &Entry{File: File{Path: filepath.Clean(path), Body: body}, Template: File{Path: tmplPath, Body: string(data)}})
}
return entries, nil
}
func (g *generatorImpl) listPathTemplates() (tmplPaths []string, err error) {
err = fs.Walk(g.templateFS, "/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
if info.IsDir() {
return nil
}
tmplPaths = append(tmplPaths, path)
return nil
})
err = errors.WithStack(err)
return
}
func (g *generatorImpl) shouldRun(e *Entry) (bool, error) {
if g.shouldRunFunc != nil && !g.shouldRunFunc(e) {
return false, nil
}
absPath := g.outDir.Join(e.Path).String()
if ok, err := afero.Exists(g.fs, absPath); err != nil {
return false, errors.WithStack(err)
} else if !ok {
return true, nil
}
existed, err := afero.ReadFile(g.fs, absPath)
if err != nil {
return false, errors.WithStack(err)
}
if string(existed) == e.Body {
return false, nil
}
g.ui.ItemFailure(e.Path[1:] + " is conflicted.")
if ok, err := g.ui.Confirm("Overwite it?"); err != nil {
zap.L().Error("failed to confirm to apply", zap.Error(err))
return false, errors.WithStack(err)
} else if ok {
return true, nil
}
return false, nil
}
func (g *generatorImpl) writeFile(e *Entry) error {
path := g.outDir.Join(e.Path).String()
dir := filepath.Dir(path)
if ok, err := afero.DirExists(g.fs, dir); err != nil {
return errors.WithStack(err)
} else if !ok {
err := g.fs.MkdirAll(dir, 0755)
if err != nil {
return errors.Wrapf(err, "failed to create directory")
}
}
err := afero.WriteFile(g.fs, path, []byte(e.Body), 0644)
if err != nil {
return errors.Wrapf(err, "failed to write %s", e.Path)
}
return nil
}