diff options
Diffstat (limited to 'commands/version.go')
-rw-r--r-- | commands/version.go | 89 |
1 files changed, 40 insertions, 49 deletions
diff --git a/commands/version.go b/commands/version.go index 668c37fe..71baba40 100644 --- a/commands/version.go +++ b/commands/version.go @@ -1,71 +1,62 @@ package commands import ( - "fmt" "runtime" "github.com/spf13/cobra" ) -// These variables are initialized externally during the build. See the Makefile. -var GitCommit string -var GitLastTag string -var GitExactTag string +type versionOptions struct { + number bool + commit bool + all bool +} -var ( - versionNumber bool - versionCommit bool - versionAll bool -) +func newVersionCommand() *cobra.Command { + env := newEnv() + options := versionOptions{} -func runVersionCmd(cmd *cobra.Command, args []string) { - if versionAll { - fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version) - fmt.Printf("System version: %s/%s\n", runtime.GOARCH, runtime.GOOS) - fmt.Printf("Golang version: %s\n", runtime.Version()) - return + cmd := &cobra.Command{ + Use: "version", + Short: "Show git-bug version information.", + Run: func(cmd *cobra.Command, args []string) { + runVersion(env, options, cmd.Root()) + }, } - if versionNumber { - fmt.Println(RootCmd.Version) - return - } + flags := cmd.Flags() + flags.SortFlags = false - if versionCommit { - fmt.Println(GitCommit) - return - } - - fmt.Printf("%s version: %s\n", rootCommandName, RootCmd.Version) -} + 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", + ) -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Show git-bug version information.", - Run: runVersionCmd, + return cmd } -func init() { - if GitExactTag == "undefined" { - GitExactTag = "" +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 } - RootCmd.Version = GitLastTag - - if GitExactTag == "" { - RootCmd.Version = fmt.Sprintf("%s-dev-%.10s", RootCmd.Version, GitCommit) + if opts.number { + env.out.Println(root.Version) + return } - RootCmd.AddCommand(versionCmd) - versionCmd.Flags().SortFlags = false + if opts.commit { + env.out.Println(GitCommit) + return + } - versionCmd.Flags().BoolVarP(&versionNumber, "number", "n", false, - "Only show the version number", - ) - versionCmd.Flags().BoolVarP(&versionCommit, "commit", "c", false, - "Only show the commit hash", - ) - versionCmd.Flags().BoolVarP(&versionAll, "all", "a", false, - "Show all version information", - ) + env.out.Printf("%s version: %s\n", rootCommandName, root.Version) } |