statping/cmd/cli.go

264 lines
9.0 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// 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/>.
2018-06-25 01:58:27 +00:00
package main
import (
"encoding/json"
"errors"
2018-06-25 01:58:27 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
2018-08-23 07:28:48 +00:00
"github.com/hunterlong/statup/handlers"
2018-10-08 10:06:25 +00:00
"github.com/hunterlong/statup/plugin"
2018-08-10 04:38:54 +00:00
"github.com/hunterlong/statup/source"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/utils"
2018-06-28 23:28:55 +00:00
"github.com/joho/godotenv"
"io/ioutil"
"net/http"
"time"
2018-06-25 01:58:27 +00:00
)
2018-10-11 16:53:13 +00:00
// catchCLI will run functions based on the commands sent to Statup
func catchCLI(args []string) error {
2018-07-28 03:24:54 +00:00
dir := utils.Directory
utils.InitLogs()
source.Assets()
2018-10-11 16:53:13 +00:00
loadDotEnvs()
2018-08-16 06:22:20 +00:00
switch args[0] {
2018-08-23 07:28:48 +00:00
case "app":
handlers.DesktopInit(ipAddress, port)
2018-06-25 01:58:27 +00:00
case "version":
2018-08-16 06:22:20 +00:00
if COMMIT != "" {
fmt.Printf("Statup v%v (%v)\n", VERSION, COMMIT)
} else {
fmt.Printf("Statup v%v\n", VERSION)
}
return errors.New("end")
2018-06-28 07:28:07 +00:00
case "assets":
err := source.CreateAllAssets(dir)
2018-08-16 06:22:20 +00:00
if err != nil {
return err
} else {
return errors.New("end")
}
2018-06-28 07:28:07 +00:00
case "sass":
err := source.CompileSASS(dir)
2018-08-16 06:22:20 +00:00
if err == nil {
return errors.New("end")
}
return err
case "update":
2018-10-11 16:53:13 +00:00
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")
}
2018-08-16 06:22:20 +00:00
if err == nil {
return errors.New("end")
}
return nil
case "test":
cmd := args[1]
switch cmd {
case "plugins":
2018-10-11 00:43:23 +00:00
plugin.LoadPlugins()
}
2018-08-16 06:22:20 +00:00
return errors.New("end")
2018-06-25 01:58:27 +00:00
case "export":
2018-06-29 00:01:43 +00:00
var err error
2018-06-25 01:58:27 +00:00
fmt.Printf("Statup v%v Exporting Static 'index.html' page...\n", VERSION)
2018-10-11 16:53:13 +00:00
core.Configs, err = core.LoadConfigFile(dir)
2018-06-29 00:01:43 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(4, "config.yml file not found")
return err
2018-06-29 00:01:43 +00:00
}
2018-06-30 00:57:05 +00:00
indexSource := core.ExportIndexHTML()
2018-10-07 04:48:33 +00:00
err = utils.SaveFile("./index.html", []byte(indexSource))
2018-06-29 02:24:31 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(4, err)
2018-08-16 06:22:20 +00:00
return err
2018-06-29 02:24:31 +00:00
}
2018-06-30 00:57:05 +00:00
utils.Log(1, "Exported Statup index page: 'index.html'")
2018-06-25 01:58:27 +00:00
case "help":
HelpEcho()
2018-08-16 06:22:20 +00:00
return errors.New("end")
2018-06-28 23:28:55 +00:00
case "run":
2018-06-30 00:57:05 +00:00
utils.Log(1, "Running 1 time and saving to database...")
RunOnce()
2018-06-28 23:28:55 +00:00
fmt.Println("Check is complete.")
2018-08-16 06:22:20 +00:00
return errors.New("end")
2018-06-28 23:28:55 +00:00
case "env":
2018-10-11 00:43:23 +00:00
fmt.Println("Statup Environment Variable")
2018-06-28 23:28:55 +00:00
envs, err := godotenv.Read(".env")
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(4, "No .env file found in current directory.")
return err
2018-06-28 23:28:55 +00:00
}
for k, e := range envs {
fmt.Printf("%v=%v\n", k, e)
}
2018-06-25 01:58:27 +00:00
default:
2018-08-16 06:22:20 +00:00
return nil
2018-06-30 00:57:05 +00:00
}
2018-08-16 06:22:20 +00:00
return errors.New("end")
2018-06-30 00:57:05 +00:00
}
// RunOnce will initialize the Statup application and check each service 1 time, will not run HTTP server
2018-06-30 00:57:05 +00:00
func RunOnce() {
var err error
2018-10-11 16:53:13 +00:00
core.Configs, err = core.LoadConfigFile(utils.Directory)
2018-06-30 00:57:05 +00:00
if err != nil {
utils.Log(4, "config.yml file not found")
}
err = core.Configs.Connect(false, utils.Directory)
2018-06-30 00:57:05 +00:00
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.")
}
2018-10-11 16:53:13 +00:00
_, err = core.CoreApp.SelectAllServices(true)
2018-06-30 00:57:05 +00:00
if err != nil {
utils.Log(4, err)
}
2018-09-12 04:14:22 +00:00
for _, out := range core.CoreApp.Services {
service := out.Select()
out.Check(true)
2018-09-12 04:14:22 +00:00
fmt.Printf(" Service %v | URL: %v | Latency: %0.0fms | Online: %v\n", service.Name, service.Domain, (service.Latency * 1000), service.Online)
2018-06-25 01:58:27 +00:00
}
}
// HelpEcho prints out available commands and flags for Statup
2018-06-25 01:58:27 +00:00
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")
2018-07-04 21:03:11 +00:00
fmt.Println(" statup run - Check all services 1 time and then quit")
fmt.Println(" statup test plugins - Test all plugins for required information")
2018-07-04 21:03:11 +00:00
fmt.Println(" statup assets - Dump all assets used locally to be edited.")
fmt.Println(" statup sass - Compile .scss files into the css directory")
2018-06-29 02:24:31 +00:00
fmt.Println(" statup env - Show all environment variables being used for Statup")
2018-06-25 01:58:27 +00:00
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")
2018-08-16 06:22:20 +00:00
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)")
2018-06-25 01:58:27 +00:00
fmt.Println("Give Statup a Star at https://github.com/hunterlong/statup")
}
2018-10-11 16:53:13 +00:00
func checkGithubUpdates() (githubResponse, error) {
var gitResp githubResponse
response, err := http.Get("https://api.github.com/repos/hunterlong/statup/releases/latest")
if err != nil {
2018-10-11 16:53:13 +00:00
return githubResponse{}, err
}
2018-08-19 08:48:02 +00:00
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
2018-10-11 16:53:13 +00:00
return githubResponse{}, err
2018-08-19 08:48:02 +00:00
}
err = json.Unmarshal(contents, &gitResp)
return gitResp, err
}
2018-10-11 16:53:13 +00:00
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"`
2018-10-11 16:53:13 +00:00
Author gitAuthor `json:"author"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
2018-10-11 16:53:13 +00:00
Assets []gitAssets `json:"assets"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
Body string `json:"body"`
}
2018-10-11 16:53:13 +00:00
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"`
}
2018-10-11 16:53:13 +00:00
type gitAssets struct {
URL string `json:"url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
Name string `json:"name"`
Label string `json:"label"`
2018-10-11 16:53:13 +00:00
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"`
}
2018-10-11 16:53:13 +00:00
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"`
}