/
parlay_ui.go
98 lines (81 loc) · 2.17 KB
/
parlay_ui.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
package parlay
import (
"fmt"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/plunder-app/plunder/pkg/parlay/parlaytypes"
)
func contains(v string, a []string) bool {
for _, i := range a {
if strings.Contains(v, i) {
return true
}
}
return false
}
// StartUI will enable parlay to provide an easier way of selecting which operations will be performed
func StartUI(m *parlaytypes.TreasureMap) (*parlaytypes.TreasureMap, error) {
deployments := []string{}
for i := range m.Deployments {
deployments = append(deployments, m.Deployments[i].Name)
}
if len(deployments) == 0 {
return nil, fmt.Errorf("No Deployments were found")
}
var multiQs = []*survey.Question{
{
Name: "letter",
Prompt: &survey.MultiSelect{
Message: "Select deployment(s)",
Options: deployments,
},
},
}
deploymentAnswers := []string{}
// ask the question
err := survey.Ask(multiQs, &deploymentAnswers)
if err != nil {
return nil, err
}
// Create a new TreasureMap from the answered questions
newMap, err := m.FindDeployments(deploymentAnswers)
if err != nil {
return nil, err
}
for i := range newMap.Deployments {
// Ask for Hosts
multiQs[0].Prompt = &survey.MultiSelect{
Message: fmt.Sprintf("Select Hosts(s) for [%s]", newMap.Deployments[i].Name),
Options: newMap.Deployments[i].Hosts,
}
hostAnswers := []string{}
err := survey.Ask(multiQs, &hostAnswers)
if err != nil {
return nil, err
}
// Ask for Actions
actions := []string{}
for y := range newMap.Deployments[i].Actions {
actions = append(actions, m.Deployments[i].Actions[y].Name)
}
if len(actions) == 0 {
return nil, fmt.Errorf("No Deployments were found")
}
multiQs[0].Prompt = &survey.MultiSelect{
Message: fmt.Sprintf("Select Actions(s) for [%s]", newMap.Deployments[i].Name),
Options: actions,
}
deploymentAnswers := []string{}
err = survey.Ask(multiQs, &deploymentAnswers)
if err != nil {
return nil, err
}
newMap.Deployments[i].Hosts = hostAnswers
foundActions, err := newMap.Deployments[i].FindActions(deploymentAnswers)
if err != nil {
return nil, err
}
newMap.Deployments[i].Actions = foundActions
}
return newMap, nil
}