Skip to content

Commit d69a055

Browse files
committed
refactor(server): use cobra to validate required flags in server network-interface
1 parent 0ad7546 commit d69a055

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

internal/commands/networkinterface/delete.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package networkinterface
22

33
import (
4-
"errors"
54
"fmt"
65

76
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
@@ -35,7 +34,9 @@ func DeleteCommand() commands.Command {
3534
func (s *deleteCommand) InitCommand() {
3635
fs := &pflag.FlagSet{}
3736
fs.IntVar(&s.interfaceIndex, "index", 0, "Interface index.")
37+
3838
s.AddFlags(fs)
39+
s.Cobra().MarkFlagRequired("index") //nolint:errcheck
3940
}
4041

4142
// MaximumExecutions implements Command.MaximumExecutions
@@ -45,12 +46,6 @@ func (s *deleteCommand) MaximumExecutions() int {
4546

4647
// ExecuteSingleArgument implements commands.SingleArgumentCommand
4748
func (s *deleteCommand) ExecuteSingleArgument(exec commands.Executor, arg string) (output.Output, error) {
48-
if s.interfaceIndex == 0 {
49-
return nil, fmt.Errorf("interface index is required")
50-
}
51-
if arg == "" {
52-
return nil, errors.New("single server uuid is required")
53-
}
5449
msg := fmt.Sprintf("Deleting network interface %d of server %s", s.interfaceIndex, arg)
5550
logline := exec.NewLogEntry(msg)
5651
logline.StartedNow()

internal/commands/networkinterface/delete_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
77
"github.com/UpCloudLtd/upcloud-cli/internal/config"
88
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
910

1011
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1112
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
12-
"github.com/gemalto/flume"
1313
"github.com/stretchr/testify/assert"
1414
)
1515

@@ -32,15 +32,10 @@ func TestDeleteCommand(t *testing.T) {
3232
flags: []string{"--index", "4"},
3333
req: request.DeleteNetworkInterfaceRequest{ServerUUID: server.UUID, Index: 4},
3434
},
35-
{
36-
name: "server is missing",
37-
flags: []string{"--index", "4"},
38-
error: "single server uuid is required",
39-
},
4035
{
4136
name: "index is missing",
4237
arg: server.UUID,
43-
error: "interface index is required",
38+
error: `required flag(s) "index" not set`,
4439
},
4540
} {
4641
t.Run(test.name, func(t *testing.T) {
@@ -51,14 +46,14 @@ func TestDeleteCommand(t *testing.T) {
5146
conf := config.New()
5247

5348
c := commands.BuildCommand(DeleteCommand(), nil, conf)
54-
err := c.Cobra().Flags().Parse(test.flags)
55-
assert.NoError(t, err)
5649

57-
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, &mService, flume.New("test")), test.arg)
50+
c.Cobra().SetArgs(append(test.flags, test.arg))
51+
_, err := mockexecute.MockExecute(c, &mService, conf)
5852

5953
if test.error != "" {
6054
assert.EqualError(t, err, test.error)
6155
} else {
56+
assert.NoError(t, err)
6257
mService.AssertNumberOfCalls(t, targetMethod, 1)
6358
}
6459
})

internal/commands/networkinterface/modify.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ func (s *modifyCommand) InitCommand() {
4444
fs.StringVar(&s.bootable, "bootable", s.bootable, "Whether to try booting through the interface.")
4545
fs.StringVar(&s.sourceIPfiltering, "source-ip-filtering", s.sourceIPfiltering, "Whether source IP filtering is enabled on the interface. Disabling it is allowed only for SDN private interfaces.")
4646
fs.StringSliceVar(&s.ipAddresses, "ip-addresses", s.ipAddresses, "A comma-separated list of IP addresses, multiple can be declared\nUsage: --ip-address address=94.237.112.143,family=IPv4")
47-
s.AddFlags(fs) // TODO(ana): replace usage with examples once the refactor is done.
47+
48+
s.AddFlags(fs) // TODO(ana): replace usage with examples once the refactor is done.
49+
s.Cobra().MarkFlagRequired("index") //nolint:errcheck
4850
}
4951

5052
// MaximumExecutions implements Command.MaximumExecutions
@@ -54,9 +56,6 @@ func (s *modifyCommand) MaximumExecutions() int {
5456

5557
// ExecuteSingleArgument implements commands.SingleArgumentCommand
5658
func (s *modifyCommand) ExecuteSingleArgument(exec commands.Executor, arg string) (output.Output, error) {
57-
if s.currentIndex == 0 {
58-
return nil, fmt.Errorf("index is required")
59-
}
6059
ipAddresses, err := mapIPAddressesToRequest(s.ipAddresses)
6160
if err != nil {
6261
return nil, err

internal/commands/networkinterface/modify_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
77
"github.com/UpCloudLtd/upcloud-cli/internal/config"
88
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
9+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
910

1011
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1112
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
12-
"github.com/gemalto/flume"
1313
"github.com/stretchr/testify/assert"
1414
)
1515

@@ -30,7 +30,7 @@ func TestModifyCommand(t *testing.T) {
3030
{
3131
name: "index is missing",
3232
flags: []string{},
33-
error: "index is required",
33+
error: `required flag(s) "index" not set`,
3434
},
3535
{
3636
name: "index is present, using default values",
@@ -71,14 +71,14 @@ func TestModifyCommand(t *testing.T) {
7171
conf := config.New()
7272

7373
c := commands.BuildCommand(ModifyCommand(), nil, conf)
74-
err := c.Cobra().Flags().Parse(test.flags)
75-
assert.NoError(t, err)
7674

77-
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, &mService, flume.New("test")), server.UUID)
75+
c.Cobra().SetArgs(append(test.flags, server.UUID))
76+
_, err := mockexecute.MockExecute(c, &mService, conf)
7877

7978
if test.error != "" {
8079
assert.EqualError(t, err, test.error)
8180
} else {
81+
assert.NoError(t, err)
8282
mService.AssertNumberOfCalls(t, targetMethod, 1)
8383
}
8484
})

internal/commands/runcommand.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func commandRunE(command Command, service internal.AllServices, config *config.C
3838
cmdLogger.Debug("executing single argument", "arguments", args)
3939
// make sure we have an argument
4040
if len(args) != 1 || args[0] == "" {
41-
return fmt.Errorf("exactly one argument is required")
41+
return fmt.Errorf("exactly one positional argument is required")
4242
}
4343
results, err := execute(typedCommand, executor, args, 1, typedCommand.ExecuteSingleArgument)
4444
if err != nil {
@@ -49,7 +49,7 @@ func commandRunE(command Command, service internal.AllServices, config *config.C
4949
cmdLogger.Debug("executing multi argument", "arguments", args)
5050
// make sure we have arguments
5151
if len(args) < 1 {
52-
return fmt.Errorf("at least one argument is required")
52+
return fmt.Errorf("at least one positional argument is required")
5353
}
5454
results, err := execute(typedCommand, executor, args, typedCommand.MaximumExecutions(), typedCommand.Execute)
5555
if err != nil {

internal/commands/runcommand_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TestRunCommand(t *testing.T) {
145145
name: "single arg with no args",
146146
args: []string{},
147147
command: &mockSingle{Command: &cobra.Command{}},
148-
expectedRunError: "exactly one argument is required",
148+
expectedRunError: "exactly one positional argument is required",
149149
},
150150
{
151151
name: "multi args",
@@ -157,7 +157,7 @@ func TestRunCommand(t *testing.T) {
157157
name: "multiarg with no args",
158158
args: []string{},
159159
command: &mockMulti{Command: &cobra.Command{}},
160-
expectedRunError: "at least one argument is required",
160+
expectedRunError: "at least one positional argument is required",
161161
},
162162
} {
163163
t.Run(test.name, func(t *testing.T) {

0 commit comments

Comments
 (0)