From 2d7e6336902040071a5a2161931f7bf5067ff453 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Mon, 27 Mar 2023 19:49:16 -0700 Subject: [PATCH] add util func to get latest version tag --- backend/app.go | 5 +++-- backend/util/util.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/backend/app.go b/backend/app.go index 77a92fc..f88f9f9 100644 --- a/backend/app.go +++ b/backend/app.go @@ -16,8 +16,9 @@ import ( ) const ( - AppName = "supersonic" - configFile = "config.toml" + AppName = "supersonic" + configFile = "config.toml" + LatestReleaseURL = "https://github.com/dweymouth/supersonic/releases/latest" ) var ( diff --git a/backend/util/util.go b/backend/util/util.go index 43fe28b..d7a24d7 100644 --- a/backend/util/util.go +++ b/backend/util/util.go @@ -2,8 +2,11 @@ package util import ( "io" + "log" "math/rand" + "net/http" "os" + "strings" "time" ) @@ -37,3 +40,18 @@ func CopyFile(srcPath, dstPath string) error { _, err = io.Copy(fout, fin) return err } + +func LatestVersionTag(latestReleaseURL string) string { + resp, err := http.Head(latestReleaseURL) + if err != nil { + log.Printf("failed to check for newest version: %s", err.Error()) + return "" + } + url := resp.Request.URL.String() + url = strings.TrimSuffix(url, "/") + idx := strings.LastIndex(url, "/") + if idx >= len(url)-1 { + return "" + } + return url[idx+1:] +}