aboutsummaryrefslogtreecommitdiffstats
path: root/commands/version.go
blob: 668c37fefbc492743633a06a3d8e8f114e59a345 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 information",
	)
}