Created
October 31, 2020 18:13
-
-
Save giuliobosco/132de016e0cb2cea17de01e641919fdd to your computer and use it in GitHub Desktop.
cli_print_stormit_logs.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
realy bad code, to rewrite | |
*/ | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
const ( | |
app = "app code" // do request via browser and take appcode | |
env = "production" | |
authBearer = "token" // i've opened session on chrome took token | |
) | |
// Log portion of deploy | |
type Log struct { | |
Message string `json:"message"` | |
Status bool `json:"status"` | |
Title string `json:"title"` | |
} | |
// Deploy is the object containing a deploy | |
type Deploy struct { | |
Branch string `json:"branch"` | |
//CreatedAt *time.Time `json:"createdAt"` | |
//StoppedAt *time.Time `json:"stoppedAt"` | |
AppID string `json:"appId"` | |
ID string `json:"id"` | |
Logs []Log `json:"logs"` | |
} | |
// Action is the result of request | |
type Action struct { | |
Deploys []Deploy `json:"deploys"` | |
} | |
func main() { | |
timeout := time.Duration(5 * time.Second) | |
client := http.Client{ | |
Timeout: timeout, | |
} | |
request, err := http.NewRequest("GET", fmt.Sprintf("https://api.stormkit.io/app/%s/deploys?env=%s&from=0&limit=1", app, env), nil) | |
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authBearer)) | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
resp, err := client.Do(request) | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
//fmt.Println(string(body)) | |
var a Action | |
err = json.Unmarshal(body, &a) | |
if err != nil { | |
log.Fatalln(err) | |
return | |
} | |
for _, d := range a.Deploys { | |
printDeploy(d) | |
} | |
} | |
func l() { | |
for i := 0; i < 20; i++ { | |
fmt.Print("#") | |
} | |
fmt.Println() | |
} | |
func printDeploy(d Deploy) { | |
l() | |
l() | |
fmt.Printf("APP: %s\n", d.AppID) | |
fmt.Printf("Branch: %s\n", d.Branch) | |
l() | |
l() | |
fmt.Println("\n\n") | |
for _, log := range d.Logs { | |
l() | |
fmt.Println(log.Title) | |
if log.Status { | |
fmt.Println("OK") | |
} else { | |
fmt.Println("ERROR") | |
} | |
l() | |
fmt.Println(log.Message, "\n\n\n") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment