-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
108 lines (92 loc) · 2.73 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"
"time"
)
type result struct {
ServiceName string `json:"serviceName"`
Status string `json:"status"`
Reason string `json:"reason"`
}
const (
notaryServiceName = "Developer ID Notary Service"
)
func status(serviceName string) string {
if serviceName == "" {
serviceName = notaryServiceName
}
log.Printf("text: %s", serviceName)
resp, err := http.Get("https://www.apple.com/support/systemstatus/data/developer/system_status_en_US.js")
if err != nil && resp.StatusCode != http.StatusOK {
return formatResult(serviceName, "na", "failed to get status")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return formatResult(serviceName, "na", "failed to read status")
}
re := regexp.MustCompile(`\((.*?)\)`)
rs := re.FindStringSubmatch(string(body))
var response response
if err := json.Unmarshal([]byte(rs[1]), &response); err != nil {
return formatResult(serviceName, "na", "failed to parse status")
}
for _, s := range response.Services {
if strings.EqualFold(serviceName, s.ServiceName) {
if len(s.Events) == 0 {
return formatResult(serviceName, "up", "service is up")
}
firstEvent := s.Events[0]
startTime, err := time.Parse("01/02/2006 15:04 MST", firstEvent.StartDate)
if err != nil {
return formatResult(serviceName, "na",
fmt.Sprintf("failed to parse start time: %s", err.Error()))
}
endTime, err := time.Parse("01/02/2006 15:04 MST", firstEvent.EndDate)
if err != nil {
return formatResult(serviceName, "na",
fmt.Sprintf("failed to parse start time: %s", err.Error()))
}
now := time.Now()
log.Printf("start: %s", startTime)
log.Printf("end : %s", endTime)
log.Printf("now : %s", now)
if now.After(startTime) && now.Before(endTime) {
return formatResult(serviceName, s.Events[0].EventStatus, s.Events[0].Message)
}
return formatResult(serviceName, "up", "service is up")
}
}
return formatResult(serviceName, "na", "service not found")
}
type response struct {
DrPost bool `json:"drpost"`
DrMessage string `json:"drMessage"`
Services []service `json:"services"`
}
type service struct {
RedirectURL string `json:"redirectUrl"`
ServiceName string `json:"serviceName"`
Events []event `json:"events"`
}
type event struct {
EventStatus string `json:"eventStatus"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
Message string `json:"message"`
}
func formatResult(service, status, reason string) string {
r := result{
ServiceName: service,
Status: status,
Reason: reason,
}
b, _ := json.MarshalIndent(r, "", " ")
return fmt.Sprintf("%s\n", string(b))
}