-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
258 lines (215 loc) · 4.91 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package generator
import (
"bytes"
"fmt"
"go/format"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"gopkg.in/yaml.v2"
)
type Survey struct {
SourceFile string
Name string `yaml:"name"`
PackageName string `yaml:"packageName"`
Questions []*Question `yaml:"questions"`
AllRequired bool `yaml:"allRequired"`
}
type Question struct {
Name string `yaml:"name"`
InputType string `yaml:"type"`
QuestionType string
AnswerType string `yaml:"answer"`
Help string `yaml:"help"`
Required bool `yaml:"required"`
Prompt string `yaml:"prompt"`
Default string `yaml:"default"`
Options []string `yaml:"options"`
}
var isSurveyTmpl = regexp.MustCompile(".*_survey.ya?ml$")
type Generator struct {
tpl *template.Template
}
func New() *Generator {
tpl := template.Must(template.New("survey.tmpl").
Funcs(sprig.GenericFuncMap()).
Parse(SurveyTmpl))
return &Generator{
tpl,
}
}
func (g *Generator) Generate(root string) error {
total := 0
err := filepath.Walk(root, func(srcFile string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if !isSurveyTmpl.MatchString(info.Name()) {
return nil
}
total += 1
filename := strings.Replace(path.Base(srcFile), "_survey.yaml", "", 1)
dirname := path.Dir(srcFile)
fmt.Printf("Found survey %s... \n", srcFile)
s, err := g.readSurvey(srcFile)
if err != nil {
return err
}
p := g.getPackageDir(dirname, s)
return g.writeSurvey(p, filename, s)
})
if err != nil {
return err
}
if total == 0 {
return fmt.Errorf("no survey found on '%s' to generate code", root)
}
return nil
}
func (g *Generator) getPackageDir(dir string, s *Survey) string {
if s.PackageName == "" {
s.PackageName = path.Base(dir)
return dir
}
pdir := path.Join(dir, s.PackageName)
os.MkdirAll(pdir, 0755)
return pdir
}
func (g *Generator) writeSurvey(dst, filename string, survey *Survey) error {
var buf bytes.Buffer
if err := g.tpl.Execute(&buf, survey); err != nil {
return err
}
// Pass gofmt to output
p, err := format.Source(buf.Bytes())
if err != nil {
return err
}
filename = path.Join(dst, fmt.Sprintf("%s_survey.go", path.Clean(filename)))
out, err := os.Create(filename)
if err != nil {
return err
}
_, err = out.Write(p)
if err != nil {
return err
}
return err
}
func (g *Generator) readSurvey(srcPath string) (*Survey, error) {
var surveyData *Survey
file, err := ioutil.ReadFile(srcPath)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(file, &surveyData)
if err != nil {
log.Fatal(err)
}
surveyData.SourceFile = srcPath
for _, q := range surveyData.Questions {
switch q.InputType {
case "confirm":
q.QuestionType = "bool"
case "select":
q.QuestionType = "string"
case "input":
q.QuestionType = "string"
case "password":
q.QuestionType = "string"
case "multiline":
q.QuestionType = "string"
case "multiselect":
q.QuestionType = "[]string"
default:
q.QuestionType = "string"
}
}
err = validateSurvey(surveyData)
if err != nil {
return nil, err
}
return surveyData, nil
}
func validateSurvey(data *Survey) error {
// TODO: validate survey content
return nil
}
/*
type QuestionType string
const (
Multiline = QuestionType("multiline")
Confirm = QuestionType("confirm")
Select = QuestionType("select")
Input = QuestionType("input")
)
func mainOld() {
var answers struct {
Q1 bool
Q2 bool
Q3 int
Option string
}
qs, err := GenerateQuestions("test.yaml")
err = survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err)
}
fmt.Println(answers)
}
func GenerateQuestions(ymlFile string) ([]*survey.Question, error) {
var yamlData Yaml
var qs []*survey.Question
file, err := ioutil.ReadFile(ymlFile)
if err != nil {
return nil, fmt.Errorf("error reading questions file: %v", err)
}
err = yaml.Unmarshal(file, &yamlData)
if err != nil {
return nil, fmt.Errorf("error parsing questions: %v", err)
}
for _, question := range yamlData.Questions {
var prompt survey.Prompt
name := question.Name
if name == "" {
return nil, fmt.Errorf("all questions should have valid name")
}
switch question.Type {
case Input:
prompt = &survey.Input{
Message: question.Prompt,
}
case Confirm:
prompt = &survey.Confirm{
Message: question.Prompt,
}
case Select:
prompt = &survey.Select{
Message: question.Prompt,
Options: question.Options,
}
case Multiline:
prompt = &survey.Multiline{
Message: question.Prompt,
}
default:
return nil, fmt.Errorf("unknown type: '%s' prompt: '%s", question.Type, question.Prompt)
}
surveyQuestion := &survey.Question{
Name: name,
Prompt: prompt,
}
if question.Required || yamlData.AllRequired {
surveyQuestion.Validate = survey.Required
}
qs = append(qs, surveyQuestion)
}
return qs, nil
}
*/