-
Notifications
You must be signed in to change notification settings - Fork 119
/
cliengine.go
175 lines (158 loc) · 4.9 KB
/
cliengine.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
/*
Copyright IBM Corporation 2020
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package qaengine
import (
"fmt"
"strconv"
"github.com/AlecAivazis/survey/v2"
log "github.com/sirupsen/logrus"
qatypes "github.com/konveyor/move2kube/types/qaengine"
)
// CliEngine handles the CLI based qa
type CliEngine struct {
}
// NewCliEngine creates a new instance of cli engine
func NewCliEngine() Engine {
ce := new(CliEngine)
return ce
}
// StartEngine starts the cli engine
func (c *CliEngine) StartEngine() error {
return nil
}
// FetchAnswer fetches the answer using cli
func (c *CliEngine) FetchAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
switch prob.Solution.Type {
case qatypes.SelectSolutionFormType:
return c.fetchSelectAnswer(prob)
case qatypes.MultiSelectSolutionFormType:
return c.fetchMultiSelectAnswer(prob)
case qatypes.ConfirmSolutionFormType:
return c.fetchConfirmAnswer(prob)
case qatypes.InputSolutionFormType:
return c.fetchInputAnswer(prob)
case qatypes.MultilineSolutionFormType:
return c.fetchMultilineAnswer(prob)
case qatypes.PasswordSolutionFormType:
return c.fetchPasswordAnswer(prob)
default:
log.Fatalf("Unknown type found: %s", prob.Solution.Type)
}
return prob, fmt.Errorf("Unknown type found : %s", prob.Solution.Type)
}
func (c *CliEngine) fetchSelectAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
var ans, d string
if len(prob.Solution.Default) > 0 {
d = prob.Solution.Default[0]
} else {
d = prob.Solution.Options[0]
}
prompt := &survey.Select{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
Options: prob.Solution.Options,
Default: d,
}
err = survey.AskOne(prompt, &ans)
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer([]string{ans})
return prob, err
}
func (c *CliEngine) fetchMultiSelectAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
ans := []string{}
prompt := &survey.MultiSelect{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
Options: prob.Solution.Options,
Default: prob.Solution.Default,
}
err = survey.AskOne(prompt, &ans, survey.WithIcons(func(icons *survey.IconSet) {
icons.MarkedOption.Text = "[\u2713]"
}))
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer(ans)
return prob, err
}
func (c *CliEngine) fetchConfirmAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
var ans, d bool
if len(prob.Solution.Default) > 0 {
d, err = strconv.ParseBool(prob.Solution.Default[0])
if err != nil {
log.Warnf("Unable to parse default value : %s", err)
}
}
prompt := &survey.Confirm{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
Default: d,
}
err = survey.AskOne(prompt, &ans)
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer([]string{fmt.Sprintf("%v", ans)})
return prob, err
}
func (c *CliEngine) fetchInputAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
var ans string
d := ""
if len(prob.Solution.Default) > 0 {
d = prob.Solution.Default[0]
}
prompt := &survey.Input{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
Default: d,
}
err = survey.AskOne(prompt, &ans)
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer([]string{ans})
return prob, err
}
func (c *CliEngine) fetchMultilineAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
var ans string
d := ""
if len(prob.Solution.Default) > 0 {
d = prob.Solution.Default[0]
}
prompt := &survey.Multiline{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
Default: d,
}
err = survey.AskOne(prompt, &ans)
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer([]string{ans})
return prob, err
}
func (c *CliEngine) fetchPasswordAnswer(prob qatypes.Problem) (answer qatypes.Problem, err error) {
var ans string
prompt := &survey.Password{
Message: fmt.Sprintf("%d. %s \nHints: \n %s\n", prob.ID, prob.Desc, prob.Context),
}
err = survey.AskOne(prompt, &ans)
if err != nil {
log.Fatalf("Error while asking a question : %s", err)
return prob, err
}
err = prob.SetAnswer([]string{ans})
return prob, err
}