-
Notifications
You must be signed in to change notification settings - Fork 17
/
prompt.go
101 lines (91 loc) · 2.4 KB
/
prompt.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
package utils
import (
"errors"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
)
// ==========
// Useful APIs of survey library
// ==========
// Used for (Yes/No) questions
func ConfirmFromUser(msg, helpText string) (bool, error) {
response := false
confirmPrompt := &survey.Confirm{
Renderer: survey.Renderer{},
Message: msg,
Default: true,
Help: helpText,
}
err := survey.AskOne(confirmPrompt, &response)
if err != nil {
return true, checkInterrupt(err)
}
return response, nil
}
// Used for Single Option Selection from Multiple Options
// Being used for selecting Java version for configuring meta of Java analyzer
// > * 1
// * 2
// * 3
func SelectFromOptions(msg, helpText string, opts []string) (string, error) {
var result string
prompt := &survey.Select{
Renderer: survey.Renderer{},
Message: msg,
Options: opts,
Default: nil,
Help: helpText,
}
err := survey.AskOne(prompt, &result)
if err != nil {
return "", checkInterrupt(err)
}
return result, nil
}
// Used for Single Line Text Input
// Being used for getting "Import root" of user for configuring meta of Go analyzer
func GetSingleLineInput(msg, helpText string) (string, error) {
response := ""
prompt := &survey.Input{
Renderer: survey.Renderer{},
Message: msg,
Default: "",
Help: helpText,
}
err := survey.AskOne(prompt, &response)
if err != nil {
return "", checkInterrupt(err)
}
return response, nil
}
// Used for multiple inputs from the displayed options
// Example:
// ? Which languages/tools does your project use?
// > [ ] Shell
// [ ] Rust
// [ ] Test Coverage
// [ ] Python
// [ ] Go
func SelectFromMultipleOptions(msg, helpText string, options []string) ([]string, error) {
response := make([]string, 0)
// Extracting languages and tools being used in the project for Analyzers
analyzerPrompt := &survey.MultiSelect{
Renderer: survey.Renderer{},
Message: msg,
Options: options,
Help: helpText,
}
err := survey.AskOne(analyzerPrompt, &response, survey.WithValidator(survey.Required))
if err != nil {
return nil, checkInterrupt(err)
}
return response, nil
}
// Utility to check for Ctrl+C interrupts
// Survey library doesn't exit on Ctrl+c interrupt. This handler helps in that.
func checkInterrupt(err error) error {
if err == terminal.InterruptErr {
return errors.New("Interrupt received. Exiting...")
}
return err
}