-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
58 lines (45 loc) · 1015 Bytes
/
plan.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
package choose
import (
"fmt"
"strings"
"github.com/mmatur/dsp/docker"
survey "github.com/AlecAivazis/survey/v2"
)
type answersPlan struct {
Name string
}
func (a answersPlan) isExit() bool {
return a.Name == ExitLabel
}
func (a answersPlan) getPlanID() (string, error) {
parts := strings.SplitN(a.Name, ":", 2)
if len(parts) != 2 {
return ExitValue, fmt.Errorf("unable to get the product ID")
}
return strings.TrimSpace(parts[1]), nil
}
// Plan choose a plan in the list.
func Plan(plans []docker.Plan) (string, error) {
var surveyOpts []string
for _, p := range plans {
surveyOpts = append(surveyOpts, fmt.Sprintf("%s: %s", p.Name, p.ID))
}
var qs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Select{
Message: "Choose a plan",
Options: append(surveyOpts, ExitLabel),
},
},
}
answers := &answersPlan{}
err := survey.Ask(qs, answers)
if err != nil {
return "", err
}
if answers.isExit() {
return ExitValue, nil
}
return answers.getPlanID()
}