Skip to content

Commit 10a8600

Browse files
authored
add --skip-version-check flag (#314)
* don't prompt to upgrade if running currentVersion-next When compiling from the latest source code, osdctl would prompt the user to upgrade to the latest version because it automatically appends "-next" during compilation. Since the latest commit in the master branch is newer than the latest tagged build, osdctl shouldn't prompt the user to downgrade. * remove dead code * add an option to skip the latest-released-version check The check was moved out of main() and into a PersistentPreRun hook on the root command to take advantage of cobra argument parsing and keep main() strictly for setting up and executing the command
1 parent 45f2465 commit 10a8600

File tree

3 files changed

+43
-42
lines changed

3 files changed

+43
-42
lines changed

cmd/cmd.go

+40
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
6+
"strings"
57

68
routev1 "github.com/openshift/api/route/v1"
79
awsv1alpha1 "github.com/openshift/aws-account-operator/api/v1alpha1"
@@ -26,6 +28,7 @@ import (
2628
"github.com/openshift/osdctl/cmd/sts"
2729
"github.com/openshift/osdctl/internal/utils/globalflags"
2830
"github.com/openshift/osdctl/pkg/k8s"
31+
"github.com/openshift/osdctl/pkg/utils"
2932
)
3033

3134
func init() {
@@ -43,6 +46,43 @@ func NewCmdRoot(streams genericclioptions.IOStreams) *cobra.Command {
4346
Short: "OSD CLI",
4447
Long: `CLI tool to provide OSD related utilities`,
4548
DisableAutoGenTag: true,
49+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
50+
skipVersionCheck, err := cmd.Flags().GetBool("skip-version-check")
51+
if err != nil {
52+
fmt.Println("flag --skip-version-check/-S undefined")
53+
os.Exit(1)
54+
}
55+
56+
if !skipVersionCheck {
57+
latestVersion, err := utils.GetLatestVersion()
58+
if err != nil {
59+
fmt.Println("Warning: Unable to verify that osdctl is running under the latest released version. Error trying to reach GitHub:")
60+
fmt.Println(err)
61+
fmt.Println("Please be aware that you are possibly running an outdated or unreleased version.")
62+
63+
// Version query failed, so we just assume that the version didn't change
64+
latestVersion = utils.Version
65+
}
66+
67+
if utils.Version != latestVersion {
68+
fmt.Println("The current version is different than the latest released version.")
69+
fmt.Println("It is recommended that you update to the latest released version to ensure that no known bugs or issues are hit.")
70+
fmt.Println("Please confirm that you would like to continue with [y|n]")
71+
72+
var input string
73+
for {
74+
fmt.Scanln(&input)
75+
if strings.ToLower(input) == "y" {
76+
break
77+
}
78+
if strings.ToLower(input) == "n" {
79+
fmt.Println("Exiting")
80+
os.Exit(0)
81+
}
82+
}
83+
}
84+
}
85+
},
4686
}
4787

4888
globalflags.AddGlobalFlags(rootCmd, globalOpts)

internal/utils/globalflags/globalflags.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010

1111
// GlobalOptions defines all available commands
1212
type GlobalOptions struct {
13-
Output string
13+
Output string
14+
SkipVersionCheck bool
1415
}
1516

1617
// AddGlobalFlags adds the Global Flags to the root command
1718
func AddGlobalFlags(cmd *cobra.Command, opts *GlobalOptions) {
1819
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
1920
cmd.PersistentFlags().StringVarP(&opts.Output, "output", "o", "", "Valid formats are ['', 'json', 'yaml', 'env']")
21+
cmd.PersistentFlags().BoolVarP(&opts.SkipVersionCheck, "skip-version-check", "S", false, "skip checking to see if this is the most recent release")
2022
}
2123

2224
// GetFlags adds the kubeFlags we care about and adds the flags from the provided command

main.go

-41
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package main
22

33
import (
4-
"flag"
54
"fmt"
65
"os"
7-
"strings"
86

97
"github.com/openshift/osdctl/cmd"
108
"github.com/openshift/osdctl/pkg/osdctlConfig"
11-
"github.com/openshift/osdctl/pkg/utils"
12-
"github.com/spf13/pflag"
139

1410
"k8s.io/cli-runtime/pkg/genericclioptions"
1511
)
@@ -22,43 +18,6 @@ func main() {
2218
return
2319
}
2420

25-
latestVersion, err := utils.GetLatestVersion()
26-
if err != nil {
27-
fmt.Println("Warning: Unable to verify that osdctl is running under the latest version. Error trying to reach GitHub:")
28-
fmt.Println(err)
29-
fmt.Println("Please be aware that you are possibly running an outdated version.")
30-
31-
// Version query failed, so we just assume that the version didn't change
32-
latestVersion = utils.Version
33-
}
34-
35-
if utils.Version != latestVersion {
36-
fmt.Println("The current version is different than the latest version.")
37-
fmt.Println("It is recommended that you update to the latest version to ensure that no known bugs or issues are hit.")
38-
fmt.Println("Please confirm that you would like to continue with [y|n]")
39-
40-
var input string
41-
for {
42-
fmt.Scanln(&input)
43-
if strings.ToLower(input) == "y" {
44-
break
45-
}
46-
if strings.ToLower(input) == "n" {
47-
fmt.Println("Exiting")
48-
return
49-
}
50-
fmt.Println("Input not recognized. Please select [y|n]")
51-
}
52-
}
53-
54-
flags := pflag.NewFlagSet("osdctl", pflag.ExitOnError)
55-
err = flag.CommandLine.Parse([]string{})
56-
if err != nil {
57-
fmt.Println("Error parsing commandline flags: ", err.Error())
58-
return
59-
}
60-
pflag.CommandLine = flags
61-
6221
command := cmd.NewCmdRoot(genericclioptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr})
6322

6423
if err := command.Execute(); err != nil {

0 commit comments

Comments
 (0)