Skip to content

Commit 59c3607

Browse files
committed
refactor(server): use cobra to validate required flags
1 parent 16b024c commit 59c3607

4 files changed

Lines changed: 14 additions & 21 deletions

File tree

internal/commands/server/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ func (s *createCommand) InitCommand() {
283283
// fs.BoolVar(&s.params.metadata, "metadata", def.metadata, "Enable metadata service.")
284284
// fs.BoolVar(&s.params.remoteAccess, "remote-access-enabled", def.remoteAccess, "Enables or disables the remote access.")
285285
s.AddFlags(fs)
286+
287+
s.Cobra().MarkFlagRequired("hostname") //nolint:errcheck
288+
s.Cobra().MarkFlagRequired("zone") //nolint:errcheck
286289
}
287290

288291
// ExecuteWithoutArguments implements commands.NoArgumentCommand
289292
func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
290-
if s.params.Hostname == "" || s.params.Zone == "" {
291-
return nil, fmt.Errorf("hostname, zone and some password delivery method are required")
292-
}
293293
if s.params.os == defaultCreateParams.os && s.params.PasswordDelivery == "none" && s.params.sshKeys == nil {
294294
return nil, fmt.Errorf("a password-delivery method, ssh-keys or a custom image must be specified")
295295
}

internal/commands/server/create_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"github.com/UpCloudLtd/upcloud-cli/internal/commands/storage"
99
"github.com/UpCloudLtd/upcloud-cli/internal/config"
1010
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
11+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1112
internal "github.com/UpCloudLtd/upcloud-cli/internal/service"
1213

1314
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1415
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
15-
"github.com/gemalto/flume"
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/mock"
1818
)
@@ -376,15 +376,15 @@ func TestCreateServer(t *testing.T) {
376376
"--title", "title",
377377
"--zone", "zone",
378378
},
379-
error: "hostname, zone and some password delivery method are required",
379+
error: `required flag(s) "hostname" not set`,
380380
},
381381
{
382382
name: "zone is missing",
383383
args: []string{
384384
"--title", "title",
385385
"--hostname", "hostname",
386386
},
387-
error: "hostname, zone and some password delivery method are required",
387+
error: `required flag(s) "zone" not set`,
388388
},
389389
} {
390390
t.Run(test.name, func(t *testing.T) {
@@ -399,10 +399,9 @@ func TestCreateServer(t *testing.T) {
399399
mService.On("GetStorages", mock.Anything).Return(storages, nil)
400400

401401
c := commands.BuildCommand(testCmd, nil, conf)
402-
err := c.Cobra().Flags().Parse(test.args)
403-
assert.NoError(t, err)
404402

405-
_, err = c.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, mService, flume.New("test")))
403+
c.Cobra().SetArgs(test.args)
404+
_, err := mockexecute.MockExecute(c, mService, conf)
406405

407406
if test.error != "" {
408407
if err == nil {

internal/commands/server/load.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ func (s *loadCommand) InitCommand() {
4747
flagSet.StringVar(&s.params.StorageUUID, "storage", defaultLoadParams.StorageUUID, "The UUID of the storage to be loaded in the CD-ROM device.")
4848

4949
s.AddFlags(flagSet)
50+
51+
s.Cobra().MarkFlagRequired("storage") //nolint:errcheck
5052
}
5153

5254
// Execute implements commands.MultipleArgumentCommand
5355
func (s *loadCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
5456
svc := exec.Storage()
5557

56-
if s.params.StorageUUID == "" {
57-
return nil, fmt.Errorf("storage is required")
58-
}
59-
6058
strg, err := storage.SearchSingleStorage(s.params.StorageUUID, svc)
6159
if err != nil {
6260
return nil, err

internal/commands/server/load_test.go

Lines changed: 4 additions & 8 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
)
@@ -66,7 +66,7 @@ func TestLoadCDROMCommand(t *testing.T) {
6666
{
6767
name: "storage is missing",
6868
args: []string{},
69-
error: "storage is required",
69+
error: `required flag(s) "storage" not set`,
7070
},
7171
{
7272
name: "storage is provided",
@@ -89,13 +89,9 @@ func TestLoadCDROMCommand(t *testing.T) {
8989
mService.On(targetMethod, &test.loadReq).Return(&details, nil)
9090

9191
c := commands.BuildCommand(testCmd, nil, conf)
92-
err := c.Cobra().Flags().Parse(test.args)
93-
assert.NoError(t, err)
9492

95-
_, err = c.(commands.MultipleArgumentCommand).Execute(
96-
commands.NewExecutor(conf, mService, flume.New("test")),
97-
Server1.UUID,
98-
)
93+
c.Cobra().SetArgs(append(test.args, Server1.UUID))
94+
_, err := mockexecute.MockExecute(c, mService, conf)
9995

10096
if test.error != "" {
10197
if err == nil {

0 commit comments

Comments
 (0)