From 5023a3b1782510568cf9e823bb74f588b9350a38 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 26 Mar 2020 15:27:34 -0400 Subject: [PATCH 1/2] cli: send requested help text to stdout This behaviour matches the GNU CLI standard: http://www.gnu.org/prep/standards/html_node/_002d_002dhelp.html --- go.mod | 2 +- go.sum | 2 + main.go | 2 + vendor/github.com/mitchellh/cli/.travis.yml | 10 +++-- vendor/github.com/mitchellh/cli/Makefile | 5 +-- vendor/github.com/mitchellh/cli/cli.go | 46 ++++++++++++--------- vendor/github.com/mitchellh/cli/go.mod | 2 + vendor/github.com/mitchellh/cli/ui_mock.go | 7 +++- vendor/modules.txt | 2 +- 9 files changed, 49 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index 61048cdbef..95b07dea84 100644 --- a/go.mod +++ b/go.mod @@ -57,7 +57,7 @@ require ( github.com/imdario/mergo v0.3.6 github.com/kr/text v0.1.0 github.com/miekg/dns v1.1.26 - github.com/mitchellh/cli v1.0.0 + github.com/mitchellh/cli v1.1.0 github.com/mitchellh/copystructure v1.0.0 github.com/mitchellh/go-testing-interface v1.0.0 github.com/mitchellh/hashstructure v0.0.0-20170609045927-2bca23e0e452 diff --git a/go.sum b/go.sum index 66e89381ae..1936fceb98 100644 --- a/go.sum +++ b/go.sum @@ -259,6 +259,8 @@ github.com/miekg/dns v1.1.26 h1:gPxPSwALAeHJSjarOs00QjVdV9QoBvc1D2ujQUr5BzU= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= github.com/mitchellh/cli v1.0.0 h1:iGBIsUe3+HZ/AD/Vd7DErOt5sU9fa8Uj7A2s1aggv1Y= github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc= +github.com/mitchellh/cli v1.1.0 h1:tEElEatulEHDeedTxwckzyYMA5c86fbmNIUL1hBIiTg= +github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI= github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= diff --git a/main.go b/main.go index 9755775f2c..3744c0e21e 100644 --- a/main.go +++ b/main.go @@ -48,6 +48,8 @@ func realMain() int { Autocomplete: true, Name: "consul", HelpFunc: cli.FilteredHelpFunc(names, cli.BasicHelpFunc("consul")), + HelpWriter: os.Stdout, + ErrorWriter: os.Stderr, } exitCode, err := cli.Run() diff --git a/vendor/github.com/mitchellh/cli/.travis.yml b/vendor/github.com/mitchellh/cli/.travis.yml index b8599b3ac6..5c01b3a0ec 100644 --- a/vendor/github.com/mitchellh/cli/.travis.yml +++ b/vendor/github.com/mitchellh/cli/.travis.yml @@ -2,10 +2,14 @@ sudo: false language: go +env: + - GO111MODULE=on + go: - - "1.8" - - "1.9" - - "1.10" + - "1.11" + - "1.12" + - "1.13" + - "1.14" branches: only: diff --git a/vendor/github.com/mitchellh/cli/Makefile b/vendor/github.com/mitchellh/cli/Makefile index 4874b0082a..89c0a12097 100644 --- a/vendor/github.com/mitchellh/cli/Makefile +++ b/vendor/github.com/mitchellh/cli/Makefile @@ -12,9 +12,6 @@ testrace: # updatedeps installs all the dependencies to run and build updatedeps: - go list ./... \ - | xargs go list -f '{{ join .Deps "\n" }}{{ printf "\n" }}{{ join .TestImports "\n" }}' \ - | grep -v github.com/mitchellh/cli \ - | xargs go get -f -u -v + go mod download .PHONY: test testrace updatedeps diff --git a/vendor/github.com/mitchellh/cli/cli.go b/vendor/github.com/mitchellh/cli/cli.go index c2dbe55aa0..0a6b6cba9b 100644 --- a/vendor/github.com/mitchellh/cli/cli.go +++ b/vendor/github.com/mitchellh/cli/cli.go @@ -109,18 +109,23 @@ type CLI struct { AutocompleteGlobalFlags complete.Flags autocompleteInstaller autocompleteInstaller // For tests - // HelpFunc and HelpWriter are used to output help information, if - // requested. - // // HelpFunc is the function called to generate the generic help // text that is shown if help must be shown for the CLI that doesn't // pertain to a specific command. - // - // HelpWriter is the Writer where the help text is outputted to. If - // not specified, it will default to Stderr. - HelpFunc HelpFunc + HelpFunc HelpFunc + + // HelpWriter is used to print help text and version when requested. + // Defaults to os.Stderr for backwards compatibility. + // It is recommended that you set HelpWriter to os.Stdout, and + // ErrorWriter to os.Stderr. HelpWriter io.Writer + // ErrorWriter used to output errors when a command can not be run. + // Defaults to the value of HelpWriter for backwards compatibility. + // It is recommended that you set HelpWriter to os.Stdout, and + // ErrorWriter to os.Stderr. + ErrorWriter io.Writer + //--------------------------------------------------------------- // Internal fields set automatically @@ -228,7 +233,7 @@ func (c *CLI) Run() (int, error) { // implementation. If the command is invalid or blank, it is an error. raw, ok := c.commandTree.Get(c.Subcommand()) if !ok { - c.HelpWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.subcommandParent())) + "\n")) + c.ErrorWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.subcommandParent())) + "\n")) return 127, nil } @@ -239,23 +244,23 @@ func (c *CLI) Run() (int, error) { // If we've been instructed to just print the help, then print it if c.IsHelp() { - c.commandHelp(command) + c.commandHelp(c.HelpWriter, command) return 0, nil } // If there is an invalid flag, then error if len(c.topFlags) > 0 { - c.HelpWriter.Write([]byte( + c.ErrorWriter.Write([]byte( "Invalid flags before the subcommand. If these flags are for\n" + "the subcommand, please put them after the subcommand.\n\n")) - c.commandHelp(command) + c.commandHelp(c.ErrorWriter, command) return 1, nil } code := command.Run(c.SubcommandArgs()) if code == RunResultHelp { // Requesting help - c.commandHelp(command) + c.commandHelp(c.ErrorWriter, command) return 1, nil } @@ -310,6 +315,9 @@ func (c *CLI) init() { if c.HelpWriter == nil { c.HelpWriter = os.Stderr } + if c.ErrorWriter == nil { + c.ErrorWriter = c.HelpWriter + } // Build our hidden commands if len(c.HiddenCommands) > 0 { @@ -404,8 +412,8 @@ func (c *CLI) initAutocomplete() { cmd.Flags = map[string]complete.Predictor{ "-" + c.AutocompleteInstall: complete.PredictNothing, "-" + c.AutocompleteUninstall: complete.PredictNothing, - "-help": complete.PredictNothing, - "-version": complete.PredictNothing, + "-help": complete.PredictNothing, + "-version": complete.PredictNothing, } } cmd.GlobalFlags = c.AutocompleteGlobalFlags @@ -483,7 +491,7 @@ func (c *CLI) initAutocompleteSub(prefix string) complete.Command { return cmd } -func (c *CLI) commandHelp(command Command) { +func (c *CLI) commandHelp(out io.Writer, command Command) { // Get the template to use tpl := strings.TrimSpace(defaultHelpTemplate) if t, ok := command.(CommandHelpTemplate); ok { @@ -533,12 +541,12 @@ func (c *CLI) commandHelp(command Command) { // Get the command raw, ok := subcommands[k] if !ok { - c.HelpWriter.Write([]byte(fmt.Sprintf( + c.ErrorWriter.Write([]byte(fmt.Sprintf( "Error getting subcommand %q", k))) } sub, err := raw() if err != nil { - c.HelpWriter.Write([]byte(fmt.Sprintf( + c.ErrorWriter.Write([]byte(fmt.Sprintf( "Error instantiating %q: %s", k, err))) } @@ -559,13 +567,13 @@ func (c *CLI) commandHelp(command Command) { data["Subcommands"] = subcommandsTpl // Write - err = t.Execute(c.HelpWriter, data) + err = t.Execute(out, data) if err == nil { return } // An error, just output... - c.HelpWriter.Write([]byte(fmt.Sprintf( + c.ErrorWriter.Write([]byte(fmt.Sprintf( "Internal error rendering help: %s", err))) } diff --git a/vendor/github.com/mitchellh/cli/go.mod b/vendor/github.com/mitchellh/cli/go.mod index 675325ffa0..bd6c53d355 100644 --- a/vendor/github.com/mitchellh/cli/go.mod +++ b/vendor/github.com/mitchellh/cli/go.mod @@ -1,5 +1,7 @@ module github.com/mitchellh/cli +go 1.11 + require ( github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 github.com/bgentry/speakeasy v0.1.0 diff --git a/vendor/github.com/mitchellh/cli/ui_mock.go b/vendor/github.com/mitchellh/cli/ui_mock.go index 0bfe0a1912..935f28a4a6 100644 --- a/vendor/github.com/mitchellh/cli/ui_mock.go +++ b/vendor/github.com/mitchellh/cli/ui_mock.go @@ -1,9 +1,11 @@ package cli import ( + "bufio" "bytes" "fmt" "io" + "strings" "sync" ) @@ -35,9 +37,12 @@ func (u *MockUi) Ask(query string) (string, error) { var result string fmt.Fprint(u.OutputWriter, query) - if _, err := fmt.Fscanln(u.InputReader, &result); err != nil { + r := bufio.NewReader(u.InputReader) + line, err := r.ReadString('\n') + if err != nil { return "", err } + result = strings.TrimRight(line, "\r\n") return result, nil } diff --git a/vendor/modules.txt b/vendor/modules.txt index fdb04e7402..c8f701523d 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -291,7 +291,7 @@ github.com/mattn/go-isatty github.com/matttproud/golang_protobuf_extensions/pbutil # github.com/miekg/dns v1.1.26 github.com/miekg/dns -# github.com/mitchellh/cli v1.0.0 +# github.com/mitchellh/cli v1.1.0 github.com/mitchellh/cli # github.com/mitchellh/copystructure v1.0.0 github.com/mitchellh/copystructure From 23e2d38b7afc0d522cacbf6ab115905509478129 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Thu, 26 Mar 2020 15:35:34 -0400 Subject: [PATCH 2/2] cli: slightly more direct way of printing custom version --- main.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 3744c0e21e..93162e0e6e 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,10 @@ import ( "os" "github.com/hashicorp/consul/command" + "github.com/hashicorp/consul/command/version" "github.com/hashicorp/consul/lib" _ "github.com/hashicorp/consul/service_os" + consulversion "github.com/hashicorp/consul/version" "github.com/mitchellh/cli" ) @@ -23,18 +25,6 @@ func main() { func realMain() int { 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} cmds := command.Map(ui) var names []string @@ -43,7 +33,7 @@ func realMain() int { } cli := &cli.CLI{ - Args: args, + Args: os.Args[1:], Commands: cmds, Autocomplete: true, Name: "consul", @@ -52,6 +42,11 @@ func realMain() int { ErrorWriter: os.Stderr, } + if cli.IsVersion() { + cmd := version.New(ui, consulversion.GetHumanVersion()) + return cmd.Run(nil) + } + exitCode, err := cli.Run() if err != nil { fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())