aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristine Dodrill <me@christine.website>2018-10-04 18:37:12 -0700
committerChristine Dodrill <me@christine.website>2018-10-04 18:37:12 -0700
commit1ea65cb9255af99adcf3cd8f55e6e2f336408a73 (patch)
tree4ecf120976ef7553b4c578fa43b073592fdd8926
parent7103fa3380d0038b4e1a452ef6aa8f3c3a9298ca (diff)
downloadx-1ea65cb9255af99adcf3cd8f55e6e2f336408a73.tar.xz
x-1ea65cb9255af99adcf3cd8f55e6e2f336408a73.zip
tools/ghstat: move github status library to a new-style Go library
-rw-r--r--tools/ghstat/main.go50
-rw-r--r--tools/ghstat/types.go60
-rw-r--r--web/ghstat/ghstat.go48
3 files changed, 78 insertions, 80 deletions
diff --git a/tools/ghstat/main.go b/tools/ghstat/main.go
index f86861e..91f260d 100644
--- a/tools/ghstat/main.go
+++ b/tools/ghstat/main.go
@@ -1,11 +1,14 @@
package main
import (
+ "encoding/json"
"flag"
"fmt"
"log"
+ "net/http"
"os"
- "time"
+
+ "github.com/Xe/x/web/ghstat"
)
var (
@@ -19,7 +22,15 @@ func main() {
flag.Parse()
if *messageFlag {
- m, err := getMessage()
+ req := ghstat.LastMessage()
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ var m ghstat.Message
+ defer resp.Body.Close()
+ err = json.NewDecoder(resp.Body).Decode(&m)
if err != nil {
log.Fatal(err)
}
@@ -27,28 +38,27 @@ func main() {
fmt.Printf("Last message:\n")
fmt.Printf("Status: %s\n", m.Status)
fmt.Printf("Message: %s\n", m.Body)
+ fmt.Printf("Time: %s\n", m.CreatedOn)
- t, err := time.Parse(time.RFC3339, m.CreatedOn)
- if err != nil {
- log.Fatal(err)
- }
+ return
+ }
- fmt.Printf("Time: %s\n", t.Format(time.ANSIC))
- } else {
- s, err := getStatus()
- if err != nil {
- log.Fatal(err)
- }
+ req := ghstat.LastStatus()
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ log.Fatal(err)
+ }
- t, err := time.Parse(time.RFC3339, s.LastUpdated)
- if err != nil {
- log.Fatal(err)
- }
+ var s ghstat.Status
+ defer resp.Body.Close()
+ err = json.NewDecoder(resp.Body).Decode(&s)
+ if err != nil {
+ log.Fatal(err)
+ }
- fmt.Printf("Status: %s (%s)\n", s.Status, t.Format(time.ANSIC))
+ fmt.Printf("Status: %s (%s)\n", s.Status, s.LastUpdated)
- if s.Status != "good" {
- os.Exit(1)
- }
+ if s.Status != "good" {
+ os.Exit(1)
}
}
diff --git a/tools/ghstat/types.go b/tools/ghstat/types.go
deleted file mode 100644
index ef824bd..0000000
--- a/tools/ghstat/types.go
+++ /dev/null
@@ -1,60 +0,0 @@
-package main
-
-import (
- "encoding/json"
- "io/ioutil"
- "net/http"
-)
-
-type Status struct {
- Status string `json:"status"`
- LastUpdated string `json:"last_updated"`
-}
-
-type Message struct {
- Status string `json:"status"`
- Body string `json:"body"`
- CreatedOn string `json:"created_on"`
-}
-
-func getMessage() (Message, error) {
- m := Message{}
-
- resp, err := http.Get("https://status.github.com/api/last-message.json")
- if err != nil {
- return Message{}, err
- }
- defer resp.Body.Close()
- content, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return Message{}, err
- }
-
- err = json.Unmarshal(content, &m)
- if err != nil {
- return Message{}, err
- }
-
- return m, nil
-}
-
-func getStatus() (Status, error) {
- s := Status{}
-
- resp, err := http.Get("https://status.github.com/api/status.json")
- if err != nil {
- return Status{}, err
- }
- defer resp.Body.Close()
- content, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return Status{}, err
- }
-
- err = json.Unmarshal(content, &s)
- if err != nil {
- return Status{}, err
- }
-
- return s, nil
-}
diff --git a/web/ghstat/ghstat.go b/web/ghstat/ghstat.go
new file mode 100644
index 0000000..8d94214
--- /dev/null
+++ b/web/ghstat/ghstat.go
@@ -0,0 +1,48 @@
+// Package ghstat is a set of wrappers for the GitHub Status API.
+package ghstat
+
+import "net/http"
+
+// Status is the human-readable status for GitHub's platform.
+type Status struct {
+ Status string `json:"status"`
+ LastUpdated string `json:"last_updated"`
+}
+
+// GitHub status API constants.
+const (
+ GHStatusAPIRoot = `https://status.github.com/api/`
+ StatusPath = `status.json`
+ MessagePath = `last-message.json`
+)
+
+// LastStatus returns a request to the most recent status for GitHub's platform.
+func LastStatus() *http.Request {
+ req, err := http.NewRequest(http.MethodGet, GHStatusAPIRoot+StatusPath, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ req.Header.Set("Accept", "application/json")
+
+ return req
+}
+
+// Message is an individiual human-readable message associated with a status update.
+type Message struct {
+ Status string `json:"status"`
+ Body string `json:"body"`
+ CreatedOn string `json:"created_on"`
+}
+
+// LastMessage returns a request to the most recent message for GitHub's platform.
+func LastMessage() *http.Request {
+ req, err := http.NewRequest(http.MethodGet, GHStatusAPIRoot+StatusPath, nil)
+ if err != nil {
+ panic(err)
+ }
+
+ req.Header.Set("Accept", "application/json")
+
+ return req
+}