Skip to content

Commit

Permalink
Merge pull request #1570 from AlexVulaj/version-prompt
Browse files Browse the repository at this point in the history
Warn users if not using latest release.
  • Loading branch information
openshift-merge-bot[bot] committed Apr 19, 2024
2 parents 86498a9 + d7bf0f4 commit 71d3a04
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
33 changes: 32 additions & 1 deletion cmd/rosa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ import (
"github.com/openshift/rosa/cmd/whoami"
"github.com/openshift/rosa/pkg/arguments"
"github.com/openshift/rosa/pkg/color"
"github.com/openshift/rosa/pkg/info"
"github.com/openshift/rosa/pkg/reporter"
versionUtils "github.com/openshift/rosa/pkg/version"
)

var root = &cobra.Command{
Expand All @@ -60,7 +63,8 @@ var root = &cobra.Command{
Long: "Command line tool for Red Hat OpenShift Service on AWS.\n" +
"For further documentation visit " +
"https://access.redhat.com/documentation/en-us/red_hat_openshift_service_on_aws\n",
Args: cobra.NoArgs,
PersistentPreRun: versionCheck,
Args: cobra.NoArgs,
}

func init() {
Expand Down Expand Up @@ -110,3 +114,30 @@ func main() {
os.Exit(1)
}
}

func versionCheck(cmd *cobra.Command, _ []string) {
if !versionUtils.ShouldRunCheck(cmd) {
return
}

rprtr := reporter.CreateReporter()
rosaVersion, err := versionUtils.NewRosaVersion()
if err != nil {
rprtr.Warnf("Could not verify the current version of ROSA.")
rprtr.Warnf("You might be running on an outdated version. Make sure you are using the current version of ROSA.")
return
}
latestVersionFromMirror, isLatest, err := rosaVersion.IsLatest(info.Version)
if err != nil {
rprtr.Warnf("There was a problem retrieving the latest version of ROSA.")
rprtr.Warnf("You might be running on an outdated version. Make sure you are using the current version of ROSA.")
}
if !isLatest {
rprtr.Warnf("The current version (%s) is not up to date with latest released version (%s).",
info.Version,
latestVersionFromMirror.Original(),
)

rprtr.Warnf("It is recommended that you update to the latest version.")
}
}
12 changes: 10 additions & 2 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"fmt"
"net/http"

"slices"

goVer "github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/cache"
"github.com/openshift/rosa/pkg/clients"
"github.com/openshift/rosa/pkg/logging"
"github.com/openshift/rosa/pkg/output"
)

const (
Expand Down Expand Up @@ -62,8 +66,8 @@ type rosaVersion struct {
retriever Retriever
}

func (v rosaVersion) IsLatest(latestVersion string) (*goVer.Version, bool, error) {
currentVersion, err := goVer.NewVersion(latestVersion)
func (v rosaVersion) IsLatest(version string) (*goVer.Version, bool, error) {
currentVersion, err := goVer.NewVersion(version)
if err != nil {
return nil, false, fmt.Errorf("failed to retrieve current version: %v", err)
}
Expand All @@ -79,3 +83,7 @@ func (v rosaVersion) IsLatest(latestVersion string) (*goVer.Version, bool, error

return nil, true, nil
}

func ShouldRunCheck(cmd *cobra.Command) bool {
return !slices.Contains([]string{"version", "rosa-client"}, cmd.Use) && !output.HasFlag()
}
38 changes: 38 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/rosa/pkg/output"
)

var _ = Describe("IsLatest", func() {
Expand Down Expand Up @@ -83,3 +86,38 @@ var _ = Describe("NewRosaVersion", func() {
})
})
})

var _ = Describe("ShouldRunCheck", func() {
When("command is part of skipped commands", func() {
It("should return false", func() {
cmd := &cobra.Command{Use: "version"}
cmd.ResetFlags()
output.AddFlag(cmd)
output.SetOutput("")
result := ShouldRunCheck(cmd)
Expect(result).To(Equal(false))
})
})

When("command contains 'output' flag", func() {
It("should return false", func() {
cmd := &cobra.Command{}
cmd.ResetFlags()
output.AddFlag(cmd)
output.SetOutput("json")
result := ShouldRunCheck(cmd)
Expect(result).To(Equal(false))
})
})

When("command is not part of skipped commands and doesn't contain 'output' flag", func() {
It("should return true", func() {
cmd := &cobra.Command{}
cmd.ResetFlags()
output.AddFlag(cmd)
output.SetOutput("")
result := ShouldRunCheck(cmd)
Expect(result).To(Equal(true))
})
})
})

0 comments on commit 71d3a04

Please sign in to comment.