-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
144 lines (131 loc) · 3.71 KB
/
cli.go
File metadata and controls
144 lines (131 loc) · 3.71 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"text/tabwriter"
"time"
cli "github.com/urfave/cli/v3"
)
func checkAction(_ context.Context, cmd *cli.Command) error {
page, err := resolveStatusPage(cmd.Args().First(), cmd.Root().String("config"))
if err != nil {
return err
}
page.Client = &http.Client{}
cmps, err := page.Components()
if err != nil {
return fmt.Errorf("failed to fetch components: %w", err)
}
writer := tabwriter.NewWriter(os.Stdout, 4, 4, 1, ' ', 0)
fTime := cmps.Page.UpdatedAt.Format(time.RFC822)
fmt.Fprintf(writer, "=== %s Components as of %s === \n", cmps.Page.Name, fTime)
for _, c := range cmps.Components {
if c.Group || (c.OnlyShowIfDegraded && c.Status == "operational") {
continue
}
fmt.Fprintf(writer, "%s\t%s\n", c.Name, toIcon(c.Status))
}
incs, err := page.Incidents()
if err != nil {
return fmt.Errorf("failed to fetch incidents: %w", err)
}
if len(incs.Incidents) > 0 {
fmt.Fprint(writer, "\n=== Incidents ===")
for _, i := range incs.Incidents {
fTime := i.UpdatedAt.Format(time.RFC822)
fmt.Fprintf(writer, "\nName:\t%s\n", i.Name)
fmt.Fprintf(writer, "Impact:\t%s %s\n", toIcon(i.Impact), i.Impact)
fmt.Fprintf(writer, "Status:\t%s\n", i.Status)
fmt.Fprintf(writer, "Details:\t%s\n", i.Updates[0].Body)
fmt.Fprintf(writer, "Link:\t%s\n", i.ShortLink)
fmt.Fprintf(writer, "Last Updated:\t%s\n", fTime)
}
}
return writer.Flush()
}
func listAction(_ context.Context, cmd *cli.Command) error {
cfg := loadConfig(cmd.Root().String("config"))
if cfg != nil {
for name, page := range cfg.Pages {
registry[strings.ToLower(name)] = page
}
}
writer := tabwriter.NewWriter(os.Stdout, 2, 4, 2, ' ', 0)
for _, name := range knownServices() {
fmt.Fprintf(writer, "%s\t%s\n", name, registry[name].URL)
}
return writer.Flush()
}
func addAction(_ context.Context, cmd *cli.Command) error {
url := cmd.Args().First()
if url == "" {
return fmt.Errorf("url is required: ruok add <url> --alias <name>")
}
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
return fmt.Errorf("url must start with http:// or https://")
}
aliases := cmd.StringSlice("alias")
if len(aliases) == 0 {
return fmt.Errorf("at least one --alias is required")
}
// Validate that the URL points to an actual Statuspage
sp := StatusPage{URL: url, Client: &http.Client{}}
if _, err := sp.Components(); err != nil {
return fmt.Errorf("URL does not appear to be a valid Statuspage: %w", err)
}
cfgPath := cmd.Root().String("config")
cfg := loadConfig(cfgPath)
if cfg == nil {
cfg = &Config{}
}
if cfg.Pages == nil {
cfg.Pages = make(map[string]StatusPage)
}
for _, alias := range aliases {
cfg.Pages[strings.ToLower(alias)] = StatusPage{URL: url}
}
if err := saveConfig(cfgPath, cfg); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
fmt.Printf("added %s as %s\n", url, strings.Join(aliases, ", "))
return nil
}
func buildRootCommand() *cli.Command {
return &cli.Command{
Name: "ruok",
Usage: "Check the status of any Statuspage-powered service",
Version: "0.2.0",
Action: checkAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Path to config file",
},
},
Commands: []*cli.Command{
{
Name: "list",
Aliases: []string{"ls"},
Usage: "List known services",
Action: listAction,
},
{
Name: "add",
Usage: "Add a service to the config file",
ArgsUsage: "<url>",
Action: addAction,
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "alias",
Aliases: []string{"a"},
Usage: "Name to register the service under (repeatable)",
},
},
},
},
}
}