diff options
author | Michael Muré <michael.mure@consensys.net> | 2019-02-20 21:56:33 +0100 |
---|---|---|
committer | Michael Muré <michael.mure@consensys.net> | 2019-02-20 21:56:33 +0100 |
commit | 59ea2e018e05e7a4033d572f819f4a696ab74397 (patch) | |
tree | 01a427bfc2f67208e1801e8e06a934d91eb9a777 /commands | |
parent | 09692456c7a5491b37a22d72e652de66ecc8d4e7 (diff) | |
download | git-bug-59ea2e018e05e7a4033d572f819f4a696ab74397.tar.gz |
add a "version" command with various outputs, including the git hash and tag
Diffstat (limited to 'commands')
-rw-r--r-- | commands/root.go | 2 | ||||
-rw-r--r-- | commands/version.go | 71 |
2 files changed, 71 insertions, 2 deletions
diff --git a/commands/root.go b/commands/root.go index 5bb2b504..797ae949 100644 --- a/commands/root.go +++ b/commands/root.go @@ -17,8 +17,6 @@ var repo repository.ClockedRepo // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ - Version: "0.4.0", - Use: rootCommandName, Short: "A bug tracker embedded in Git", Long: `git-bug is a bug tracker embedded in git. diff --git a/commands/version.go b/commands/version.go new file mode 100644 index 00000000..b4dc008e --- /dev/null +++ b/commands/version.go @@ -0,0 +1,71 @@ +package commands + +import ( + "fmt" + "runtime" + + "github.com/spf13/cobra" +) + +// These variables are initialized externally during the build. See the Makefile. +var GitCommit string +var GitLastTag string +var GitExactTag string + +var ( + versionNumber bool + versionCommit bool + versionAll bool +) + +func runVersionCmd(cmd *cobra.Command, args []string) { + if versionAll { + fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version) + fmt.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS) + fmt.Printf("Golang version: %s\n", runtime.Version()) + return + } + + if versionNumber { + fmt.Println(RootCmd.Version) + return + } + + if versionCommit { + fmt.Println(GitCommit) + return + } + + fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version) +} + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Show git-bug version information", + Run: runVersionCmd, +} + +func init() { + if GitExactTag == "undefined" { + GitExactTag = "" + } + + RootCmd.Version = GitLastTag + + if GitExactTag == "" { + RootCmd.Version = fmt.Sprintf("%s-dev-%.10s", RootCmd.Version, GitCommit) + } + + RootCmd.AddCommand(versionCmd) + versionCmd.Flags().SortFlags = false + + versionCmd.Flags().BoolVarP(&versionNumber, "number", "n", false, + "Only show the version number", + ) + versionCmd.Flags().BoolVarP(&versionCommit, "commit", "c", false, + "Only show the commit hash", + ) + versionCmd.Flags().BoolVarP(&versionAll, "all", "a", false, + "Show all version informations", + ) +} |