forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.go
181 lines (166 loc) · 3.92 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package silenced
import (
"fmt"
"io"
"strconv"
"github.com/AlecAivazis/survey"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/sensu/sensu-go/cli/commands/timeutil"
"github.com/sensu/sensu-go/types"
"github.com/spf13/pflag"
)
const (
expireDefault = "-1"
beginDefault = "0"
)
type silencedOpts struct {
Check string `survey:"check"`
Subscription string `survey:"subscription"`
Expire string `survey:"expire"`
ExpireOnResolve bool `survey:"expire_on_resolve"`
Creator string
Reason string `survey:"reason"`
Env string
Namespace string
Begin string `survey:"begin"`
}
func newSilencedOpts() *silencedOpts {
opts := silencedOpts{}
opts.Expire = expireDefault
opts.Begin = beginDefault
return &opts
}
func (o *silencedOpts) Apply(s *types.Silenced) (err error) {
s.Subscription = o.Subscription
s.Check = o.Check
s.Creator = o.Creator
s.Reason = o.Reason
s.Namespace = o.Namespace
s.ExpireOnResolve = o.ExpireOnResolve
s.Expire, err = strconv.ParseInt(o.Expire, 10, 64)
if err != nil {
return err
}
s.Begin, err = timeutil.ConvertToUnix(o.Begin)
return err
}
func (o *silencedOpts) withFlags(flags *pflag.FlagSet) {
o.Expire, _ = flags.GetString("expire")
o.ExpireOnResolve, _ = flags.GetBool("expire-on-resolve")
o.Reason, _ = flags.GetString("reason")
o.Subscription, _ = flags.GetString("subscription")
o.Check, _ = flags.GetString("check")
o.Begin, _ = flags.GetString("begin")
if namespace := helpers.GetChangedStringValueFlag("namespace", flags); namespace != "" {
o.Namespace = namespace
}
}
func (o *silencedOpts) administerQuestionnaire(editing bool) error {
var qs []*survey.Question
if !editing {
qs = []*survey.Question{
{
Name: "namespace",
Prompt: &survey.Input{
Message: "Namespace:",
Default: o.Namespace,
},
Validate: survey.Required,
},
{
Name: "subscription",
Prompt: &survey.Input{
Message: "Subscription:",
Default: o.Subscription,
Help: "One of subscription or check is required.",
},
},
{
Name: "check",
Prompt: &survey.Input{
Message: "Check:",
Default: o.Check,
Help: "One of subscription or check is required.",
},
},
}
}
qs = append(qs, []*survey.Question{
{
Name: "begin",
Prompt: &survey.Input{
Message: "Begin time:",
Default: "now",
Help: "Start silencing events at this time. Format: Jan 02 2006 3:04PM MST",
},
Validate: func(val interface{}) error {
_, err := timeutil.ConvertToUnix(val.(string))
return err
},
},
{
Name: "expire",
Prompt: &survey.Input{
Message: "Expiry in Seconds:",
Default: o.Expire,
},
},
{
Name: "expire_on_resolve",
Prompt: &survey.Confirm{
Message: "Expire on Resolve:",
Default: o.ExpireOnResolve,
Help: "Clear the silenced entry on resolution if true.",
},
},
{
Name: "reason",
Prompt: &survey.Input{
Message: "Reason:",
Default: o.Reason,
},
}}...)
if err := survey.Ask(qs, o); err != nil && err != io.EOF {
return err
}
return nil
}
type silencedName struct {
Subscription string
Check string
}
func askName(help string) (string, error) {
questions := []*survey.Question{
{
Name: "Subscription",
Prompt: &survey.Input{
Message: "Subscription:",
Help: help,
},
},
{
Name: "Check",
Prompt: &survey.Input{
Message: "Check:",
Help: help,
},
},
}
var name silencedName
if err := survey.Ask(questions, &name); err != nil {
return "", err
}
return types.SilencedName(name.Subscription, name.Check)
}
func toOpts(s *types.Silenced) *silencedOpts {
var o silencedOpts
o.Subscription = s.Subscription
o.Check = s.Check
o.Creator = s.Creator
o.Reason = s.Reason
o.Namespace = s.Namespace
o.ExpireOnResolve = s.ExpireOnResolve
o.Expire = fmt.Sprintf("%d", s.Expire)
o.Begin = fmt.Sprintf("%d", s.Begin)
return &o
}