forked from rancher/cli
/
questions.go
48 lines (40 loc) · 1.03 KB
/
questions.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
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/rancher/go-rancher/catalog"
)
func askQuestions(answers map[string]interface{}, templateVersion catalog.TemplateVersion) (map[string]interface{}, error) {
result := map[string]interface{}{}
for _, q := range templateVersion.Questions {
question := catalog.Question{}
err := mapstructure.Decode(q, &question)
if err != nil {
return nil, err
}
if answer, ok := answers[question.Variable]; ok {
result[question.Variable] = answer
} else {
result[question.Variable] = askQuestion(question)
}
}
return result, nil
}
func askQuestion(question catalog.Question) interface{} {
if len(question.Description) > 0 {
fmt.Println(question.Description)
}
fmt.Printf("%s %s[%s]: ", question.Label, question.Variable, question.Default)
answer, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return ""
}
answer = strings.TrimSpace(answer)
if answer == "" {
answer = question.Default
}
return answer
}