From 3e305828c3db92fe49bbac440950b42ff1462b63 Mon Sep 17 00:00:00 2001 From: Nick Silverman Date: Wed, 3 Jan 2024 14:59:22 -0500 Subject: [PATCH] cleaner version checking code and actually enabling it to run --- .goreleaser.yml | 4 ++-- cmd/arconn/main.go | 1 + go.mod | 1 + go.sum | 2 ++ pkg/utils/utils.go | 15 +++++++++++---- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 651d8df..840ebb9 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -11,7 +11,7 @@ builds: - -s -w -X github.com/ruelala/arconn/pkg/utils.binary=arconn - -s -w -X github.com/ruelala/arconn/pkg/utils.commit={{.Commit}} - -s -w -X github.com/ruelala/arconn/pkg/utils.date={{.Date}} - - -s -w -X github.com/ruelala/arconn/pkg/utils.version={{.Version}} + - -s -w -X github.com/ruelala/arconn/pkg/utils.version=v{{.Version}} changelog: sort: desc @@ -25,4 +25,4 @@ changelog: archives: - id: default format: zip - name_template: "{{.ProjectName}}-{{.Version}}-{{.Os}}-{{.Arch}}" + name_template: "{{.ProjectName}}-v{{.Version}}-{{.Os}}-{{.Arch}}" diff --git a/cmd/arconn/main.go b/cmd/arconn/main.go index a4dee90..4a17d34 100644 --- a/cmd/arconn/main.go +++ b/cmd/arconn/main.go @@ -10,6 +10,7 @@ import ( ) func main() { + utils.IsLatest() args := utils.ParseFlags() target := utils.Target{} diff --git a/go.mod b/go.mod index 64cecb8..74f08a2 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/twinj/uuid v1.0.0 github.com/xtaci/smux v1.5.24 golang.org/x/crypto v0.17.0 + golang.org/x/mod v0.14.0 golang.org/x/sync v0.5.0 golang.org/x/sys v0.15.0 ) diff --git a/go.sum b/go.sum index f0ed8d3..482222d 100644 --- a/go.sum +++ b/go.sum @@ -85,6 +85,8 @@ github.com/xtaci/smux v1.5.24 h1:77emW9dtnOxxOQ5ltR+8BbsX1kzcOxQ5gB+aaV9hXOY= github.com/xtaci/smux v1.5.24/go.mod h1:OMlQbT5vcgl2gb49mFkYo6SMf+zP3rcjcwQz7ZU7IGY= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= +golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index c196919..49cf0cc 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -11,6 +11,7 @@ import ( "github.com/buger/jsonparser" "github.com/integrii/flaggy" + "golang.org/x/mod/semver" ) // these get passed in as ldflags by goreleaser @@ -21,7 +22,7 @@ var binary string func IsLatest() { var client http.Client - url := fmt.Sprintf("https://api.github.com/repos/RueLaLa/%s/releases/latest", BinaryName()) + url := fmt.Sprintf("https://api.github.com/repos/RueLaLa/%s/releases/latest", binary) resp, err := client.Get(url) if err != nil { return @@ -37,9 +38,15 @@ func IsLatest() { if err != nil { return } - fmt.Printf("v%s\n", version) - if latest != fmt.Sprintf("v%s", version) { - fmt.Printf("A new version of %s is available (%s), head to https://github.com/RueLaLa/%s/releases/latest to get the latest binary\n", binary, latest, binary) + + // this function returns 0 for equality, 1 if left value is greater, and -1 if right value is greater + comparison := semver.Compare(latest, version) + html_url, err := jsonparser.GetString(bodyBytes, "html_url") + if err != nil { + return + } + if comparison == 1 { + fmt.Printf("A new version is available (%s), head to %s to get the latest binary\n", latest, html_url) } }