cli: Ensure all no argument console messages are the same. (#26331)

Use a constant to ensure consistency across the CLI when displaying
a console message indicating the command was passed arguments when
it takes none.
This commit is contained in:
James Rasell
2025-07-25 08:05:10 +02:00
committed by GitHub
parent ccaa3b7325
commit 2ef837f02f
45 changed files with 48 additions and 44 deletions

View File

@@ -131,7 +131,7 @@ func (a *ACLAuthMethodCreateCommand) Run(args []string) int {
// Check that we got no arguments.
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -41,7 +41,7 @@ func TestACLAuthMethodCreateCommand_Run(t *testing.T) {
// Test the basic validation on the command.
must.Eq(t, 1, cmd.Run([]string{"-address=" + url, "this-command-does-not-take-args"}))
must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments")
must.StrContains(t, ui.ErrorWriter.String(), uiMessageNoArguments)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

View File

@@ -77,7 +77,7 @@ func (a *ACLAuthMethodListCommand) Run(args []string) int {
// Check that we got no arguments
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -117,7 +117,7 @@ func (a *ACLBindingRuleCreateCommand) Run(args []string) int {
// Check that we got no arguments.
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -39,7 +39,7 @@ func TestACLBindingRuleCreateCommand_Run(t *testing.T) {
// Test the basic validation on the command.
must.Eq(t, 1, cmd.Run([]string{"-address=" + url, "this-command-does-not-take-args"}))
must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments")
must.StrContains(t, ui.ErrorWriter.String(), uiMessageNoArguments)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

View File

@@ -78,7 +78,7 @@ func (a *ACLBindingRuleListCommand) Run(args []string) int {
// Check that we got no arguments
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -74,7 +74,7 @@ func (c *ACLPolicyListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -69,7 +69,7 @@ func (c *ACLPolicySelfCommand) Run(args []string) int {
// Check that we have no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -98,7 +98,7 @@ func (a *ACLRoleCreateCommand) Run(args []string) int {
// Check that we got no arguments.
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -38,7 +38,7 @@ func TestACLRoleCreateCommand_Run(t *testing.T) {
// Test the basic validation on the command.
must.One(t, cmd.Run([]string{"-address=" + url, "this-command-does-not-take-args"}))
must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments")
must.StrContains(t, ui.ErrorWriter.String(), uiMessageNoArguments)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

View File

@@ -75,7 +75,7 @@ func (a *ACLRoleListCommand) Run(args []string) int {
// Check that we got no arguments
if len(flags.Args()) != 0 {
a.Ui.Error("This command takes no arguments")
a.Ui.Error(uiMessageNoArguments)
a.Ui.Error(commandErrorText(a))
return 1
}

View File

@@ -122,7 +122,7 @@ func (c *ACLTokenCreateCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -72,7 +72,7 @@ func (c *ACLTokenListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -53,7 +53,7 @@ func (c *ACLTokenSelfCommand) Run(args []string) int {
// Check that we have no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -74,7 +74,7 @@ func (c *AgentInfoCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -92,7 +92,7 @@ func (c *MonitorCommand) Run(args []string) int {
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -66,7 +66,7 @@ func (c *AgentCheckCommand) Run(args []string) int {
args = flags.Args()
if len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -83,7 +83,7 @@ func (c *DeploymentListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -121,7 +121,7 @@ func (c *EvalListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -32,6 +32,10 @@ const (
formatHCL2 = "hcl2"
)
// uiMessageNoArguments is the message to write to the UI when a command is
// passed arguments, but it does not take any.
const uiMessageNoArguments = "This command takes no arguments"
// maxLineLength is the maximum width of any line.
const maxLineLength int = 78

View File

@@ -105,7 +105,7 @@ func (l *LoginCommand) Run(args []string) int {
args = flags.Args()
if len(args) != 0 {
l.Ui.Error("This command takes no arguments")
l.Ui.Error(uiMessageNoArguments)
l.Ui.Error(commandErrorText(l))
return 1
}

View File

@@ -36,7 +36,7 @@ func TestLoginCommand_Run(t *testing.T) {
// Test the basic validation on the command.
must.Eq(t, 1, cmd.Run([]string{"-address=" + agentURL, "this-command-does-not-take-args"}))
must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments")
must.StrContains(t, ui.ErrorWriter.String(), uiMessageNoArguments)
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()

View File

@@ -78,7 +78,7 @@ func (c *OperatorMetricsCommand) Run(args []string) int {
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -75,7 +75,7 @@ func (c *NamespaceListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -93,7 +93,7 @@ func (c *NodePoolListCommand) Run(args []string) int {
// Check that we don't have any arguments.
if len(flags.Args()) != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -122,7 +122,7 @@ prod-1 <none>`,
{
name: "fail because of arg",
args: []string{"invalid"},
expectedErr: "This command takes no arguments",
expectedErr: uiMessageNoArguments,
expectedCode: 1,
},
{

View File

@@ -462,7 +462,7 @@ func (c *OperatorDebugCommand) Run(args []string) int {
// Verify there are no extra arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -326,7 +326,7 @@ func TestDebug_Failures(t *testing.T) {
name: "fails incorrect args",
args: []string{"some", "bad", "args"},
expectedCode: 1,
expectedError: "This command takes no arguments",
expectedError: uiMessageNoArguments,
},
{
name: "Fails illegal node ids",

View File

@@ -68,7 +68,7 @@ func (c *OperatorGossipKeyringListCommand) Run(args []string) int {
args = flags.Args()
if len(args) != 0 {
c.Ui.Error("This command requires no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -70,7 +70,7 @@ func (c *OperatorRootKeyringListCommand) Run(args []string) int {
args = flags.Args()
if len(args) != 0 {
c.Ui.Error("This command requires no arguments.")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -94,7 +94,7 @@ func (c *OperatorRootKeyringRotateCommand) Run(args []string) int {
args = flags.Args()
if len(args) != 0 {
c.Ui.Error("This command requires no arguments.")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -87,7 +87,7 @@ func (o *OperatorSchedulerSetConfig) Run(args []string) int {
// Check that we got no arguments.
args = flags.Args()
if l := len(args); l != 0 {
o.Ui.Error("This command takes no arguments")
o.Ui.Error(uiMessageNoArguments)
o.Ui.Error(commandErrorText(o))
return 1
}

View File

@@ -77,7 +77,7 @@ func (c *OperatorUtilizationCommand) Run(args []string) int {
args = flags.Args()
if len(args) != 0 {
c.Ui.Error("This command requires no arguments.")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -74,7 +74,7 @@ func (c *QuotaListCommand) Run(args []string) int {
// Check that we got no arguments
args = flags.Args()
if l := len(args); l != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -94,7 +94,7 @@ func (r *RecommendationListCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
r.Ui.Error("This command takes no arguments")
r.Ui.Error(uiMessageNoArguments)
r.Ui.Error(commandErrorText(r))
}

View File

@@ -93,7 +93,7 @@ func (s *ScalingPolicyListCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
s.Ui.Error("This command takes no arguments")
s.Ui.Error(uiMessageNoArguments)
s.Ui.Error(commandErrorText(s))
return 1
}

View File

@@ -54,7 +54,7 @@ func (c *SentinelListCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
}
// Get the HTTP client

View File

@@ -91,7 +91,7 @@ func (c *ServerMembersCommand) Run(args []string) int {
// Check for extra arguments
args = flags.Args()
if len(args) != 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}

View File

@@ -81,7 +81,7 @@ func (s *ServiceListCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
s.Ui.Error("This command takes no arguments")
s.Ui.Error(uiMessageNoArguments)
s.Ui.Error(commandErrorText(s))
return 1
}

View File

@@ -51,7 +51,7 @@ func TestServiceListCommand_Run(t *testing.T) {
// Run the command with some random arguments to ensure we are performing
// this check.
must.One(t, cmd.Run([]string{"-address=" + url, "pretty-please"}))
must.StrContains(t, ui.ErrorWriter.String(), "This command takes no arguments")
must.StrContains(t, ui.ErrorWriter.String(), uiMessageNoArguments)
ui.ErrorWriter.Reset()
// Create a test job with a Nomad service.

View File

@@ -120,7 +120,7 @@ func (s *SetupConsulCommand) Run(args []string) int {
// Check that we got no arguments.
if len(flags.Args()) != 0 {
s.Ui.Error("This command takes no arguments")
s.Ui.Error(uiMessageNoArguments)
s.Ui.Error(commandErrorText(s))
return 1
}

View File

@@ -159,7 +159,7 @@ func (s *SetupVaultCommand) Run(args []string) int {
// Check that we got no arguments.
if len(flags.Args()) != 0 {
s.Ui.Error("This command takes no arguments")
s.Ui.Error(uiMessageNoArguments)
s.Ui.Error(commandErrorText(s))
return 1
}

View File

@@ -51,7 +51,7 @@ func (c *SystemGCCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
}

View File

@@ -51,7 +51,7 @@ func (c *SystemReconcileSummariesCommand) Run(args []string) int {
}
if args = flags.Args(); len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
}

View File

@@ -107,7 +107,7 @@ func (c *VolumeSnapshotListCommand) Run(args []string) int {
args = flags.Args()
if len(args) > 0 {
c.Ui.Error("This command takes no arguments")
c.Ui.Error(uiMessageNoArguments)
c.Ui.Error(commandErrorText(c))
return 1
}