-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
create.go
167 lines (138 loc) · 4.14 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
package actions
import (
"drawbridge/pkg/config"
"drawbridge/pkg/errors"
"drawbridge/pkg/utils"
"fmt"
"gopkg.in/yaml.v2"
"log"
"path"
"strconv"
)
type CreateAction struct {
Config config.Interface
}
func (e *CreateAction) Start(cliAnswerData map[string]interface{}) error {
// prepare answer data with config.options
answerData := map[string]interface{}{}
e.Config.UnmarshalKey("options", &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)
// ensuer 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
}
err = activeConfigTemplate.WriteTemplate(answerData)
if err != nil {
return err
}
// load up all active_extra_templates and attempt to merge answers with it.
activeExtraTemplates, err := e.Config.GetActiveExtraTemplates()
if err != nil {
return err
}
for _, template := range activeExtraTemplates {
err := template.WriteTemplate(answerData)
if err != nil {
return err
}
}
// write the answers.yaml file
answersFilePath := path.Join(e.Config.GetString("options.config_dir"), fmt.Sprintf(".%v.answers.yaml", path.Base(activeConfigTemplate.FilePath)))
answersFilePath, err = utils.PopulateTemplate(answersFilePath, answerData)
if err != nil {
return err
}
answersFileContent, err := yaml.Marshal(answerData)
if err != nil {
return err
}
err = utils.FileWrite(answersFilePath, string(answersFileContent), 0600)
if err != nil {
return err
}
return nil
}
func (e *CreateAction) Query(questions map[string]config.Question, answerData map[string]interface{}) (map[string]interface{}, error) {
for questionKey, questionData := range questions {
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
}
//TODO: figure out how to handle empty strings (still valid answer for some reason)
err = question.Validate(questionKey, answerTyped)
if err != nil {
fmt.Printf("%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))
}
}