1package commands
 2
 3import (
 4	"runtime"
 5
 6	"github.com/spf13/cobra"
 7)
 8
 9type versionOptions struct {
10	number bool
11	commit bool
12	all    bool
13}
14
15func newVersionCommand() *cobra.Command {
16	env := newEnv()
17	options := versionOptions{}
18
19	cmd := &cobra.Command{
20		Use:   "version",
21		Short: "Show git-bug version information.",
22		Run: func(cmd *cobra.Command, args []string) {
23			runVersion(env, options, cmd.Root())
24		},
25	}
26
27	flags := cmd.Flags()
28	flags.SortFlags = false
29
30	flags.BoolVarP(&options.number, "number", "n", false,
31		"Only show the version number",
32	)
33	flags.BoolVarP(&options.commit, "commit", "c", false,
34		"Only show the commit hash",
35	)
36	flags.BoolVarP(&options.all, "all", "a", false,
37		"Show all version information",
38	)
39
40	return cmd
41}
42
43func runVersion(env *Env, opts versionOptions, root *cobra.Command) {
44	if opts.all {
45		env.out.Printf("%s version: %s\n", rootCommandName, root.Version)
46		env.out.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
47		env.out.Printf("Golang version: %s\n", runtime.Version())
48		return
49	}
50
51	if opts.number {
52		env.out.Println(root.Version)
53		return
54	}
55
56	if opts.commit {
57		env.out.Println(GitCommit)
58		return
59	}
60
61	env.out.Printf("%s version: %s\n", rootCommandName, root.Version)
62}