Skip to content

Commit

Permalink
Add --version flag to rootCmd (#59).
Browse files Browse the repository at this point in the history
  • Loading branch information
jimafisk committed Aug 23, 2020
1 parent bbedca5 commit 24e3ce8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ before:
- git diff
builds:
- id: linux-build
ldflags:
- -X main.version=$(git describe --tags)
env:
- CGO_ENABLED=1
goos:
Expand All @@ -16,6 +18,7 @@ builds:
- id: darwin-build
ldflags:
- -s
- -X main.version=$(git describe --tags)
env:
- CGO_ENABLED=1
- CC=/home/runner/work/osxcross/target/bin/o64-clang
Expand Down
16 changes: 14 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (

var cfgFile string

var versionFlag bool

// Version gets replaced by git tag referenced in -ldflags on build.
var Version = "undefined"

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "plenti",
Expand All @@ -24,7 +29,13 @@ Svelte frontend = snappy displays
Learn more at https://plenti.co`,
// 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) {
if versionFlag {
fmt.Println(Version)
} else {
cmd.Help()
}
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -47,7 +58,8 @@ func init() {

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display the release number of the build you're using.")
}

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

0 comments on commit 24e3ce8

Please sign in to comment.