1package commands
 2
 3import (
 4	"fmt"
 5	"runtime"
 6
 7	"github.com/spf13/cobra"
 8)
 9
10// These variables are initialized externally during the build. See the Makefile.
11var GitCommit string
12var GitLastTag string
13var GitExactTag string
14
15var (
16	versionNumber bool
17	versionCommit bool
18	versionAll    bool
19)
20
21func runVersionCmd(cmd *cobra.Command, args []string) {
22	if versionAll {
23		fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version)
24		fmt.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
25		fmt.Printf("Golang version: %s\n", runtime.Version())
26		return
27	}
28
29	if versionNumber {
30		fmt.Println(RootCmd.Version)
31		return
32	}
33
34	if versionCommit {
35		fmt.Println(GitCommit)
36		return
37	}
38
39	fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version)
40}
41
42var versionCmd = &cobra.Command{
43	Use:   "version",
44	Short: "Show git-bug version information.",
45	Run:   runVersionCmd,
46}
47
48func init() {
49	if GitExactTag == "undefined" {
50		GitExactTag = ""
51	}
52
53	RootCmd.Version = GitLastTag
54
55	if GitExactTag == "" {
56		RootCmd.Version = fmt.Sprintf("%s-dev-%.10s", RootCmd.Version, GitCommit)
57	}
58
59	RootCmd.AddCommand(versionCmd)
60	versionCmd.Flags().SortFlags = false
61
62	versionCmd.Flags().BoolVarP(&versionNumber, "number", "n", false,
63		"Only show the version number",
64	)
65	versionCmd.Flags().BoolVarP(&versionCommit, "commit", "c", false,
66		"Only show the commit hash",
67	)
68	versionCmd.Flags().BoolVarP(&versionAll, "all", "a", false,
69		"Show all version informations",
70	)
71}