Skip to content

Commit

Permalink
Make command.Apply private; Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plar committed Feb 14, 2024
1 parent 5047687 commit a65e3d5
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 36 deletions.
6 changes: 3 additions & 3 deletions command/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ type Applier interface {
Apply(safecli.CommandAppender) error
}

// Apply appends multiple arguments to the command.
// If any of the arguments encounter an error during the Apply process,
// apply appends multiple arguments to the command.
// If any of the arguments encounter an error during the apply process,
// the error is returned and no changes are made to the command.
// If no error, the arguments are appended to the command.
func Apply(cmd safecli.CommandAppender, args ...Applier) error {
func apply(cmd safecli.CommandAppender, args ...Applier) error {
// create a new subcmd builder which will be used to apply the arguments
// to avoid mutating the command if an error is encountered.
subcmd := safecli.NewBuilder()
Expand Down
2 changes: 1 addition & 1 deletion command/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type Arguments []Applier

// Apply applies the flags to the CLI.
func (args Arguments) Apply(cli safecli.CommandAppender) error {
return Apply(cli, args...)
return apply(cli, args...)
}

// NewArguments creates a new collection of arguments.
Expand Down
3 changes: 0 additions & 3 deletions command/arg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewErrorArgument with error",
Argument: command.NewErrorArgument(ErrArgument),
ExpectedCLI: []string{"cmd"},
ExpectedErr: ErrArgument,
},
{
Expand All @@ -63,7 +62,6 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewArgument with empty name",
Argument: command.NewArgument(""),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidArgumentName,
},
{
Expand All @@ -75,7 +73,6 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewRedactedArgument with empty name",
Argument: command.NewRedactedArgument(""),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidArgumentName,
},
{
Expand Down
12 changes: 8 additions & 4 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ package command

import "github.com/kanisterio/safecli"

// New creates a new safecli.Builder with the given name and arguments.
func New(name string, args ...Applier) (*safecli.Builder, error) {
cmd := safecli.NewBuilder(name)
if err := Apply(cmd, args...); err != nil {
// New creates a new safecli.Builder with the given command name and arguments.
// If the command name is empty, it will be omitted from the output.
func New(cmdName string, args ...Applier) (*safecli.Builder, error) {
cmd := safecli.NewBuilder()
if cmdName != "" {
cmd.AppendLoggable(cmdName)
}
if err := apply(cmd, args...); err != nil {
return nil, err
}
return cmd, nil
Expand Down
4 changes: 0 additions & 4 deletions command/option_arg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewOptionWithArgument with empty name",
Argument: command.NewOptionWithArgument("", "optArg1"),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Name: "NewOptionWithArgument with invalid name",
Argument: command.NewOptionWithArgument("option", "optArg1"),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Expand All @@ -57,13 +55,11 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewOptionWithRedactedArgument with empty name",
Argument: command.NewOptionWithRedactedArgument("", "optArg1"),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Name: "NewOptionWithRedactedArgument with invalid name",
Argument: command.NewOptionWithRedactedArgument("option", "optArg1"),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Expand Down
6 changes: 0 additions & 6 deletions command/option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewOption with empty name",
Argument: command.NewOption("", false),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Name: "NewOption with invalid name",
Argument: command.NewOption("arg1", true),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Expand All @@ -55,13 +53,11 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewToggleOption with empty name",
Argument: command.NewToggleOption("", "--no-option", true),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Name: "NewToggleOption with invalid name",
Argument: command.NewToggleOption("option", "--no-option", true),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Expand All @@ -72,13 +68,11 @@ var _ = check.Suite(&test.ArgumentSuite{Cmd: "cmd", Arguments: []test.ArgumentTe
{
Name: "NewToggleOption with empty name",
Argument: command.NewToggleOption("--option", "", false),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
{
Name: "NewToggleOption with invalid name",
Argument: command.NewToggleOption("--option", "no-option", false),
ExpectedCLI: []string{"cmd"},
ExpectedErr: command.ErrInvalidOptionName,
},
}})
16 changes: 3 additions & 13 deletions test/arg_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (t *ArgumentTest) assertLog(c *check.C, b *safecli.Builder) {
}

// Test runs the argument test.
func (t *ArgumentTest) Test(c *check.C, cmd *safecli.Builder) {
func (t *ArgumentTest) Test(c *check.C, cmdName string) {
if t.Name == "" {
c.Fatal("Name is required")
}
err := command.Apply(cmd, t.Argument)
cmd, err := command.New(cmdName, t.Argument)
if err == nil {
t.assertNoError(c, err)
} else {
Expand All @@ -119,21 +119,11 @@ type ArgumentSuite struct {
// TestArguments runs all tests in the argument suite.
func (s *ArgumentSuite) TestArguments(c *check.C) {
for _, arg := range s.Arguments {
b := newBuilder(s.Cmd)
arg.Test(c, b)
arg.Test(c, s.Cmd)
}
}

// NewArgumentSuite creates a new ArgumentSuite.
func NewArgumentSuite(args []ArgumentTest) *ArgumentSuite {
return &ArgumentSuite{Arguments: args}
}

// newBuilder creates a new safecli.Builder with the given command.
func newBuilder(cmd string) *safecli.Builder {
builder := safecli.NewBuilder()
if cmd != "" {
builder.AppendLoggable(cmd)
}
return builder
}
3 changes: 1 addition & 2 deletions test/arg_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func (t *CustomArgumentTest) Test(c *check.C) {
if t.arg != "" {
argTest.ExpectedCLI = []string{t.arg}
}
b := safecli.NewBuilder()
argTest.Test(c, b)
argTest.Test(c, "")
}

type CustomArgumentSuite struct {
Expand Down

0 comments on commit a65e3d5

Please sign in to comment.