git-bug/commands/version.go

65 lines
1.3 KiB
Go
Raw Normal View History

package commands
import (
"runtime"
"github.com/spf13/cobra"
"github.com/MichaelMure/git-bug/commands/execenv"
)
2020-06-28 19:26:29 +03:00
type versionOptions struct {
number bool
commit bool
all bool
}
2020-06-28 19:26:29 +03:00
func newVersionCommand() *cobra.Command {
env := execenv.NewEnv()
2020-06-28 19:26:29 +03:00
options := versionOptions{}
2020-06-28 19:26:29 +03:00
cmd := &cobra.Command{
Use: "version",
Short: "Show git-bug version information",
2020-06-28 19:26:29 +03:00
Run: func(cmd *cobra.Command, args []string) {
runVersion(env, options, cmd.Root())
},
}
2020-06-28 19:26:29 +03:00
flags := cmd.Flags()
flags.SortFlags = false
2020-06-28 19:26:29 +03:00
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",
)
2020-06-28 19:26:29 +03:00
return cmd
}
func runVersion(env *execenv.Env, opts versionOptions, root *cobra.Command) {
2020-06-28 19:26:29 +03:00
if opts.all {
env.Out.Printf("%s version: %s\n", execenv.RootCommandName, root.Version)
env.Out.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS)
env.Out.Printf("Golang version: %s\n", runtime.Version())
2020-06-28 19:26:29 +03:00
return
}
2020-06-28 19:26:29 +03:00
if opts.number {
env.Out.Println(root.Version)
2020-06-28 19:26:29 +03:00
return
}
2020-06-28 19:26:29 +03:00
if opts.commit {
env.Out.Println(GitCommit)
2020-06-28 19:26:29 +03:00
return
}
env.Out.Printf("%s version: %s\n", execenv.RootCommandName, root.Version)
}