-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsurvey.go
178 lines (157 loc) · 3.85 KB
/
survey.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
package main
import (
"errors"
"github.com/AlecAivazis/survey/v2"
"log"
"strconv"
"strings"
"time"
)
func askOneProjectId(projects []Project) (projectId string) {
projectNames := make([]string, len(projects))
for i, node := range projects {
projectNames[i] = node.Title
if node.Id == "" {
log.Print(`[Warning] This extension requires permission for the "project" scope. You may not currently have permission to retrieve project information, please check`)
}
}
qs := []*survey.Question{
{
Name: "ProjectId",
Prompt: &survey.Select{
Message: "Choose a Project",
Options: projectNames,
Description: func(value string, index int) string {
return projects[index].Type
},
},
},
}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
optionAnswer := answers["ProjectId"].(survey.OptionAnswer)
return projects[optionAnswer.Index].Id
}
func askOneContentType(itemTypes []string) string {
var selectedType string
typeQuestion := &survey.Select{
Message: "Choose a Item Type",
Options: itemTypes,
Default: itemTypes[0],
}
survey.AskOne(typeQuestion, &selectedType)
return selectedType
}
func askContentNumber(contentType string, contents []Content) string {
var numbers = make([]string, len(contents))
for i, c := range contents {
numbers[i] = strconv.Itoa(c.Number)
}
name := contentType + " Number"
qs := []*survey.Question{
{
Name: "number",
Prompt: &survey.Select{
Message: name,
Options: numbers,
Description: func(value string, index int) string {
return contents[index].Title
},
Filter: func(filterValue string, optValue string, optIndex int) bool {
return strings.Contains(contents[optIndex].Title, filterValue)
},
PageSize: 50,
},
},
}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
optionAnswer := answers["number"].(survey.OptionAnswer)
return optionAnswer.Value
}
func askTextFieldValue(fieldName string) string {
input := ""
prompt := &survey.Input{
Message: fieldName,
}
survey.AskOne(prompt, &input)
return input
}
func askDateFieldValue(fieldName string) string {
qs := []*survey.Question{
{
Name: fieldName,
Prompt: &survey.Input{Message: fieldName},
Validate: func(v interface{}) error {
strValue := v.(string)
// Allow Zero Value
if strValue == "" {
return nil
}
_, err := time.Parse("2006-01-02", strValue)
if err != nil {
return errors.New("Please format it like this '2006-01-02'.")
}
return nil
},
},
}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
return answers[fieldName].(string)
}
func askNumberFieldValue(fieldName string) float64 {
qs := []*survey.Question{
{
Name: fieldName,
Prompt: &survey.Input{Message: fieldName},
Validate: func(v interface{}) error {
strValue := v.(string)
_, err := strconv.ParseFloat(strValue, 64)
if err != nil {
return errors.New("Value is Int or Float.")
}
return nil
},
},
}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
f, _ := strconv.ParseFloat(answers[fieldName].(string), 64)
return f
}
func askOneSelectFieldValue(fieldName string, options []Option) string {
fieldOptionSize := len(options)
optionNames := make([]string, fieldOptionSize)
for i, opt := range options {
optionNames[i] = opt.Name
}
qs := []*survey.Question{
{
Name: fieldName,
Prompt: &survey.Select{
Message: fieldName,
Options: optionNames,
},
},
}
answers := map[string]interface{}{}
err := survey.Ask(qs, &answers)
if err != nil {
log.Fatal(err.Error())
}
optionAnswer := answers[fieldName].(survey.OptionAnswer)
return options[optionAnswer.Index].Id
}