-
Notifications
You must be signed in to change notification settings - Fork 20
/
status.go
122 lines (102 loc) · 2.76 KB
/
status.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
package cmd
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/spf13/cobra"
"github.com/exoscale/cli/table"
)
const (
statusURL = "https://exoscalestatus.com"
jsonStatusURL = statusURL + "/api.json"
statusContentPage = "application/json"
twitterURL = "https://twitter.com/exoscalestatus"
)
// ServiceStatus represents the state of a service
type ServiceStatus struct {
State string `json:"state"`
}
// RunStatus represents a runstatus struct
type RunStatus struct {
URL string `json:"url"`
Incidents []struct {
Message string `json:"message"`
Status string `json:"status"`
Updated time.Time `json:"updated"`
Title string `json:"title"`
Created time.Time `json:"created"`
} `json:"incidents"`
UpcomingMaintenances []struct {
Description string `json:"description"`
Title string `json:"title"`
Date time.Time `json:"date"`
} `json:"upcoming_maintenances"`
Status map[string]ServiceStatus `json:"status"`
}
func init() {
RootCmd.AddCommand(&cobra.Command{
Use: "status",
Short: "Exoscale status",
RunE: func(cmd *cobra.Command, args []string) error {
return statusShow()
},
})
}
func statusShow() error {
status, err := fetchRunStatus(jsonStatusURL)
if err != nil {
return err
}
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Exoscale Status"})
buf := bytes.NewBuffer(nil)
st := table.NewEmbeddedTable(buf)
for service, status := range status.Status {
st.Append([]string{service, status.State})
}
st.Render()
t.Append([]string{"Services", buf.String()})
buf = bytes.NewBuffer([]byte("n/a"))
if len(status.Incidents) > 0 {
buf.Reset()
it := table.NewEmbeddedTable(buf)
for _, i := range status.Incidents {
it.Append([]string{i.Title, i.Status, fmt.Sprint(i.Created), fmt.Sprint(i.Updated)})
}
it.Render()
}
t.Append([]string{"Incidents", buf.String()})
buf = bytes.NewBuffer([]byte("n/a"))
if len(status.UpcomingMaintenances) > 0 {
buf.Reset()
mt := table.NewEmbeddedTable(buf)
for _, m := range status.UpcomingMaintenances {
mt.Append([]string{m.Title, m.Description, fmt.Sprint(m.Date)})
}
mt.Render()
}
t.Append([]string{"Maintenances", buf.String()})
t.Render()
fmt.Println("Updates available at", twitterURL)
return nil
}
func fetchRunStatus(url string) (*RunStatus, error) {
// XXX need gContext
r, err := http.Get(url)
if err != nil {
return nil, err
}
defer r.Body.Close()
contentType := r.Header.Get("content-type")
if contentType != statusContentPage {
return nil, fmt.Errorf("status page content type expected %q, but got %q", statusContentPage, contentType)
}
response := &RunStatus{}
if err := json.NewDecoder(r.Body).Decode(response); err != nil {
return nil, err
}
return response, nil
}