Skip to content

Commit

Permalink
Merge pull request #46 from daichirata/feature/version
Browse files Browse the repository at this point in the history
Add version command
  • Loading branch information
daichirata committed Oct 14, 2022
2 parents ed095e0 + 4c81b59 commit b57c940
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"fmt"
"runtime/debug"

"github.com/spf13/cobra"
)

var Version string

var (
versionCmd = &cobra.Command{
Use: "version",
Short: "Display version",
Run: func(cmd *cobra.Command, args []string) {
if Version != "" {
fmt.Println(Version)
return
}
if buildInfo, ok := debug.ReadBuildInfo(); ok {
fmt.Println(buildInfo.Main.Version)
return
}
fmt.Println("(unknown)")
},
}
)

func init() {
rootCmd.AddCommand(versionCmd)
}
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
"github.com/daichirata/hammer/cmd"
)

var version string

func main() {
cmd.Version = version

if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down

0 comments on commit b57c940

Please sign in to comment.