-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
201 lines (167 loc) · 5.46 KB
/
create.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
package actions
import (
"fmt"
"github.com/analogj/drawbridge/pkg/config"
"github.com/analogj/drawbridge/pkg/errors"
"github.com/analogj/drawbridge/pkg/utils"
"github.com/fatih/color"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"path/filepath"
"sort"
"strconv"
)
type CreateAction struct {
Config config.Interface
}
func (e *CreateAction) Start(cliAnswerData map[string]interface{}, dryRun bool) error {
log.Debugf("Answer Data: %v", cliAnswerData)
// prepare answer data with config.options
answerData := map[string]interface{}{}
e.Config.UnmarshalKey("options", &answerData)
log.Debugf("Current Options: %v", answerData)
// add defaults into answerData
questions, err := e.Config.GetQuestions()
if err != nil {
return err
}
for questionKey, question := range questions {
if question.DefaultValue != nil {
answerData[questionKey] = question.DefaultValue
}
}
// merge cliAnswerData into answerData
for cliAnswerKey, cliAnswerValue := range cliAnswerData {
answerData[cliAnswerKey] = cliAnswerValue
}
//log.Printf("answers found before questioning: %v \n", answerData)
fmt.Println("\nCurrent Answers:")
questionKeys := utils.MapKeys(answerData)
for _, questionKey := range questionKeys {
if utils.SliceIncludes(e.Config.InternalQuestionKeys(), questionKey) {
log.Debugf("%v: %v\n",
questionKey,
color.GreenString(fmt.Sprintf("%v", answerData[questionKey])))
continue
}
fmt.Printf("%v: %v\n",
questionKey,
color.GreenString(fmt.Sprintf("%v", answerData[questionKey])))
}
// ensure that that all questions are answered, query user if missing anything.
answerData, err = e.Query(questions, answerData)
if err != nil {
return err
}
//set any optional keys to nil value.
for questionKey, question := range questions {
if !question.Required() {
if _, ok := answerData[questionKey]; !ok {
//answerdata does not contain this optional key
answerData[questionKey] = nil
}
}
}
// write the config template, make sure we "fix" the config filepath
activeConfigTemplate, err := e.Config.GetActiveConfigTemplate()
if err != nil {
return err
}
configTemplateData, err := activeConfigTemplate.WriteTemplate(answerData, e.Config.InternalQuestionKeys(), dryRun)
if err != nil {
return err
}
//make sure that we copy the config template data into the answerData object so it can be used by custom templates
//and is persisted in the answers.yaml file. Set it as key `config`
answerData["config"] = configTemplateData
// load up all active_custom_templates and attempt to merge answers with it.
activeCustomTemplates, err := e.Config.GetActiveCustomTemplates()
if err != nil {
return err
}
answerData["custom"] = []interface{}{}
for _, template := range activeCustomTemplates {
customTemplateData, err := template.WriteTemplate(answerData, dryRun)
if err != nil {
return err
}
answerData["custom"] = append(answerData["custom"].([]interface{}), customTemplateData)
}
// write the answers.yaml file
return e.WriteAnswersFile(filepath.Base(activeConfigTemplate.FilePath), answerData, dryRun)
}
func (e *CreateAction) WriteAnswersFile(baseName string, answerData map[string]interface{}, dryRun bool) error {
answersFilePath, err := utils.PopulatePathTemplate(filepath.Join(e.Config.GetString("options.config_dir"), fmt.Sprintf(".%v.answers.yaml", baseName)), answerData)
if err != nil {
return nil
}
answersFileContent, err := yaml.Marshal(answerData)
if err != nil {
return err
}
err = utils.FileWrite(answersFilePath, string(answersFileContent), 0640, dryRun)
if err != nil {
return err
}
return nil
}
func (e *CreateAction) Query(questions map[string]config.Question, answerData map[string]interface{}) (map[string]interface{}, error) {
questionKeys := []string{}
for k := range questions {
questionKeys = append(questionKeys, k)
}
sort.Strings(questionKeys)
for _, questionKey := range questionKeys {
questionData := questions[questionKey]
val, ok := questionData.Schema["required"]
required := ok && val.(bool)
if _, ok := answerData[questionKey]; !ok && required {
answerData[questionKey] = e.queryResponse(questionKey, questionData)
}
}
return answerData, nil
}
func (e *CreateAction) queryResponse(questionKey string, question config.Question) interface{} {
for true {
//this question is not answered, and it is required. We should ask the user.
answer := utils.StdinQuery(fmt.Sprintf("Please enter a value for `%s` [%s] - %s:", questionKey, question.GetType(), question.Description))
answerTyped, err := convertAnswerType(answer, question.GetType())
if err != nil {
fmt.Printf("%v\n", err)
continue
}
err = question.Validate(questionKey, answerTyped)
if err != nil {
color.HiRed("%v\n", err)
} else {
return answerTyped
}
}
//return answerTyped
return nil
}
func convertAnswerType(answer string, questionType string) (interface{}, error) {
if questionType == "integer" {
answer, err := strconv.ParseInt(answer, 10, 64)
if err != nil {
return nil, err
}
return answer, nil
} else if questionType == "number" {
answer, err := strconv.ParseFloat(answer, 64)
if err != nil {
return nil, err
}
return answer, nil
} else if questionType == "boolean" {
answer, err := strconv.ParseBool(answer)
if err != nil {
return nil, err
}
return answer, nil
} else if questionType == "string" {
return answer, nil
} else {
return nil, errors.AnswerFormatError(fmt.Sprintf("could not convert %v to unknown %v type", answer, questionType))
}
}