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
This commit is contained in:
soup 2023-03-19 21:04:52 +01:00 committed by GitHub
parent 830e719169
commit 9c5c2f1a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,7 @@ import (
"log"
"net/http"
"os"
"time"
"github.com/autobrr/autobrr/internal/config"
"github.com/autobrr/autobrr/internal/database"
@ -52,19 +53,38 @@ 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 {
// 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.Fprintf(flag.CommandLine.Output(), "Latest release: %v\n", rel.TagName)
}
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":