forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
368 lines (336 loc) · 11.6 KB
/
cli.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"encoding/json"
"fmt"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/joho/godotenv"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"time"
"upper.io/db.v3/sqlite"
)
const (
BRAKER = "=============================================================================="
POINT = " "
)
func CatchCLI(args []string) {
switch args[1] {
case "version":
fmt.Printf("Statup v%v\n", VERSION)
case "assets":
RenderBoxes()
core.CreateAllAssets()
case "sass":
core.CompileSASS()
case "update":
gitCurrent, err := CheckGithubUpdates()
if err != nil {
fmt.Println(err)
os.Exit(2)
}
fmt.Printf("Statup Version: v%v\nLatest Version: %v\n", VERSION, gitCurrent.TagName)
if VERSION != gitCurrent.TagName[1:] {
fmt.Printf("You don't have the latest version v%v!\nDownload the latest release at: https://github.com/hunterlong/statup\n", gitCurrent.TagName[1:])
} else {
fmt.Printf("You have the latest version of Statup!\n")
}
case "test":
cmd := args[2]
switch cmd {
case "plugins":
LoadPlugins(true)
}
case "export":
var err error
fmt.Printf("Statup v%v Exporting Static 'index.html' page...\n", VERSION)
RenderBoxes()
core.Configs, err = core.LoadConfig()
if err != nil {
utils.Log(4, "config.yml file not found")
}
RunOnce()
indexSource := core.ExportIndexHTML()
err = core.SaveFile("./index.html", []byte(indexSource))
if err != nil {
utils.Log(4, err)
}
utils.Log(1, "Exported Statup index page: 'index.html'")
case "help":
HelpEcho()
case "run":
utils.Log(1, "Running 1 time and saving to database...")
RunOnce()
fmt.Println("Check is complete.")
case "env":
fmt.Println("Statup Environment Variables")
envs, err := godotenv.Read(".env")
if err != nil {
utils.Log(4, "No .env file found in current directory.")
}
for k, e := range envs {
fmt.Printf("%v=%v\n", k, e)
}
default:
utils.Log(3, "Statup does not have the command you entered.")
}
}
func CheckUpdates() {
}
func RunOnce() {
var err error
core.Configs, err = core.LoadConfig()
if err != nil {
utils.Log(4, "config.yml file not found")
}
err = core.DbConnection(core.Configs.Connection)
if err != nil {
utils.Log(4, err)
}
core.CoreApp, err = core.SelectCore()
if err != nil {
fmt.Println("Core database was not found, Statup is not setup yet.")
}
core.CoreApp.Services, err = core.SelectAllServices()
if err != nil {
utils.Log(4, err)
}
for _, s := range core.CoreApp.Services {
out := core.ServiceCheck(s.ToService())
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", out.Name, out.Domain, (out.Latency * 1000), out.Online)
}
}
func HelpEcho() {
fmt.Printf("Statup v%v - Statup.io\n", VERSION)
fmt.Printf("A simple Application Status Monitor that is opensource and lightweight.\n")
fmt.Printf("Commands:\n")
fmt.Println(" statup - Main command to run Statup server")
fmt.Println(" statup version - Returns the current version of Statup")
fmt.Println(" statup run - Check all services 1 time and then quit")
fmt.Println(" statup test plugins - Test all plugins for required information")
fmt.Println(" statup assets - Dump all assets used locally to be edited.")
fmt.Println(" statup env - Show all environment variables being used for Statup")
fmt.Println(" statup export - Exports the index page as a static HTML for pushing")
fmt.Println(" statup update - Attempts to update to the latest version")
fmt.Println(" statup help - Shows the user basic information about Statup")
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
}
func TestPlugin(plug types.PluginActions) {
defer utils.DeleteFile("./.plugin_test.db")
RenderBoxes()
info := plug.GetInfo()
fmt.Printf("\n" + BRAKER + "\n")
fmt.Printf(" Plugin Name: %v\n", info.Name)
fmt.Printf(" Plugin Description: %v\n", info.Description)
fmt.Printf(" Plugin Routes: %v\n", len(plug.Routes()))
for k, r := range plug.Routes() {
fmt.Printf(" - Route %v - (%v) /%v \n", k+1, r.Method, r.URL)
}
// Function to create a new Core with example services, hits, failures, users, and default communications
FakeSeed(plug)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnLoad(sqlbuilder.Database)'")
core.OnLoad(core.DbSession)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnSuccess(Service)'")
core.OnSuccess(core.SelectService(1).ToService())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnFailure(Service, FailureData)'")
fakeFailD := core.FailureData{
Issue: "No issue, just testing this plugin. This would include HTTP failure information though",
}
core.OnFailure(core.SelectService(1).ToService(), fakeFailD)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnSettingsSaved(Core)'")
fmt.Println(BRAKER)
core.OnSettingsSaved(core.CoreApp.ToCore())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnNewService(Service)'")
core.OnNewService(core.SelectService(2).ToService())
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnNewUser(User)'")
user, _ := core.SelectUser(1)
core.OnNewUser(user)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnUpdateService(Service)'")
srv := core.SelectService(2).ToService()
srv.Type = "http"
srv.Domain = "https://yahoo.com"
core.OnUpdateService(srv)
fmt.Println("\n" + BRAKER)
fmt.Println(POINT + "Sending 'OnDeletedService(Service)'")
core.OnDeletedService(core.SelectService(1).ToService())
fmt.Println("\n" + BRAKER)
}
func FakeSeed(plug types.PluginActions) {
var err error
core.CoreApp = core.NewCore()
core.CoreApp.AllPlugins = []types.PluginActions{plug}
fmt.Printf("\n" + BRAKER)
fmt.Println("\nCreating a SQLite database for testing, will be deleted automatically...")
sqlFake := sqlite.ConnectionURL{
Database: "./.plugin_test.db",
}
core.DbSession, err = sqlite.Open(sqlFake)
if err != nil {
utils.Log(3, err)
}
up, _ := core.SqlBox.String("sqlite_up.sql")
requests := strings.Split(up, ";")
for _, request := range requests {
_, err := core.DbSession.Exec(request)
if err != nil {
utils.Log(2, err)
}
}
fmt.Println("Finished creating Test SQLite database")
fmt.Println("Inserting example services into test database...")
core.CoreApp.Name = "Plugin Test"
core.CoreApp.Description = "This is a fake Core for testing your plugin"
core.CoreApp.Domain = "http://localhost:8080"
core.CoreApp.ApiSecret = "0x0x0x0x0"
core.CoreApp.ApiKey = "abcdefg12345"
fakeSrv := &types.Service{
Name: "Test Plugin Service",
Domain: "https://google.com",
Method: "GET",
}
core.CreateService(fakeSrv)
fakeSrv2 := &types.Service{
Name: "Awesome Plugin Service",
Domain: "https://netflix.com",
Method: "GET",
}
core.CreateService(fakeSrv2)
fakeUser := &types.User{
Id: 6334,
Username: "Bulbasaur",
Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
Email: "info@testdomain.com",
Admin: true,
CreatedAt: time.Now(),
}
core.CreateUser(fakeUser)
fakeUser = &types.User{
Id: 6335,
Username: "Billy",
Password: "$2a$14$NzT/fLdE3f9iB1Eux2C84O6ZoPhI4NfY0Ke32qllCFo8pMTkUPZzy",
Email: "info@awesome.com",
CreatedAt: time.Now(),
}
core.CreateUser(fakeUser)
for i := 0; i <= 50; i++ {
dd := core.HitData{
Latency: rand.Float64(),
}
core.CreateServiceHit(fakeSrv, dd)
dd = core.HitData{
Latency: rand.Float64(),
}
core.CreateServiceHit(fakeSrv2, dd)
fail := core.FailureData{
Issue: "This is not an issue, but it would container HTTP response errors.",
}
core.CreateServiceFailure(fakeSrv, fail)
fail = core.FailureData{
Issue: "HTTP Status Code 521 did not match 200",
}
core.CreateServiceFailure(fakeSrv, fail)
}
fmt.Println("Seeding example data is complete, running Plugin Tests")
}
func CheckGithubUpdates() (GithubResponse, error) {
var gitResp GithubResponse
response, err := http.Get("https://api.github.com/repos/hunterlong/statup/releases/latest")
if err != nil {
return GithubResponse{}, err
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return GithubResponse{}, err
}
err = json.Unmarshal(contents, &gitResp)
return gitResp, err
}
return gitResp, err
}
type GithubResponse struct {
URL string `json:"url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
HTMLURL string `json:"html_url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Draft bool `json:"draft"`
Author GitAuthor `json:"author"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
Assets []GitAssets `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}
type GitAuthor struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}
type GitAssets struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
Uploader GitUploader `json:"uploader"`
ContentType string `json:"content_type"`
State string `json:"state"`
Size int `json:"size"`
DownloadCount int `json:"download_count"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
BrowserDownloadURL string `json:"browser_download_url"`
}
type GitUploader struct {
Login string `json:"login"`
ID int `json:"id"`
NodeID string `json:"node_id"`
AvatarURL string `json:"avatar_url"`
GravatarID string `json:"gravatar_id"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
SiteAdmin bool `json:"site_admin"`
}