From 8acda6d4a662c60dba7582b930d37ba4bcfc282a Mon Sep 17 00:00:00 2001 From: hamid ghaf Date: Wed, 3 May 2023 19:50:51 -0700 Subject: [PATCH 1/5] disable printing flags warning message for the ssh command --- command/base.go | 15 ++++++++++++--- command/ssh.go | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/command/base.go b/command/base.go index 72071587dc2d..06cd13dc78ac 100644 --- a/command/base.go +++ b/command/base.go @@ -588,6 +588,7 @@ func (f *FlagSets) Completions() complete.Flags { type ( ParseOptions interface{} ParseOptionAllowRawFormat bool + DisableDisplayFlagWarning bool ) // Parse parses the given flags, returning any errors. @@ -595,9 +596,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 6553a2b0a45a..90bb03080118 100644 --- a/command/ssh.go +++ b/command/ssh.go @@ -241,7 +241,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 } From a8c46c9825fec80cf2cf557a4f05e3b489d7c3eb Mon Sep 17 00:00:00 2001 From: hamid ghaf Date: Thu, 4 May 2023 08:34:53 -0700 Subject: [PATCH 2/5] adding a test --- command/ssh_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/command/ssh_test.go b/command/ssh_test.go index 837865a9ae2f..5a94aec86ad2 100644 --- a/command/ssh_test.go +++ b/command/ssh_test.go @@ -4,6 +4,7 @@ package command import ( + "strings" "testing" "github.com/mitchellh/cli" @@ -217,3 +218,19 @@ func TestIsSingleSSHArg(t *testing.T) { }) } } + +func TestSSHCommandOmitFlagWarning(t *testing.T) { + t.Parallel() + + ui, cmd := testSSHCommand(t) + + code := cmd.Run([]string{"-mode", "ca", "-role", "otp_key_role", "user@1.2.3.4", "-extraFlag", "bug"}) + if code != 2 { + t.Fatalf("expected %d to be %d", code, 2) + } + + 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") + } +} From d616f7511d1bec84be934d2b854680d8998e85c7 Mon Sep 17 00:00:00 2001 From: hamid ghaf Date: Fri, 5 May 2023 15:37:42 -0700 Subject: [PATCH 3/5] CL --- changelog/20502.txt | 3 +++ 1 file changed, 3 insertions(+) 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 +``` From 000894c2eea30e1bb08174da6f3546dc1be6e508 Mon Sep 17 00:00:00 2001 From: hamid ghaf Date: Fri, 5 May 2023 15:40:02 -0700 Subject: [PATCH 4/5] add go doc on the test --- command/ssh_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/command/ssh_test.go b/command/ssh_test.go index 5a94aec86ad2..506fabcb02f4 100644 --- a/command/ssh_test.go +++ b/command/ssh_test.go @@ -219,6 +219,8 @@ 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() From 051f0e113295a46786f46fdff9f7cf2b912953b1 Mon Sep 17 00:00:00 2001 From: hamid ghaf Date: Fri, 5 May 2023 16:12:22 -0700 Subject: [PATCH 5/5] remove the unnecessary code check --- command/ssh_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/command/ssh_test.go b/command/ssh_test.go index 506fabcb02f4..137e541a8681 100644 --- a/command/ssh_test.go +++ b/command/ssh_test.go @@ -226,10 +226,7 @@ func TestSSHCommandOmitFlagWarning(t *testing.T) { ui, cmd := testSSHCommand(t) - code := cmd.Run([]string{"-mode", "ca", "-role", "otp_key_role", "user@1.2.3.4", "-extraFlag", "bug"}) - if code != 2 { - t.Fatalf("expected %d to be %d", code, 2) - } + _ = 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") {