From a5e9c1deb18e015fc0c5a1571a6af0a3827f6044 Mon Sep 17 00:00:00 2001 From: Sid Kattoju Date: Thu, 7 Dec 2023 11:09:16 -0500 Subject: [PATCH] add check for latest available version --- cmd/preflight/cmd/root.go | 4 ++++ go.mod | 3 +++ go.sum | 4 ++++ version/version.go | 25 ++++++++++++++++++++++++- 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/cmd/preflight/cmd/root.go b/cmd/preflight/cmd/root.go index e52de9557..a11bd1167 100644 --- a/cmd/preflight/cmd/root.go +++ b/cmd/preflight/cmd/root.go @@ -34,6 +34,10 @@ func rootCmd() *cobra.Command { PersistentPreRun: preRunConfig, } + //TODO: Ensure this is non-blocking + //TODO: Find appropriate place for the check to happen + version.Version.CheckForLatestAvailableVersion() + viper := viper.Instance() rootCmd.PersistentFlags().String("config", "", "A preflight config file. The default is config.yaml (env: PFLT_CONFIG)") _ = viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")) diff --git a/go.mod b/go.mod index eb4f5db4f..abe54fca8 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,8 @@ require ( github.com/glebarez/go-sqlite v1.21.2 github.com/go-logr/logr v1.2.4 github.com/google/go-containerregistry v0.15.2 + github.com/google/go-github v17.0.0+incompatible + github.com/hashicorp/go-version v1.2.1 github.com/knqyf263/go-rpmdb v0.0.0-20230517124904-b97c85e63254 github.com/onsi/ginkgo/v2 v2.12.1 github.com/onsi/gomega v1.28.0 @@ -82,6 +84,7 @@ require ( github.com/google/cel-go v0.15.3 // indirect github.com/google/gnostic v0.5.7-v3refs // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-querystring v1.0.0 // indirect github.com/google/gofuzz v1.2.0 // indirect github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect diff --git a/go.sum b/go.sum index f50d19fb1..9910ba47b 100644 --- a/go.sum +++ b/go.sum @@ -444,7 +444,10 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-containerregistry v0.15.2 h1:MMkSh+tjSdnmJZO7ljvEqV1DjfekB6VUEAZgy3a+TQE= github.com/google/go-containerregistry v0.15.2/go.mod h1:wWK+LnOv4jXMM23IT/F1wdYftGWGr47Is8CG+pmHK1Q= +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= +github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= github.com/google/go-github/v35 v35.0.1-0.20210421135231-b235769d1606/go.mod h1:s0515YVTI+IMrDoy9Y4pHt9ShGpzHvHO8rZ7L7acgvs= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/go-replayers/grpcreplay v1.0.0/go.mod h1:8Ig2Idjpr6gifRd6pNVggX6TC1Zw6Jx74AKp7QNH2QE= github.com/google/go-replayers/httpreplay v0.1.2/go.mod h1:YKZViNhiGgqdBlUbI2MwGpq4pXxNmhJLPHQ7cv2b5no= @@ -530,6 +533,7 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= diff --git a/version/version.go b/version/version.go index 157ea917e..259a0dbb8 100644 --- a/version/version.go +++ b/version/version.go @@ -2,7 +2,14 @@ // describing the preflight project. package version -import "fmt" +import ( + "context" + "fmt" + "strings" + + "github.com/google/go-github/github" + semvc "github.com/hashicorp/go-version" +) var ( projectName = "github.com/redhat-openshift-ecosystem/openshift-preflight" @@ -25,3 +32,19 @@ type VersionContext struct { func (vc *VersionContext) String() string { return fmt.Sprintf("%s ", vc.Version, vc.Commit) } + +func (vc *VersionContext) CheckForLatestAvailableVersion() { + client := github.NewClient(nil) + projectTokens := strings.Split(vc.Name, "/") + owner := projectTokens[1] + repo := projectTokens[2] + latestRelease, _, err := client.Repositories.GetLatestRelease(context.Background(), owner, repo) + if err != nil { + return + } + currentVersion, err := semvc.NewVersion(vc.Version) + latestVersion, err := semvc.NewVersion(*latestRelease.TagName) + if currentVersion.LessThan(latestVersion) { + fmt.Println("New version", latestVersion.String(), "available at", *latestRelease.HTMLURL) + } +}