From 9c5c2f1a81d9f044ebc65543561b875a8f9f93cc Mon Sep 17 00:00:00 2001 From: soup Date: Sun, 19 Mar 2023 21:04:52 +0100 Subject: [PATCH] fix(autobrrctl): use brr-api for version check (#752) * use brr-api instead of github directly * fixed comment * better error handling * removed bloat from printing * added timeout and os.Exit upon errors --- cmd/autobrrctl/main.go | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/cmd/autobrrctl/main.go b/cmd/autobrrctl/main.go index 9714505..4c8ccb3 100644 --- a/cmd/autobrrctl/main.go +++ b/cmd/autobrrctl/main.go @@ -9,6 +9,7 @@ import ( "log" "net/http" "os" + "time" "github.com/autobrr/autobrr/internal/config" "github.com/autobrr/autobrr/internal/database" @@ -52,20 +53,39 @@ func main() { switch cmd := flag.Arg(0); cmd { case "version": - fmt.Fprintf(flag.CommandLine.Output(), "Version: %v\nCommit: %v\nBuild: %v\n", version, commit, date) + fmt.Printf("Version: %v\nCommit: %v\nBuild: %v\n", version, commit, date) - // get the latest release tag from Github - resp, err := http.Get(fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)) - if err == nil && resp.StatusCode == http.StatusOK { - defer resp.Body.Close() - var rel struct { - TagName string `json:"tag_name"` - } - if err := json.NewDecoder(resp.Body).Decode(&rel); err == nil { - fmt.Fprintf(flag.CommandLine.Output(), "Latest release: %v\n", rel.TagName) - } + // get the latest release tag from brr-api + client := &http.Client{ + Timeout: 10 * time.Second, } + resp, err := client.Get(fmt.Sprintf("https://api.autobrr.com/repos/%s/%s/releases/latest", owner, repo)) + if err != nil { + if errors.Is(err, http.ErrHandlerTimeout) { + fmt.Println("Server timed out while fetching latest release from api") + } else { + fmt.Printf("Failed to fetch latest release from api: %v\n", err) + } + os.Exit(1) + } + defer resp.Body.Close() + + // brr-api returns 500 instead of 404 here + if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusInternalServerError { + fmt.Printf("No release found for %s/%s\n", owner, repo) + os.Exit(1) + } + + var rel struct { + TagName string `json:"tag_name"` + } + if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil { + fmt.Printf("Failed to decode response from api: %v\n", err) + os.Exit(1) + } + fmt.Printf("Latest release: %v\n", rel.TagName) + case "create-user": if configPath == "" {