Skip to content

Commit 11bcfb2

Browse files
committed
refactor(network): use cobra to validate required flags
1 parent d69a055 commit 11bcfb2

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

internal/commands/network/create.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (s *createCommand) InitCommand() {
4141
fs.StringVar(&s.name, "name", s.name, "Names the network.")
4242
fs.StringVar(&s.zone, "zone", s.zone, "The zone in which the network is configured.")
4343
fs.StringVar(&s.router, "router", s.router, "Add this network to an existing router.")
44-
//XXX: handle multiline flag doc (try nested flags)
44+
// TODO: handle multiline flag doc (try nested flags)
4545
fs.StringArrayVar(&s.networks, "ip-network", s.networks, "A network interface for the server, multiple can be declared.\n\n "+
4646
"Fields: \n"+
4747
" address: string \n"+
@@ -50,7 +50,11 @@ func (s *createCommand) InitCommand() {
5050
" dhcp: true/false \n"+
5151
" dhcp-default-route: true/false \n"+
5252
" dhcp-dns: array of strings")
53+
5354
s.AddFlags(fs)
55+
s.Cobra().MarkFlagRequired("name") //nolint:errcheck
56+
s.Cobra().MarkFlagRequired("zone") //nolint:errcheck
57+
s.Cobra().MarkFlagRequired("ip-network") //nolint:errcheck
5458
}
5559

5660
// MaximumExecutions implements Command.MaximumExecutions
@@ -89,16 +93,6 @@ func (s *createCommand) buildRequest() (*request.CreateNetworkRequest, error) {
8993

9094
// ExecuteWithoutArguments implements commands.NoArgumentCommand
9195
func (s *createCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
92-
// TODO: should we, for example, accept name as the first argument instead of as a flag?
93-
if s.name == "" {
94-
return nil, fmt.Errorf("name is required")
95-
}
96-
if s.zone == "" {
97-
return nil, fmt.Errorf("zone is required")
98-
}
99-
if len(s.networks) == 0 {
100-
return nil, fmt.Errorf("at least one IP network is required")
101-
}
10296
svc := exec.Network()
10397

10498
msg := fmt.Sprintf("Creating network %v", s.name)

internal/commands/network/create_test.go

Lines changed: 10 additions & 9 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,17 +32,17 @@ func TestCreateCommand(t *testing.T) {
3232
{
3333
name: "name is missing",
3434
args: []string{"--zone", n.Zone},
35-
error: "name is required",
35+
error: `required flag(s) "name", "ip-network" not set`,
3636
},
3737
{
3838
name: "zone is missing",
3939
args: []string{"--name", n.Name},
40-
error: "zone is required",
40+
error: `required flag(s) "zone", "ip-network" not set`,
4141
},
4242
{
4343
name: "without network",
4444
args: []string{"--name", n.Name, "--zone", n.Zone},
45-
error: "at least one IP network is required",
45+
error: `required flag(s) "ip-network" not set`,
4646
},
4747
{
4848
name: "with single network",
@@ -96,15 +96,16 @@ func TestCreateCommand(t *testing.T) {
9696
mService := smock.Service{}
9797
mService.On(targetMethod, &test.expected).Return(&upcloud.Network{}, nil)
9898
conf := config.New()
99+
99100
c := commands.BuildCommand(CreateCommand(), nil, conf)
100-
err := c.Cobra().Flags().Parse(test.args)
101-
assert.NoError(t, err)
102101

103-
_, err = c.(commands.NoArgumentCommand).ExecuteWithoutArguments(commands.NewExecutor(conf, &mService, flume.New("test")))
102+
c.Cobra().SetArgs(test.args)
103+
_, err := mockexecute.MockExecute(c, &mService, conf)
104104

105-
if err != nil {
106-
assert.Equal(t, test.error, err.Error())
105+
if test.error != "" {
106+
assert.EqualError(t, err, test.error)
107107
} else {
108+
assert.NoError(t, err)
108109
mService.AssertNumberOfCalls(t, targetMethod, 1)
109110
}
110111
})

internal/commands/network/show_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package network
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/jedib0t/go-pretty/v6/text"
87

98
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
109
"github.com/UpCloudLtd/upcloud-cli/internal/config"
1110
smock "github.com/UpCloudLtd/upcloud-cli/internal/mock"
12-
"github.com/UpCloudLtd/upcloud-cli/internal/output"
11+
"github.com/UpCloudLtd/upcloud-cli/internal/mockexecute"
1312

1413
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud"
1514
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
16-
"github.com/gemalto/flume"
1715
"github.com/stretchr/testify/assert"
1816
)
1917

@@ -114,22 +112,17 @@ func TestShowCommand(t *testing.T) {
114112
}
115113

116114
conf := config.New()
117-
// force human output
118-
conf.Viper().Set(config.KeyOutput, config.ValueOutputHuman)
119-
120115
command := commands.BuildCommand(ShowCommand(), nil, conf)
121116

122117
// get resolver to initialize command cache
123118
_, err := command.(*showCommand).Get(&mService)
124119
if err != nil {
125120
t.Fatal(err)
126121
}
127-
res, err := command.(commands.MultipleArgumentCommand).Execute(commands.NewExecutor(conf, &mService, flume.New("test")), network.UUID)
128122

129-
assert.Nil(t, err)
123+
command.Cobra().SetArgs([]string{network.UUID})
124+
output, err := mockexecute.MockExecute(command, &mService, conf)
130125

131-
buf := bytes.NewBuffer(nil)
132-
err = output.Render(buf, conf, res)
133126
assert.NoError(t, err)
134-
assert.Equal(t, expected, buf.String())
127+
assert.Equal(t, expected, output)
135128
}

0 commit comments

Comments
 (0)