diff options
author | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 16:51:13 +0200 |
---|---|---|
committer | Alexandre Garand <alexandre.garand@fretlink.com> | 2019-08-09 16:51:13 +0200 |
commit | b74d3065f299a1953e6d21e3d48d3ce0629bcaf7 (patch) | |
tree | 0ce1b8fd1a2b3ff7d427b567111f0732faa461ec /vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go | |
parent | ebf8436f01bcacb71e3c378f08b165dfd6dd90e6 (diff) | |
parent | 464a0a808240c83e365ddc7a4c52b82ecf9b76c4 (diff) | |
download | terraform-provider-statuscake-b74d3065f299a1953e6d21e3d48d3ce0629bcaf7.tar.gz terraform-provider-statuscake-b74d3065f299a1953e6d21e3d48d3ce0629bcaf7.tar.zst terraform-provider-statuscake-b74d3065f299a1953e6d21e3d48d3ce0629bcaf7.zip |
Merge branch 'add_ssl_tests' of github.com:alexandreFre/terraform-provider-statuscake
Diffstat (limited to 'vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go')
-rw-r--r-- | vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go b/vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go new file mode 100644 index 0000000..59f41a9 --- /dev/null +++ b/vendor/github.com/DreamItGetIT/statuscake/cmd/statuscake/main.go | |||
@@ -0,0 +1,246 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | logpkg "log" | ||
6 | "os" | ||
7 | "strconv" | ||
8 | |||
9 | "github.com/DreamItGetIT/statuscake" | ||
10 | "strings" | ||
11 | ) | ||
12 | |||
13 | var log *logpkg.Logger | ||
14 | |||
15 | type command func(*statuscake.Client, ...string) error | ||
16 | |||
17 | var commands map[string]command | ||
18 | |||
19 | func init() { | ||
20 | log = logpkg.New(os.Stderr, "", 0) | ||
21 | commands = map[string]command{ | ||
22 | "list": cmdList, | ||
23 | "detail": cmdDetail, | ||
24 | "delete": cmdDelete, | ||
25 | "create": cmdCreate, | ||
26 | "update": cmdUpdate, | ||
27 | } | ||
28 | } | ||
29 | |||
30 | func colouredStatus(s string) string { | ||
31 | switch s { | ||
32 | case "Up": | ||
33 | return fmt.Sprintf("\033[0;32m%s\033[0m", s) | ||
34 | case "Down": | ||
35 | return fmt.Sprintf("\033[0;31m%s\033[0m", s) | ||
36 | default: | ||
37 | return s | ||
38 | } | ||
39 | } | ||
40 | |||
41 | func getEnv(name string) string { | ||
42 | v := os.Getenv(name) | ||
43 | if v == "" { | ||
44 | log.Fatalf("`%s` env variable is required", name) | ||
45 | } | ||
46 | |||
47 | return v | ||
48 | } | ||
49 | |||
50 | func cmdList(c *statuscake.Client, args ...string) error { | ||
51 | tt := c.Tests() | ||
52 | tests, err := tt.All() | ||
53 | if err != nil { | ||
54 | return err | ||
55 | } | ||
56 | |||
57 | for _, t := range tests { | ||
58 | var paused string | ||
59 | if t.Paused { | ||
60 | paused = "yes" | ||
61 | } else { | ||
62 | paused = "no" | ||
63 | } | ||
64 | |||
65 | fmt.Printf("* %d: %s\n", t.TestID, colouredStatus(t.Status)) | ||
66 | fmt.Printf(" WebsiteName: %s\n", t.WebsiteName) | ||
67 | fmt.Printf(" TestType: %s\n", t.TestType) | ||
68 | fmt.Printf(" Paused: %s\n", paused) | ||
69 | fmt.Printf(" ContactGroup: %s\n", fmt.Sprint(t.ContactGroup)) | ||
70 | fmt.Printf(" Uptime: %f\n", t.Uptime) | ||
71 | } | ||
72 | |||
73 | return nil | ||
74 | } | ||
75 | |||
76 | func cmdDetail(c *statuscake.Client, args ...string) error { | ||
77 | if len(args) != 1 { | ||
78 | return fmt.Errorf("command `detail` requires a single argument `TestID`") | ||
79 | } | ||
80 | |||
81 | id, err := strconv.Atoi(args[0]) | ||
82 | if err != nil { | ||
83 | return err | ||
84 | } | ||
85 | |||
86 | tt := c.Tests() | ||
87 | t, err := tt.Detail(id) | ||
88 | if err != nil { | ||
89 | return err | ||
90 | } | ||
91 | |||
92 | var paused string | ||
93 | if t.Paused { | ||
94 | paused = "yes" | ||
95 | } else { | ||
96 | paused = "no" | ||
97 | } | ||
98 | |||
99 | fmt.Printf("* %d: %s\n", t.TestID, colouredStatus(t.Status)) | ||
100 | fmt.Printf(" WebsiteName: %s\n", t.WebsiteName) | ||
101 | fmt.Printf(" WebsiteURL: %s\n", t.WebsiteURL) | ||
102 | fmt.Printf(" PingURL: %s\n", t.PingURL) | ||
103 | fmt.Printf(" TestType: %s\n", t.TestType) | ||
104 | fmt.Printf(" Paused: %s\n", paused) | ||
105 | fmt.Printf(" ContactGroup: %s\n", fmt.Sprint(t.ContactGroup)) | ||
106 | fmt.Printf(" Uptime: %f\n", t.Uptime) | ||
107 | fmt.Printf(" NodeLocations: %s\n", fmt.Sprint(t.NodeLocations)) | ||
108 | |||
109 | return nil | ||
110 | } | ||
111 | |||
112 | func cmdDelete(c *statuscake.Client, args ...string) error { | ||
113 | if len(args) != 1 { | ||
114 | return fmt.Errorf("command `delete` requires a single argument `TestID`") | ||
115 | } | ||
116 | |||
117 | id, err := strconv.Atoi(args[0]) | ||
118 | if err != nil { | ||
119 | return err | ||
120 | } | ||
121 | |||
122 | return c.Tests().Delete(id) | ||
123 | } | ||
124 | |||
125 | func askString(name string) string { | ||
126 | var v string | ||
127 | |||
128 | fmt.Printf("%s: ", name) | ||
129 | _, err := fmt.Scanln(&v) | ||
130 | if err != nil { | ||
131 | log.Fatal(err) | ||
132 | } | ||
133 | |||
134 | return v | ||
135 | } | ||
136 | |||
137 | func askInt(name string) int { | ||
138 | v := askString(name) | ||
139 | i, err := strconv.Atoi(v) | ||
140 | if err != nil { | ||
141 | log.Fatalf("Invalid number `%s`", v) | ||
142 | } | ||
143 | |||
144 | return i | ||
145 | } | ||
146 | |||
147 | func cmdCreate(c *statuscake.Client, args ...string) error { | ||
148 | websiteName := askString("WebsiteName") | ||
149 | websiteURL := askString("WebsiteURL") | ||
150 | testType := askString("TestType") | ||
151 | checkRate := askInt("CheckRate") | ||
152 | contactGroupString := askString("ContactGroup (comma separated list)") | ||
153 | contactGroup := strings.Split(contactGroupString, ",") | ||
154 | nodeLocationsString := askString("NodeLocations (comma separated list)") | ||
155 | nodeLocations := strings.Split(nodeLocationsString, ",") | ||
156 | |||
157 | t := &statuscake.Test{ | ||
158 | WebsiteName: websiteName, | ||
159 | WebsiteURL: websiteURL, | ||
160 | TestType: testType, | ||
161 | CheckRate: checkRate, | ||
162 | NodeLocations: nodeLocations, | ||
163 | ContactGroup: contactGroup, | ||
164 | } | ||
165 | |||
166 | t2, err := c.Tests().Update(t) | ||
167 | if err != nil { | ||
168 | return err | ||
169 | } | ||
170 | |||
171 | fmt.Printf("CREATED: \n%+v\n", t2) | ||
172 | |||
173 | return nil | ||
174 | } | ||
175 | |||
176 | func cmdUpdate(c *statuscake.Client, args ...string) error { | ||
177 | if len(args) != 1 { | ||
178 | return fmt.Errorf("command `update` requires a single argument `TestID`") | ||
179 | } | ||
180 | |||
181 | id, err := strconv.Atoi(args[0]) | ||
182 | if err != nil { | ||
183 | return err | ||
184 | } | ||
185 | |||
186 | tt := c.Tests() | ||
187 | t, err := tt.Detail(id) | ||
188 | if err != nil { | ||
189 | return err | ||
190 | } | ||
191 | |||
192 | t.TestID = id | ||
193 | t.WebsiteName = askString(fmt.Sprintf("WebsiteName [%s]", t.WebsiteName)) | ||
194 | t.WebsiteURL = askString(fmt.Sprintf("WebsiteURL [%s]", t.WebsiteURL)) | ||
195 | t.TestType = askString(fmt.Sprintf("TestType [%s]", t.TestType)) | ||
196 | t.CheckRate = askInt(fmt.Sprintf("CheckRate [%d]", t.CheckRate)) | ||
197 | contactGroupString := askString("ContactGroup (comma separated list)") | ||
198 | t.ContactGroup = strings.Split(contactGroupString, ",") | ||
199 | nodeLocationsString := askString("NodeLocations (comma separated list)") | ||
200 | t.NodeLocations = strings.Split(nodeLocationsString, ",") | ||
201 | |||
202 | t2, err := c.Tests().Update(t) | ||
203 | if err != nil { | ||
204 | return err | ||
205 | } | ||
206 | |||
207 | fmt.Printf("UPDATED: \n%+v\n", t2) | ||
208 | |||
209 | return nil | ||
210 | } | ||
211 | |||
212 | func usage() { | ||
213 | fmt.Printf("Usage:\n") | ||
214 | fmt.Printf(" %s COMMAND\n", os.Args[0]) | ||
215 | fmt.Printf("Available commands:\n") | ||
216 | for k := range commands { | ||
217 | fmt.Printf(" %+v\n", k) | ||
218 | } | ||
219 | } | ||
220 | |||
221 | func main() { | ||
222 | username := getEnv("STATUSCAKE_USERNAME") | ||
223 | apikey := getEnv("STATUSCAKE_APIKEY") | ||
224 | |||
225 | if len(os.Args) < 2 { | ||
226 | usage() | ||
227 | os.Exit(1) | ||
228 | } | ||
229 | |||
230 | var err error | ||
231 | |||
232 | c, err := statuscake.New(statuscake.Auth{Username: username, Apikey: apikey}) | ||
233 | if err != nil { | ||
234 | log.Fatal(err) | ||
235 | } | ||
236 | |||
237 | if cmd, ok := commands[os.Args[1]]; ok { | ||
238 | err = cmd(c, os.Args[2:]...) | ||
239 | } else { | ||
240 | err = fmt.Errorf("Unknown command `%s`", os.Args[1]) | ||
241 | } | ||
242 | |||
243 | if err != nil { | ||
244 | log.Fatalf("Error running command `%s`: %s", os.Args[1], err.Error()) | ||
245 | } | ||
246 | } | ||