Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport of disable printing flags warning message for the ssh command into release/1.13.x #20543

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/20502.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: disable printing flags warnings messages for the ssh command
```
15 changes: 12 additions & 3 deletions command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,16 +585,25 @@ func (f *FlagSets) Completions() complete.Flags {
type (
ParseOptions interface{}
ParseOptionAllowRawFormat bool
DisableDisplayFlagWarning bool
)

// 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 {
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 {
Expand Down
2 changes: 1 addition & 1 deletion command/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions command/ssh_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"strings"
"testing"

"github.com/mitchellh/cli"
Expand Down Expand Up @@ -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")
}
}