-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstatus.go
More file actions
116 lines (98 loc) · 3.12 KB
/
Copy pathstatus.go
File metadata and controls
116 lines (98 loc) · 3.12 KB
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
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"os"
"time"
"github.com/kernel/cli/pkg/util"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
type statusComponent struct {
Name string `json:"name"`
Status string `json:"status"`
}
type statusGroup struct {
Name string `json:"name"`
Status string `json:"status"`
Components []statusComponent `json:"components"`
}
type statusResponse struct {
Status string `json:"status"`
Groups []statusGroup `json:"groups"`
}
var statusCmd = &cobra.Command{
Use: "status",
Short: "Check the operational status of Kernel services",
RunE: runStatus,
}
func init() {
statusCmd.Flags().StringP("output", "o", "", "Output format (json)")
}
func runStatus(cmd *cobra.Command, args []string) error {
output, _ := cmd.Flags().GetString("output")
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(util.GetBaseURL() + "/status")
if err != nil {
pterm.Error.Println("Could not reach Kernel API. Check https://status.kernel.sh for updates.")
return nil
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
pterm.Error.Println("Could not reach Kernel API. Check https://status.kernel.sh for updates.")
return nil
}
var status statusResponse
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return fmt.Errorf("invalid response: %w", err)
}
if output == "json" {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(status)
}
printStatus(status)
return nil
}
// Colors match the dashboard's api-status-indicator.tsx
var statusDisplay = map[string]struct {
label string
rgb pterm.RGB
}{
"operational": {label: "Operational", rgb: pterm.NewRGB(31, 163, 130)},
"degraded_performance": {label: "Degraded Performance", rgb: pterm.NewRGB(245, 158, 11)},
"partial_outage": {label: "Partial Outage", rgb: pterm.NewRGB(242, 85, 51)},
"full_outage": {label: "Major Outage", rgb: pterm.NewRGB(239, 68, 68)},
"maintenance": {label: "Maintenance", rgb: pterm.NewRGB(36, 99, 235)},
"unknown": {label: "Unknown", rgb: pterm.NewRGB(128, 128, 128)},
}
func getStatusDisplay(status string) (string, pterm.RGB) {
if d, ok := statusDisplay[status]; ok {
return d.label, d.rgb
}
return "Unknown", pterm.NewRGB(128, 128, 128)
}
func coloredDot(rgb pterm.RGB) string {
return rgb.Sprint("●")
}
func printStatus(resp statusResponse) {
label, rgb := getStatusDisplay(resp.Status)
header := fmt.Sprintf("Kernel Status: %s", rgb.Sprint(label))
pterm.Println()
pterm.Println(" " + header)
for _, group := range resp.Groups {
pterm.Println()
if len(group.Components) == 0 {
groupLabel, groupColor := getStatusDisplay(group.Status)
pterm.Printf(" %s %s %s\n", coloredDot(groupColor), pterm.Bold.Sprint(group.Name), groupLabel)
} else {
pterm.Println(" " + pterm.Bold.Sprint(group.Name))
for _, comp := range group.Components {
compLabel, compColor := getStatusDisplay(comp.Status)
pterm.Printf(" %s %-20s %s\n", coloredDot(compColor), comp.Name, compLabel)
}
}
}
pterm.Println()
}