diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-28 18:26:29 +0200 |
commit | 26bd1dd11010b4d86cebe2510ad7085a6b316334 (patch) | |
tree | f1fe939311c75bd615071e96f3d37822cccd77a7 /commands/version.go | |
parent | c0dbc149d5c0c3610476ba14a800c9ba803a2c2c (diff) | |
download | git-bug-26bd1dd11010b4d86cebe2510ad7085a6b316334.tar.gz |
commands: refactor to avoid globals
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) } |