cli: slightly more direct way of printing custom version

This commit is contained in:
Daniel Nephin 2020-03-26 15:35:34 -04:00
parent b7ff6424f5
commit 0c74867ee9
1 changed files with 8 additions and 13 deletions

21
main.go
View File

@ -7,8 +7,10 @@ import (
"os" "os"
"github.com/hashicorp/consul/command" "github.com/hashicorp/consul/command"
"github.com/hashicorp/consul/command/version"
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
_ "github.com/hashicorp/consul/service_os" _ "github.com/hashicorp/consul/service_os"
consulversion "github.com/hashicorp/consul/version"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
@ -23,18 +25,6 @@ func main() {
func realMain() int { func realMain() int {
log.SetOutput(ioutil.Discard) log.SetOutput(ioutil.Discard)
args := os.Args[1:]
for _, arg := range args {
if arg == "--" {
break
}
if arg == "-v" || arg == "--version" {
args = []string{"version"}
break
}
}
ui := &cli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr} ui := &cli.BasicUi{Writer: os.Stdout, ErrorWriter: os.Stderr}
cmds := command.Map(ui) cmds := command.Map(ui)
var names []string var names []string
@ -43,7 +33,7 @@ func realMain() int {
} }
cli := &cli.CLI{ cli := &cli.CLI{
Args: args, Args: os.Args[1:],
Commands: cmds, Commands: cmds,
Autocomplete: true, Autocomplete: true,
Name: "consul", Name: "consul",
@ -52,6 +42,11 @@ func realMain() int {
ErrorWriter: os.Stderr, ErrorWriter: os.Stderr,
} }
if cli.IsVersion() {
cmd := version.New(ui, consulversion.GetHumanVersion())
return cmd.Run(nil)
}
exitCode, err := cli.Run() exitCode, err := cli.Run()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())