From b450d572c8a48d87864d1b79c66e41b58fcc334b Mon Sep 17 00:00:00 2001 From: Hamid Ghaf <83242695+hghaf099@users.noreply.github.com> Date: Mon, 8 May 2023 16:15:44 +0000 Subject: [PATCH] backport of commit c93f4aa6d097b4f959d56701349277c5f98bc469 --- changelog/20502.txt | 3 +++ command/base.go | 15 ++++++++++++--- command/ssh.go | 2 +- command/ssh_test.go | 16 ++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 changelog/20502.txt diff --git a/changelog/20502.txt b/changelog/20502.txt new file mode 100644 index 000000000000..153309ab84ce --- /dev/null +++ b/changelog/20502.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: disable printing flags warnings messages for the ssh command +``` diff --git a/command/base.go b/command/base.go index 3825db0b1c68..1ccad3b2eb3a 100644 --- a/command/base.go +++ b/command/base.go @@ -585,6 +585,7 @@ func (f *FlagSets) Completions() complete.Flags { type ( ParseOptions interface{} ParseOptionAllowRawFormat bool + DisableDisplayFlagWarning bool ) // Parse parses the given flags, returning any errors. @@ -592,9 +593,17 @@ type ( func (f *FlagSets) Parse(args []string, opts ...ParseOptions) error { err := f.mainSet.Parse(args) - warnings := generateFlagWarnings(f.Args()) - if warnings != "" && Format(f.ui) == "table" { - f.ui.Warn(warnings) + displayFlagWarningsDisabled := false + for _, opt := range opts { + if value, ok := opt.(DisableDisplayFlagWarning); ok { + displayFlagWarningsDisabled = bool(value) + } + } + if !displayFlagWarningsDisabled { + warnings := generateFlagWarnings(f.Args()) + if warnings != "" && Format(f.ui) == "table" { + f.ui.Warn(warnings) + } } if err != nil { diff --git a/command/ssh.go b/command/ssh.go index e5e5af373e7a..5ac2a3dd1484 100644 --- a/command/ssh.go +++ b/command/ssh.go @@ -238,7 +238,7 @@ type SSHCredentialResp struct { func (c *SSHCommand) Run(args []string) int { f := c.Flags() - if err := f.Parse(args); err != nil { + if err := f.Parse(args, DisableDisplayFlagWarning(true)); err != nil { c.UI.Error(err.Error()) return 1 } diff --git a/command/ssh_test.go b/command/ssh_test.go index 344e3de0d247..3d2c8e0c886e 100644 --- a/command/ssh_test.go +++ b/command/ssh_test.go @@ -1,6 +1,7 @@ package command import ( + "strings" "testing" "github.com/mitchellh/cli" @@ -214,3 +215,18 @@ func TestIsSingleSSHArg(t *testing.T) { }) } } + +// TestSSHCommandOmitFlagWarning checks if flags warning messages are printed +// in the output of the CLI command or not. If so, it will fail. +func TestSSHCommandOmitFlagWarning(t *testing.T) { + t.Parallel() + + ui, cmd := testSSHCommand(t) + + _ = cmd.Run([]string{"-mode", "ca", "-role", "otp_key_role", "user@1.2.3.4", "-extraFlag", "bug"}) + + combined := ui.OutputWriter.String() + ui.ErrorWriter.String() + if strings.Contains(combined, "Command flags must be provided before positional arguments. The following arguments will not be parsed as flags") { + t.Fatalf("ssh command displayed flag warnings") + } +}