-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
choose.go
56 lines (47 loc) · 1 KB
/
choose.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
package choose
import (
"math"
"github.com/AlecAivazis/survey/v2"
"github.com/ldez/prm/v3/config"
)
const (
allLabel = "all"
// ExitLabel name.
ExitLabel = "exit"
// ExitValue representation.
ExitValue = 0
// AllValue representation.
AllValue = math.MaxInt32
)
type answersProject struct {
Directory string
}
func (a answersProject) isExit() bool {
return a.Directory == ExitLabel
}
// Project Choose a project in the list.
func Project(configs []config.Configuration) (*config.Configuration, error) {
var surveyOpts []string
for _, conf := range configs {
surveyOpts = append(surveyOpts, conf.Directory)
}
surveyOpts = append(surveyOpts, ExitLabel)
qs := []*survey.Question{
{
Name: "directory",
Prompt: &survey.Select{
Message: "Choose a directory",
Options: surveyOpts,
},
},
}
answers := &answersProject{}
err := survey.Ask(qs, answers)
if err != nil {
return nil, err
}
if answers.isExit() {
return nil, nil
}
return config.Find(configs, answers.Directory)
}