-
Notifications
You must be signed in to change notification settings - Fork 19
/
runstatus_incident_show.go
132 lines (109 loc) · 3.59 KB
/
runstatus_incident_show.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
package cmd
import (
"bytes"
"fmt"
"os"
"strings"
"time"
"github.com/exoscale/cli/table"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type runstatusIncidentEventShowOutput struct {
Date *time.Time `json:"date"`
Status string `json:"status"`
Text string `json:"text"`
}
type runstatusIncidentShowOutput struct {
ID int `json:"id"`
Title string `json:"title"`
StartDate *time.Time `json:"start_date"`
EndDate *time.Time `json:"end_date"`
State string `json:"state"`
Status string `json:"status"`
AffectedServices []string `json:"affected_services,omitempty"`
Events []runstatusIncidentEventShowOutput `json:"events,omitempty"`
}
func (o *runstatusIncidentShowOutput) toJSON() { outputJSON(o) }
func (o *runstatusIncidentShowOutput) toText() { outputText(o) }
func (o *runstatusIncidentShowOutput) toTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Incident"})
t.Append([]string{"ID", fmt.Sprint(o.ID)})
t.Append([]string{"Title", o.Title})
t.Append([]string{"State", o.State})
t.Append([]string{"Status", o.Status})
t.Append([]string{"Start Date", fmt.Sprint(o.StartDate)})
if o.EndDate != nil {
t.Append([]string{"End Date", fmt.Sprint(o.EndDate)})
}
t.Append([]string{"Affected Services", strings.Join(o.AffectedServices, "\n")})
if len(o.Events) > 0 {
buf := bytes.NewBuffer(nil)
et := table.NewEmbeddedTable(buf)
et.SetHeader([]string{" "})
for _, e := range o.Events {
et.Append([]string{fmt.Sprint(e.Date), e.Status, e.Text})
}
et.Render()
t.Append([]string{"Event Stream", buf.String()})
}
t.Render()
}
func init() {
runstatusIncidentCmd.AddCommand(&cobra.Command{
Use: "show [PAGE] INCIDENT-NAME|ID",
Short: "Show an incident details",
Long: fmt.Sprintf(`This command shows a runstat.us incident details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&runstatusIncidentShowOutput{}), ", ")),
Aliases: gShowAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
page := gCurrentAccount.DefaultRunstatusPage
incident := args[0]
if gCurrentAccount.DefaultRunstatusPage == "" && len(args) == 1 {
return fmt.Errorf("No default runstat.us page is set.\n"+
"Please specify a page in parameter or add it to your configuration file %s",
gConfigFilePath)
}
if len(args) > 1 {
page = args[0]
incident = args[1]
}
return output(showRunstatusIncident(page, incident))
},
})
}
func showRunstatusIncident(p, i string) (outputter, error) {
page, err := csRunstatus.GetRunstatusPage(gContext, egoscale.RunstatusPage{Subdomain: p})
if err != nil {
return nil, err
}
incident, err := getRunstatusIncidentByNameOrID(*page, i)
if err != nil {
return nil, err
}
out := runstatusIncidentShowOutput{
ID: incident.ID,
Title: incident.Title,
StartDate: incident.StartDate,
EndDate: incident.EndDate,
State: incident.State,
Status: incident.Status,
AffectedServices: incident.Services,
}
if n := len(incident.Events); n > 0 {
out.Events = make([]runstatusIncidentEventShowOutput, n)
for i, e := range incident.Events {
out.Events[i] = runstatusIncidentEventShowOutput{
Date: e.Created,
Status: e.Status,
Text: e.Text,
}
}
}
return &out, nil
}