aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
Diffstat (limited to 'commands')
-rw-r--r--commands/root.go2
-rw-r--r--commands/version.go71
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",
+ )
+}