-
Notifications
You must be signed in to change notification settings - Fork 20
/
status.go
104 lines (89 loc) · 2.4 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
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"os"
"text/tabwriter"
"time"
"github.com/spf13/cobra"
)
const (
statusURL = "https://exoscalestatus.com"
jsonStatusURL = statusURL + "/api.json"
statusContentPage = "application/json"
twitterURL = "https://twitter.com/exoscalestatus"
)
// statusCmd represents the status command
var statusCmd = &cobra.Command{
Use: "status",
Short: "Exoscale status",
RunE: func(cmd *cobra.Command, args []string) error {
status, err := fetchRunStatus(jsonStatusURL)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.FilterHTML)
fmt.Printf("Exoscale Status\n\t%s\n\n", statusURL)
for k, service := range status.Status {
fmt.Fprintf(w, "%s\t%s\n", k, service.State) // nolint: errcheck
}
fmt.Fprintln(w) // nolint: errcheck
w.Flush()
if len(status.Incidents) > 0 {
suffix := ""
if len(status.Incidents) > 1 {
suffix = "s"
}
msg := fmt.Sprintf("%d ongoing Incident%s (last: %s)",
len(status.Incidents),
suffix,
status.Incidents[0].Title)
fmt.Println(msg)
fmt.Printf("Updates available at %s\n", twitterURL)
return fmt.Errorf(msg)
}
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
}
// 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(statusCmd)
}