VAULT-24050 Fix erroneous warning showing when -address is provided (#27265)

* VAULT-24050 Fix erroneous warning showing when -address is provided

* VAULT-24050 changelog
This commit is contained in:
Violet Hynes
2024-05-30 09:48:06 -04:00
committed by GitHub
parent b3a4392a06
commit b41dcee3fa
3 changed files with 19 additions and 4 deletions

View File

@@ -705,7 +705,7 @@ func (c *Client) SetAddress(addr string) error {
parsedAddr, err := c.config.ParseAddress(addr) parsedAddr, err := c.config.ParseAddress(addr)
if err != nil { if err != nil {
return errwrap.Wrapf("failed to set address: {{err}}", err) return fmt.Errorf("failed to set address: %w", err)
} }
c.addr = parsedAddr c.addr = parsedAddr

3
changelog/27265.txt Normal file
View File

@@ -0,0 +1,3 @@
```release-note:bug
cli: Fixed an erroneous warning appearing about `-address` not being set when it is.
```

View File

@@ -8,7 +8,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"regexp" "regexp"
@@ -212,7 +211,7 @@ func (c *BaseCommand) Client() (*api.Client, error) {
} }
if c.addrWarning != "" && c.UI != nil { 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()) { if !c.flagNonInteractive && isatty.IsTerminal(os.Stdin.Fd()) {
c.UI.Warn(wrapAtLength(c.addrWarning)) c.UI.Warn(wrapAtLength(c.addrWarning))
} }
@@ -627,6 +626,10 @@ type FlagSets struct {
hiddens map[string]struct{} hiddens map[string]struct{}
completions complete.Flags completions complete.Flags
ui cli.Ui 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. // NewFlagSets creates a new flag sets.
@@ -635,7 +638,7 @@ func NewFlagSets(ui cli.Ui) *FlagSets {
// Errors and usage are controlled by the CLI. // Errors and usage are controlled by the CLI.
mainSet.Usage = func() {} mainSet.Usage = func() {}
mainSet.SetOutput(ioutil.Discard) mainSet.SetOutput(io.Discard)
return &FlagSets{ return &FlagSets{
flagSets: make([]*FlagSet, 0, 6), flagSets: make([]*FlagSet, 0, 6),
@@ -669,6 +672,15 @@ type (
// Parse parses the given flags, returning any errors. // Parse parses the given flags, returning any errors.
// Warnings, if any, regarding the arguments format are sent to stdout // Warnings, if any, regarding the arguments format are sent to stdout
func (f *FlagSets) Parse(args []string, opts ...ParseOptions) error { 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) err := f.mainSet.Parse(args)
displayFlagWarningsDisabled := false displayFlagWarningsDisabled := false