diff --git a/api/client.go b/api/client.go index 52c991b1e2..1735657936 100644 --- a/api/client.go +++ b/api/client.go @@ -705,7 +705,7 @@ func (c *Client) SetAddress(addr string) error { parsedAddr, err := c.config.ParseAddress(addr) if err != nil { - return errwrap.Wrapf("failed to set address: {{err}}", err) + return fmt.Errorf("failed to set address: %w", err) } c.addr = parsedAddr diff --git a/changelog/27265.txt b/changelog/27265.txt new file mode 100644 index 0000000000..926b2c7a07 --- /dev/null +++ b/changelog/27265.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: Fixed an erroneous warning appearing about `-address` not being set when it is. +``` diff --git a/command/base.go b/command/base.go index 1c16618414..47f7be04a8 100644 --- a/command/base.go +++ b/command/base.go @@ -8,7 +8,6 @@ import ( "flag" "fmt" "io" - "io/ioutil" "net/http" "os" "regexp" @@ -212,7 +211,7 @@ func (c *BaseCommand) Client() (*api.Client, error) { } if c.addrWarning != "" && c.UI != nil { - if os.Getenv("VAULT_ADDR") == "" { + if os.Getenv("VAULT_ADDR") == "" && !c.flags.hadAddressFlag { if !c.flagNonInteractive && isatty.IsTerminal(os.Stdin.Fd()) { c.UI.Warn(wrapAtLength(c.addrWarning)) } @@ -627,6 +626,10 @@ type FlagSets struct { hiddens map[string]struct{} completions complete.Flags ui cli.Ui + // hadAddressFlag signals if the FlagSet had an -address + // flag set, for the purposes of warning (see also: + // BaseCommand::addrWarning). + hadAddressFlag bool } // NewFlagSets creates a new flag sets. @@ -635,7 +638,7 @@ func NewFlagSets(ui cli.Ui) *FlagSets { // Errors and usage are controlled by the CLI. mainSet.Usage = func() {} - mainSet.SetOutput(ioutil.Discard) + mainSet.SetOutput(io.Discard) return &FlagSets{ flagSets: make([]*FlagSet, 0, 6), @@ -669,6 +672,15 @@ type ( // Parse parses the given flags, returning any errors. // Warnings, if any, regarding the arguments format are sent to stdout func (f *FlagSets) Parse(args []string, opts ...ParseOptions) error { + // Before parsing, check to see if we have an address flag, for the + // purposes of warning later. This must be done now, as the argument + // will be removed during parsing. + for _, arg := range args { + if strings.HasPrefix(arg, "-address") { + f.hadAddressFlag = true + } + } + err := f.mainSet.Parse(args) displayFlagWarningsDisabled := false