-
Notifications
You must be signed in to change notification settings - Fork 20
/
runstatus_incident_add.go
199 lines (166 loc) · 5.01 KB
/
runstatus_incident_add.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/exoscale/egoscale"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
const (
runstatusFlagTitle = "title"
runstatusFlagDescription = "description"
runstatusFlagStatus = "status"
runstatusFlagState = "state"
runstatusFlagServices = "services"
)
// addCmd represents the add command
var runstatusIncidentAddCmd = &cobra.Command{
Use: "add [page name]",
Short: "Add an incident to a runstat.us page",
RunE: func(cmd *cobra.Command, args []string) error {
if gCurrentAccount.DefaultRunstatusPage == "" && len(args) == 0 {
fmt.Fprintf(os.Stderr, `Error: No default runstat.us page is set:
Please specify a page in parameter or add it to %q
`, gConfigFilePath)
return cmd.Usage()
}
if len(args) == 0 {
args = append(args, gCurrentAccount.DefaultRunstatusPage)
}
runstatusPage, err := csRunstatus.GetRunstatusPage(gContext, egoscale.RunstatusPage{Subdomain: args[0]})
if err != nil {
return err
}
reader := bufio.NewReader(os.Stdin)
title, err := cmd.Flags().GetString(runstatusFlagTitle)
if err != nil {
return nil
}
if title == "" {
title, err = readInput(reader, "Title of the incident", "none")
if err != nil {
return err
}
}
description, err := cmd.Flags().GetString(runstatusFlagDescription)
if err != nil {
return nil
}
if description == "" {
description, err = readInput(reader, "Description for the initial creation event", "none")
if err != nil {
return err
}
}
state, err := cmd.Flags().GetString(runstatusFlagState)
if err != nil {
return nil
}
if state == "" {
prompt := promptui.Select{
Label: "Services State",
Items: []string{"major_outage", "partial_outage", "degraded_performance", "operational"},
}
_, state, err = prompt.Run()
if err != nil {
return fmt.Errorf("prompt failed %v", err)
}
}
status, err := cmd.Flags().GetString(runstatusFlagStatus)
if err != nil {
return nil
}
if status == "" {
prompt := promptui.Select{
Label: "Event Status",
Items: []string{"investigating", "identified", "monitoring"},
}
_, status, err = prompt.Run()
if err != nil {
return fmt.Errorf("prompt failed %v", err)
}
}
services, err := cmd.Flags().GetStringSlice(runstatusFlagServices)
if err != nil {
return err
}
if len(services) == 0 {
services, err = promptGetService(*runstatusPage)
if err != nil {
return err
}
}
fmt.Println()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, "Title:\t%s\n", title) // nolint: errcheck
fmt.Fprintf(w, "Description:\t%s\n", description) // nolint: errcheck
fmt.Fprintf(w, "State:\t%s\n", state) // nolint: errcheck
fmt.Fprintf(w, "Status:\t%s\n", status) // nolint: errcheck
fmt.Fprintf(w, "Service(s):\t%s\n", strings.Join(services, ", ")) // nolint: errcheck
if err := w.Flush(); err != nil {
return err
}
if !askQuestion("sure you want to add this incident") {
return nil
}
incident, err := csRunstatus.CreateRunstatusIncident(gContext, egoscale.RunstatusIncident{
PageURL: runstatusPage.URL,
Services: services,
State: state,
Status: status,
StatusText: description,
Title: title,
})
if err != nil {
return err
}
fmt.Printf("Incident %q successfully created\n", incident.Title)
return nil
},
}
func promptGetService(page egoscale.RunstatusPage) ([]string, error) {
servs, err := csRunstatus.ListRunstatusServices(gContext, page)
if err != nil {
return nil, err
}
var services []string
tmp := make([]string, 0, len(servs))
for _, serv := range servs {
tmp = append(tmp, serv.Name)
}
if len(servs) > 0 {
tmp = append([]string{"[DONE] (Validate choice)"}, tmp...)
var index int
for {
prompt := promptui.Select{
Label: "Select some service",
Items: tmp,
}
var result string
index, result, err = prompt.Run()
if index != 0 {
services = append(services, result)
tmp = append(tmp[:index], tmp[index+1:]...)
continue
}
break
}
if err != nil {
return nil, fmt.Errorf("prompt failed %v", err)
}
return services, nil
}
return []string{}, nil
}
func init() {
runstatusIncidentCmd.AddCommand(runstatusIncidentAddCmd)
//required
runstatusIncidentAddCmd.Flags().StringSliceP(runstatusFlagServices, "", []string{}, "List of strings with the services impacted. e.g: <service1,service2,...>")
runstatusIncidentAddCmd.Flags().StringP(runstatusFlagTitle, "t", "", "Title of the incident")
runstatusIncidentAddCmd.Flags().StringP(runstatusFlagDescription, "d", "", "Description for the initial creation event")
runstatusIncidentAddCmd.Flags().StringP(runstatusFlagStatus, "", "", "<investigating | identified | monitoring>")
runstatusIncidentAddCmd.Flags().StringP(runstatusFlagState, "s", "", "<major_outage | partial_outage | degraded_performance | operational")
}