/
interactive.go
123 lines (109 loc) · 2.91 KB
/
interactive.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
package mutator
import (
"strconv"
"strings"
"github.com/AlecAivazis/survey"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/types"
"github.com/spf13/pflag"
)
type mutatorOpts struct {
Name string `survey:"name"`
Command string `survey:"command"`
Timeout string `survey:"timeout"`
EnvVars string `survey:"env-vars"`
Namespace string
RuntimeAssets string `survey:"assets"`
}
func newMutatorOpts() *mutatorOpts {
opts := mutatorOpts{}
return &opts
}
func (opts *mutatorOpts) withMutator(mutator *types.Mutator) {
opts.Name = mutator.Name
opts.Namespace = mutator.Namespace
opts.Command = mutator.Command
opts.Timeout = strconv.FormatUint(uint64(mutator.Timeout), 10)
opts.EnvVars = strings.Join(mutator.EnvVars, ",")
opts.RuntimeAssets = strings.Join(mutator.RuntimeAssets, ",")
}
func (opts *mutatorOpts) withFlags(flags *pflag.FlagSet) {
opts.Command, _ = flags.GetString("command")
opts.Timeout, _ = flags.GetString("timeout")
opts.EnvVars, _ = flags.GetString("env-vars")
opts.RuntimeAssets, _ = flags.GetString("runtime-assets")
if namespace := helpers.GetChangedStringValueFlag("namespace", flags); namespace != "" {
opts.Namespace = namespace
}
}
func (opts *mutatorOpts) administerQuestionnaire(editing bool) error {
var qs []*survey.Question
if !editing {
qs = append(qs, []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{
Message: "Mutator Name:",
Default: opts.Name},
Validate: survey.Required,
},
{
Name: "namespace",
Prompt: &survey.Input{
Message: "Namespace:",
Default: opts.Namespace,
},
Validate: survey.Required,
},
}...)
}
qs = append(qs, []*survey.Question{
{Name: "command",
Prompt: &survey.Input{
Message: "Command:",
Default: opts.Command,
},
},
{
Name: "timeout",
Prompt: &survey.Input{
Message: "Timeout:",
Default: opts.Timeout,
},
},
{
Name: "env-vars",
Prompt: &survey.Input{
Message: "Environment variables:",
Help: "A list of comma-separated key=value pairs of environment variables.",
Default: opts.EnvVars,
},
},
{
Name: "assets",
Prompt: &survey.Input{
Message: "Runtime Assets:",
Help: "A list of comma-separated list of assets to use when executing the mutator",
Default: opts.RuntimeAssets,
},
},
}...)
return survey.Ask(qs, opts)
}
func (opts *mutatorOpts) Copy(mutator *types.Mutator) {
mutator.Name = opts.Name
mutator.Namespace = opts.Namespace
mutator.Command = opts.Command
mutator.EnvVars = helpers.SafeSplitCSV(opts.EnvVars)
if len(opts.Timeout) > 0 {
t, _ := strconv.ParseUint(opts.Timeout, 10, 32)
mutator.Timeout = uint32(t)
} else {
mutator.Timeout = 0
}
assets := helpers.SafeSplitCSV(opts.RuntimeAssets)
mutator.RuntimeAssets = make([]string, len(assets))
for i, h := range assets {
mutator.RuntimeAssets[i] = strings.TrimSpace(h)
}
}