1package mutator
2
3import (
4	"strconv"
5	"strings"
6
7	"github.com/AlecAivazis/survey"
8	"github.com/sensu/sensu-go/cli/commands/helpers"
9	"github.com/sensu/sensu-go/types"
10	"github.com/spf13/pflag"
11)
12
13type mutatorOpts struct {
14	Name          string `survey:"name"`
15	Command       string `survey:"command"`
16	Timeout       string `survey:"timeout"`
17	EnvVars       string `survey:"env-vars"`
18	Namespace     string
19	RuntimeAssets string `survey:"assets"`
20}
21
22func newMutatorOpts() *mutatorOpts {
23	opts := mutatorOpts{}
24	return &opts
25}
26
27func (opts *mutatorOpts) withMutator(mutator *types.Mutator) {
28	opts.Name = mutator.Name
29	opts.Namespace = mutator.Namespace
30
31	opts.Command = mutator.Command
32	opts.Timeout = strconv.FormatUint(uint64(mutator.Timeout), 10)
33	opts.EnvVars = strings.Join(mutator.EnvVars, ",")
34	opts.RuntimeAssets = strings.Join(mutator.RuntimeAssets, ",")
35}
36
37func (opts *mutatorOpts) withFlags(flags *pflag.FlagSet) {
38	opts.Command, _ = flags.GetString("command")
39	opts.Timeout, _ = flags.GetString("timeout")
40	opts.EnvVars, _ = flags.GetString("env-vars")
41	opts.RuntimeAssets, _ = flags.GetString("runtime-assets")
42
43	if namespace := helpers.GetChangedStringValueFlag("namespace", flags); namespace != "" {
44		opts.Namespace = namespace
45	}
46}
47
48func (opts *mutatorOpts) administerQuestionnaire(editing bool) error {
49	var qs []*survey.Question
50	if !editing {
51		qs = append(qs, []*survey.Question{
52			{
53				Name: "name",
54				Prompt: &survey.Input{
55					Message: "Mutator Name:",
56					Default: opts.Name},
57				Validate: survey.Required,
58			},
59			{
60				Name: "namespace",
61				Prompt: &survey.Input{
62					Message: "Namespace:",
63					Default: opts.Namespace,
64				},
65				Validate: survey.Required,
66			},
67		}...)
68	}
69	qs = append(qs, []*survey.Question{
70		{Name: "command",
71			Prompt: &survey.Input{
72				Message: "Command:",
73				Default: opts.Command,
74			},
75		},
76		{
77			Name: "timeout",
78			Prompt: &survey.Input{
79				Message: "Timeout:",
80				Default: opts.Timeout,
81			},
82		},
83		{
84			Name: "env-vars",
85			Prompt: &survey.Input{
86				Message: "Environment variables:",
87				Help:    "A list of comma-separated key=value pairs of environment variables.",
88				Default: opts.EnvVars,
89			},
90		},
91		{
92			Name: "assets",
93			Prompt: &survey.Input{
94				Message: "Runtime Assets:",
95				Help:    "A list of comma-separated list of assets to use when executing the mutator",
96				Default: opts.RuntimeAssets,
97			},
98		},
99	}...)
100
101	return survey.Ask(qs, opts)
102}
103
104func (opts *mutatorOpts) Copy(mutator *types.Mutator) {
105	mutator.Name = opts.Name
106	mutator.Namespace = opts.Namespace
107
108	mutator.Command = opts.Command
109	mutator.EnvVars = helpers.SafeSplitCSV(opts.EnvVars)
110
111	if len(opts.Timeout) > 0 {
112		t, _ := strconv.ParseUint(opts.Timeout, 10, 32)
113		mutator.Timeout = uint32(t)
114	} else {
115		mutator.Timeout = 0
116	}
117
118	assets := helpers.SafeSplitCSV(opts.RuntimeAssets)
119	mutator.RuntimeAssets = make([]string, len(assets))
120	for i, h := range assets {
121		mutator.RuntimeAssets[i] = strings.TrimSpace(h)
122	}
123}
124