9 "github.com/DreamItGetIT/statuscake"
12 var log *logpkg.Logger
14 type command func(*statuscake.Client, ...string) error
16 var commands map[string]command
19 log = logpkg.New(os.Stderr, "", 0)
20 commands = map[string]command{
29 func colouredStatus(s string) string {
32 return fmt.Sprintf("\033[0;32m%s\033[0m", s)
34 return fmt.Sprintf("\033[0;31m%s\033[0m", s)
40 func getEnv(name string) string {
43 log.Fatalf("`%s` env variable is required", name)
49 func cmdList(c *statuscake.Client, args ...string) error {
51 tests, err := tt.All()
56 for _, t := range tests {
64 fmt.Printf("* %d: %s\n", t.TestID, colouredStatus(t.Status))
65 fmt.Printf(" WebsiteName: %s\n", t.WebsiteName)
66 fmt.Printf(" TestType: %s\n", t.TestType)
67 fmt.Printf(" Paused: %s\n", paused)
68 fmt.Printf(" ContactID: %d\n", t.ContactID)
69 fmt.Printf(" Uptime: %f\n", t.Uptime)
75 func cmdDetail(c *statuscake.Client, args ...string) error {
77 return fmt.Errorf("command `detail` requires a single argument `TestID`")
80 id, err := strconv.Atoi(args[0])
86 t, err := tt.Detail(id)
98 fmt.Printf("* %d: %s\n", t.TestID, colouredStatus(t.Status))
99 fmt.Printf(" WebsiteName: %s\n", t.WebsiteName)
100 fmt.Printf(" WebsiteURL: %s\n", t.WebsiteURL)
101 fmt.Printf(" PingURL: %s\n", t.PingURL)
102 fmt.Printf(" TestType: %s\n", t.TestType)
103 fmt.Printf(" Paused: %s\n", paused)
104 fmt.Printf(" ContactID: %d\n", t.ContactID)
105 fmt.Printf(" Uptime: %f\n", t.Uptime)
110 func cmdDelete(c *statuscake.Client, args ...string) error {
112 return fmt.Errorf("command `delete` requires a single argument `TestID`")
115 id, err := strconv.Atoi(args[0])
120 return c.Tests().Delete(id)
123 func askString(name string) string {
126 fmt.Printf("%s: ", name)
127 _, err := fmt.Scanln(&v)
135 func askInt(name string) int {
137 i, err := strconv.Atoi(v)
139 log.Fatalf("Invalid number `%s`", v)
145 func cmdCreate(c *statuscake.Client, args ...string) error {
146 t := &statuscake.Test{
147 WebsiteName: askString("WebsiteName"),
148 WebsiteURL: askString("WebsiteURL"),
149 TestType: askString("TestType"),
150 CheckRate: askInt("CheckRate"),
153 t2, err := c.Tests().Update(t)
158 fmt.Printf("CREATED: \n%+v\n", t2)
163 func cmdUpdate(c *statuscake.Client, args ...string) error {
165 return fmt.Errorf("command `update` requires a single argument `TestID`")
168 id, err := strconv.Atoi(args[0])
174 t, err := tt.Detail(id)
180 t.WebsiteName = askString(fmt.Sprintf("WebsiteName [%s]", t.WebsiteName))
181 t.WebsiteURL = askString(fmt.Sprintf("WebsiteURL [%s]", t.WebsiteURL))
182 t.TestType = askString(fmt.Sprintf("TestType [%s]", t.TestType))
183 t.CheckRate = askInt(fmt.Sprintf("CheckRate [%d]", t.CheckRate))
185 t2, err := c.Tests().Update(t)
190 fmt.Printf("UPDATED: \n%+v\n", t2)
196 fmt.Printf("Usage:\n")
197 fmt.Printf(" %s COMMAND\n", os.Args[0])
198 fmt.Printf("Available commands:\n")
199 for k := range commands {
200 fmt.Printf(" %+v\n", k)
205 username := getEnv("STATUSCAKE_USERNAME")
206 apikey := getEnv("STATUSCAKE_APIKEY")
208 if len(os.Args) < 2 {
215 c, err := statuscake.New(statuscake.Auth{Username: username, Apikey: apikey})
220 if cmd, ok := commands[os.Args[1]]; ok {
221 err = cmd(c, os.Args[2:]...)
223 err = fmt.Errorf("Unknown command `%s`", os.Args[1])
227 log.Fatalf("Error running command `%s`: %s", os.Args[1], err.Error())