aboutsummaryrefslogtreecommitdiffstats
path: root/commands/version.go
blob: 71baba40a4894cdb2120962efb7307cb05bcf048 (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
package commands

import (
	"runtime"

	"github.com/spf13/cobra"
)

type versionOptions struct {
	number bool
	commit bool
	all    bool
}

func newVersionCommand() *cobra.Command {
	env := newEnv()
	options := versionOptions{}

	cmd := &cobra.Command{
		Use:   "version",
		Short: "Show git-bug version information.",
		Run: func(cmd *cobra.Command, args []string) {
			runVersion(env, options, cmd.Root())
		},
	}

	flags := cmd.Flags()
	flags.SortFlags = false

	flags.BoolVarP(&options.number, "number", "n", false,
		"Only show the version number",
	)
	flags.BoolVarP(&options.commit, "commit", "c", false,
		"Only show the commit hash",
	)
	flags.BoolVarP(&options.all, "all", "a", false,
		"Show all version information",
	)

	return cmd
}

func runVersion(env *Env, opts versionOptions, root *cobra.Command) {
	if opts.all {
		env.out.Printf("%s version: %s\n", rootCommandName, root.Version)
		env.out.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
		env.out.Printf("Golang version: %s\n", runtime.Version())
		return
	}

	if opts.number {
		env.out.Println(root.Version)
		return
	}

	if opts.commit {
		env.out.Println(GitCommit)
		return
	}

	env.out.Printf("%s version: %s\n", rootCommandName, root.Version)
}