Skip to content

Commit

Permalink
feat: add version flag to root command
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjuhminStewart committed Jun 11, 2024
1 parent d432282 commit 14ceb07
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

var cfgFile string
var version string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Expand All @@ -29,7 +30,15 @@ var rootCmd = &cobra.Command{
Long: ``,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Run: func(cmd *cobra.Command, args []string) {
flag, _ := cmd.Flags().GetBool("version")
if flag {
fmt.Println(version)
os.Exit(0)
}

cmd.Help()
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -45,6 +54,8 @@ func setDefaults() {
viper.SetDefault("stewsPath", util.GetHomeDir()+"/.stews.json")
viper.SetDefault("timeFormat", "2006-01-02 15:04:05")
viper.SetDefault("allowFileCreation", false)

version = "v1.2.1"
}

func addSubCommands() {
Expand All @@ -58,6 +69,11 @@ func addSubCommands() {

}

func flags() {
// -v, --version flag to print the version
rootCmd.PersistentFlags().BoolP("version", "v", false, "Print the version")
}

func init() {
cobra.OnInitialize(initConfig)

Expand All @@ -73,6 +89,8 @@ func init() {

// add sub commands
addSubCommands()

flags()
}

// initConfig reads in config file and ENV variables if set.
Expand Down

0 comments on commit 14ceb07

Please sign in to comment.