From 9698ae00bcad595b5ef3b199d9ad8b40cd26d757 Mon Sep 17 00:00:00 2001 From: Sharpz7 Date: Thu, 4 Nov 2021 02:49:25 +0000 Subject: [PATCH] Fixed a tonne of bugs --- .version | 2 +- README.md | 8 +++++++- src/client.go | 3 ++- src/errors.go | 8 ++++---- src/structs.go | 2 +- src/trak.go | 47 +++++++++++++++++++++++++++++++++++++---------- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/.version b/.version index e53d5aa..9a622d6 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -VERSION=3 \ No newline at end of file +VERSION=3.1 \ No newline at end of file diff --git a/README.md b/README.md index aae519a..5049139 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,13 @@ tasks: On linux, just run: ```console ╭─adam@box ~/ -╰─➤ sudo curl -s -L https://github.com/Sharpz7/sharpcd/releases/download/3/install.sh | sudo bash +╰─➤ sudo curl -s -L https://github.com/Sharpz7/sharpcd/releases/download/3.1/install.sh | sudo bash +``` + +Or for just the client: +```console +╭─adam@box ~/ +╰─➤ sudo curl -s -L https://github.com/Sharpz7/sharpcd/releases/download/3.1/install.sh | sudo bash -s client ``` ## Command Options diff --git a/src/client.go b/src/client.go index 87fe1b6..8196705 100644 --- a/src/client.go +++ b/src/client.go @@ -24,7 +24,7 @@ func client() { var err error var file []byte - if len(secretFlag) == 0 { + if len(secretFlag) != 0 { resp, err := http.Get(secretFlag) handle(err, "Failed to download remote sharpcd.yml") defer resp.Body.Close() @@ -101,6 +101,7 @@ func post(payload postData, url string) (response, int) { InsecureSkipVerify: true, }, }, + Timeout: 5 * time.Second, } // Do Request diff --git a/src/errors.go b/src/errors.go index eda9b78..b25f0b1 100644 --- a/src/errors.go +++ b/src/errors.go @@ -1,11 +1,10 @@ package main import ( - "flag" "fmt" - "log" "os" + ui "github.com/gizak/termui" "github.com/joho/godotenv" ) @@ -19,8 +18,9 @@ func handle(e error, msg string) { if os.Getenv("DEV") == "TRUE" { fmt.Println(e) } - log.Fatal(msg) - flag.Usage() + ui.Close() + fmt.Print(msg) + os.Exit(1) } } diff --git a/src/structs.go b/src/structs.go index b60c4d7..9b05160 100644 --- a/src/structs.go +++ b/src/structs.go @@ -4,7 +4,7 @@ import ( "github.com/gorilla/websocket" ) -var sharpCDVersion = "3" +var sharpCDVersion = "3.1" type statusCodes struct { NotPostMethod int diff --git a/src/trak.go b/src/trak.go index 47eda70..4ce223c 100644 --- a/src/trak.go +++ b/src/trak.go @@ -23,8 +23,15 @@ import ( var tablePadding int = 2 +var forceEnd = make(chan int) + func trak() { + if len(flag.Args()) < 2 { + handle(errors.New(""), "No valid trak arg was given") + flag.Usage() + } + var trakArg = flag.Args()[1] switch trakArg { @@ -43,6 +50,11 @@ func trak() { // Lists all jobs running on server func listJobs() { + if len(flag.Args()) < 3 { + handle(errors.New(""), "No valid location was given") + flag.Usage() + } + var location = flag.Args()[2] var jobIDs []string @@ -55,7 +67,10 @@ func listJobs() { urlJobs := con.Trak[location] + "/api/jobs/" - apiOutput, _ := getAPIOutput(urlJobs) + // Insert needed data + secret := getSec() + + apiOutput, _ := getAPIOutput(urlJobs, secret) jobs := apiOutput.Jobs for _, job := range jobs { @@ -77,6 +92,11 @@ func liveFeed() { var trakAll string = "alljobs" var trakOne string = "job" + if len(flag.Args()) < 3 { + handle(errors.New(""), "No valid location was given") + flag.Usage() + } + var trakArg = flag.Args()[1] var location = flag.Args()[2] @@ -90,14 +110,22 @@ func liveFeed() { // Only needed for single job requests if trakArg == trakOne { + if len(flag.Args()) < 3 { + handle(errors.New(""), "No valid Job ID was given") + flag.Usage() + } + var jobID = flag.Args()[3] urlJob = con.Trak[location] + "/api/job/" + jobID urlLog = con.Trak[location] + "/api/logsfeed/" + jobID } + // Insert needed data + secret := getSec() + // Tests to ensure you can actually reach the server - apiOutput, code := getAPIOutput(urlJobs) + apiOutput, code := getAPIOutput(urlJobs, secret) if code == statCode.Accepted { fmt.Printf("Connection to API...") @@ -121,7 +149,7 @@ func liveFeed() { // Gets all rows if there is one job if trakArg == trakOne { - apiOutput, _ := getAPIOutput(urlJob) + apiOutput, _ := getAPIOutput(urlJob, secret) job := apiOutput.Job jobs = append(jobs, job) @@ -129,7 +157,7 @@ func liveFeed() { // Gets all rows if there is there are multiple jobs if trakArg == trakAll { - apiOutput, _ = getAPIOutput(urlJobs) + apiOutput, _ = getAPIOutput(urlJobs, secret) jobs = apiOutput.Jobs } @@ -166,7 +194,7 @@ func liveFeed() { var trakLogs *widgets.Paragraph if trakArg == trakOne { - trakLogs, heightLogs = logging(width, heightTable, urlLog) + trakLogs, heightLogs = logging(width, heightTable, urlLog, secret) ui.Render(trakLogs) } else { heightLogs = heightTable @@ -191,6 +219,7 @@ func liveFeed() { } case <-ticker: draw() + } } @@ -273,8 +302,8 @@ func closing(width int, tablePadding int, heightLogs int) *widgets.Paragraph { } // Creates the logging page -func logging(width int, heightTable int, urlLog string) (*widgets.Paragraph, int) { - logs, _ := getAPIOutput(urlLog) +func logging(width int, heightTable int, urlLog string, secret string) (*widgets.Paragraph, int) { + logs, _ := getAPIOutput(urlLog, secret) msg := logs.Message deltaYLogs := heightTable @@ -329,10 +358,8 @@ func generateColumnWidths(rows [][]string, columns []string) []int { return columnWidths } -func getAPIOutput(url string) (response, int) { +func getAPIOutput(url string, secret string) (response, int) { - // Insert needed data - secret := getSec() trakPayload := trakPostData{ Secret: secret, Version: sharpCDVersion,