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