Skip to content

Commit 7c821cf

Browse files
committed
refactor(server): use cobra to validate required flags in server storage
1 parent ce8dfa3 commit 7c821cf

4 files changed

Lines changed: 12 additions & 18 deletions

File tree

internal/commands/serverstorage/attach.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (s *attachCommand) InitCommand() {
5858
config.AddToggleFlag(flagSet, &s.params.bootable, "boot-disk", false, "Set attached device as the server's boot disk.")
5959

6060
s.AddFlags(flagSet)
61+
s.Cobra().MarkFlagRequired("storage") //nolint:errcheck
6162
}
6263

6364
// MaximumExecutions implements command.Command
@@ -69,10 +70,6 @@ func (s *attachCommand) MaximumExecutions() int {
6970
func (s *attachCommand) ExecuteSingleArgument(exec commands.Executor, uuid string) (output.Output, error) {
7071
storageSvc := exec.Storage()
7172

72-
if s.params.StorageUUID == "" {
73-
return nil, fmt.Errorf("storage is required")
74-
}
75-
7673
strg, err := storage.SearchSingleStorage(s.params.StorageUUID, storageSvc)
7774
if err != nil {
7875
return nil, err

internal/commands/serverstorage/attach_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ 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
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
1011

1112
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1213
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
13-
"github.com/gemalto/flume"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/mock"
1616
)
@@ -72,7 +72,7 @@ func TestAttachStorageCommand(t *testing.T) {
7272
{
7373
name: "storage is missing",
7474
args: []string{},
75-
error: "storage is required",
75+
error: `required flag(s) "storage" not set`,
7676
},
7777
{
7878
name: "use default values",
@@ -113,15 +113,15 @@ func TestAttachStorageCommand(t *testing.T) {
113113
mService.On("GetStorages", mock.Anything).Return(storages, nil)
114114

115115
c := commands.BuildCommand(AttachCommand(), nil, conf)
116-
err := c.Cobra().Flags().Parse(test.args)
117-
assert.NoError(t, err)
118116

119-
_, err = c.(commands.SingleArgumentCommand).ExecuteSingleArgument(commands.NewExecutor(conf, mService, flume.New("test")), Server1.UUID)
117+
c.Cobra().SetArgs(append(test.args, Server1.UUID))
118+
_, err := mockexecute.MockExecute(c, mService, conf)
120119

121120
if test.error != "" {
122121
assert.Error(t, err)
123122
assert.Equal(t, test.error, err.Error())
124123
} else {
124+
assert.NoError(t, err)
125125
mService.AssertNumberOfCalls(t, targetMethod, 1)
126126
}
127127
})

internal/commands/serverstorage/detach.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func (s *detachCommand) InitCommand() {
4747
flagSet.StringVar(&s.params.Address, "address", defaultDetachParams.Address, "Detach the storage attached to this address.")
4848

4949
s.AddFlags(flagSet)
50+
s.Cobra().MarkFlagRequired("address") //nolint:errcheck
5051
}
5152

5253
// MaximumExecutions implements command.Command
@@ -58,10 +59,6 @@ func (s *detachCommand) MaximumExecutions() int {
5859
func (s *detachCommand) ExecuteSingleArgument(exec commands.Executor, uuid string) (output.Output, error) {
5960
storageSvc := exec.Storage()
6061

61-
if s.params.Address == "" {
62-
return nil, fmt.Errorf("address is required")
63-
}
64-
6562
req := s.params.DetachStorageRequest
6663
req.ServerUUID = uuid
6764

internal/commands/serverstorage/detach_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ 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
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
1011

1112
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1213
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
13-
"github.com/gemalto/flume"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/mock"
1616
)
@@ -50,7 +50,7 @@ func TestDetachCommand(t *testing.T) {
5050
{
5151
name: "Address missing",
5252
args: []string{},
53-
error: "address is required",
53+
error: `required flag(s) "address" not set`,
5454
},
5555
{
5656
name: "Address provided",
@@ -71,15 +71,15 @@ func TestDetachCommand(t *testing.T) {
7171
mService.On(targetMethod, &test.detachReq).Return(&details, nil)
7272

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

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

7978
if test.error != "" {
8079
assert.Error(t, err)
8180
assert.Equal(t, test.error, err.Error())
8281
} else {
82+
assert.NoError(t, err)
8383
mService.AssertNumberOfCalls(t, targetMethod, 1)
8484
}
8585
})

0 commit comments

Comments
 (0)