forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
263 lines (252 loc) · 8.98 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
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/handlers"
"github.com/hunterlong/statup/plugin"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/utils"
"github.com/joho/godotenv"
"io/ioutil"
"net/http"
"time"
)
// catchCLI will run functions based on the commands sent to Statup
func catchCLI(args []string) error {
dir := utils.Directory
utils.InitLogs()
source.Assets()
loadDotEnvs()
switch args[0] {
case "app":
handlers.DesktopInit(ipAddress, port)
case "version":
if COMMIT != "" {
fmt.Printf("Statup v%v (%v)\n", VERSION, COMMIT)
} else {
fmt.Printf("Statup v%v\n", VERSION)
}
return errors.New("end")
case "assets":
err := source.CreateAllAssets(dir)
if err != nil {
return err
} else {
return errors.New("end")
}
case "sass":
err := source.CompileSASS(dir)
if err == nil {
return errors.New("end")
}
return err
case "update":
gitCurrent, err := checkGithubUpdates()
if err != nil {
return nil
}
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")
}
if err == nil {
return errors.New("end")
}
return nil
case "test":
cmd := args[1]
switch cmd {
case "plugins":
plugin.LoadPlugins()
}
return errors.New("end")
case "export":
var err error
fmt.Printf("Statup v%v Exporting Static 'index.html' page...\n", VERSION)
core.Configs, err = core.LoadConfigFile(dir)
if err != nil {
utils.Log(4, "config.yml file not found")
return err
}
indexSource := core.ExportIndexHTML()
err = utils.SaveFile("./index.html", []byte(indexSource))
if err != nil {
utils.Log(4, err)
return err
}
utils.Log(1, "Exported Statup index page: 'index.html'")
case "help":
HelpEcho()
return errors.New("end")
case "run":
utils.Log(1, "Running 1 time and saving to database...")
RunOnce()
fmt.Println("Check is complete.")
return errors.New("end")
case "env":
fmt.Println("Statup Environment Variable")
envs, err := godotenv.Read(".env")
if err != nil {
utils.Log(4, "No .env file found in current directory.")
return err
}
for k, e := range envs {
fmt.Printf("%v=%v\n", k, e)
}
default:
return nil
}
return errors.New("end")
}
// RunOnce will initialize the Statup application and check each service 1 time, will not run HTTP server
func RunOnce() {
var err error
core.Configs, err = core.LoadConfigFile(utils.Directory)
if err != nil {
utils.Log(4, "config.yml file not found")
}
err = core.Configs.Connect(false, utils.Directory)
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.")
}
_, err = core.CoreApp.SelectAllServices(true)
if err != nil {
utils.Log(4, err)
}
for _, out := range core.CoreApp.Services {
service := out.Select()
out.Check(true)
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", service.Name, service.Domain, (service.Latency * 1000), service.Online)
}
}
// HelpEcho prints out available commands and flags for Statup
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 sass - Compile .scss files into the css directory")
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.Printf("Flags:\n")
fmt.Println(" -ip 127.0.0.1 - Run HTTP server on specific IP address (default: localhost)")
fmt.Println(" -port 8080 - Run HTTP server on Port (default: 8080)")
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
}
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
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return githubResponse{}, err
}
err = json.Unmarshal(contents, &gitResp)
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"`
}