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