Skip to content

Commit 4fd35a5

Browse files
committed
core: set verb handles all positional arguments together
1 parent 29f059d commit 4fd35a5

7 files changed

+170
-2
lines changed

internal/core/cobra_utils.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,31 @@ func cobraRun(ctx context.Context, cmd *Command) func(*cobra.Command, []string)
7474

7575
results := MultiResults(nil)
7676
rawArgs = rawArgs.RemoveAllPositional()
77-
for _, positionalArg := range positionalArgs {
78-
rawArgsWithPositional := rawArgs.Add(positionalArgSpec.Name, positionalArg)
7977

78+
if cmd.Verb == "set" {
79+
argNameWithIndex := fmt.Sprintf("%s.%d", positionalArgSpec.Name, 0)
80+
rawArgsWithPositional := rawArgs.Add(argNameWithIndex, positionalArgs[0])
81+
for i := 1; i < len(positionalArgs); i++ {
82+
argNameWithIndex = fmt.Sprintf("%s.%d", positionalArgSpec.Name, i)
83+
rawArgsWithPositional = rawArgsWithPositional.Add(argNameWithIndex, positionalArgs[i])
84+
}
8085
result, err := run(ctx, cobraCmd, cmd, rawArgsWithPositional)
8186
if err != nil {
8287
return err
8388
}
8489

8590
results = append(results, result)
91+
} else {
92+
for _, positionalArg := range positionalArgs {
93+
rawArgsWithPositional := rawArgs.Add(positionalArgSpec.Name, positionalArg)
94+
95+
result, err := run(ctx, cobraCmd, cmd, rawArgsWithPositional)
96+
if err != nil {
97+
return err
98+
}
99+
100+
results = append(results, result)
101+
}
86102
}
87103
// If only one positional parameter was provided we return the result directly instead of
88104
// an array of results

internal/core/cobra_utils_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,31 @@ func Test_PositionalArg(t *testing.T) {
246246
core.TestCheckGolden(),
247247
),
248248
}))
249+
250+
t.Run("set verb with one positional", core.Test(&core.TestConfig{
251+
Commands: testGetCommands(),
252+
Cmd: "scw test positional set pos1 tag=tag1",
253+
Check: core.TestCheckCombine(
254+
core.TestCheckExitCode(0),
255+
core.TestCheckGolden(),
256+
),
257+
}))
258+
259+
t.Run("set verb with multi positional", core.Test(&core.TestConfig{
260+
Commands: testGetCommands(),
261+
Cmd: "scw test positional set pos1 pos2 pos3 tag=tag1",
262+
Check: core.TestCheckCombine(
263+
core.TestCheckExitCode(0),
264+
core.TestCheckGolden(),
265+
),
266+
}))
267+
268+
t.Run("set verb with no positional", core.Test(&core.TestConfig{
269+
Commands: testGetCommands(),
270+
Cmd: "scw test positional set tag=tag1",
271+
Check: core.TestCheckCombine(
272+
core.TestCheckExitCode(0),
273+
core.TestCheckGolden(),
274+
),
275+
}))
249276
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
NameID set
4+
Tag tag1
5+
NameID pos1
6+
Tag tag1
7+
NameID pos2
8+
Tag tag1
9+
NameID pos3
10+
Tag tag1
11+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
12+
[
13+
{
14+
"NameID": "set",
15+
"Tag": "tag1"
16+
},
17+
{
18+
"NameID": "pos1",
19+
"Tag": "tag1"
20+
},
21+
{
22+
"NameID": "pos2",
23+
"Tag": "tag1"
24+
},
25+
{
26+
"NameID": "pos3",
27+
"Tag": "tag1"
28+
}
29+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
NameID set
4+
Tag tag1
5+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
6+
{
7+
"NameID": "set",
8+
"Tag": "tag1"
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
NameID set
4+
Tag tag1
5+
NameID pos1
6+
Tag tag1
7+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
8+
[
9+
{
10+
"NameID": "set",
11+
"Tag": "tag1"
12+
},
13+
{
14+
"NameID": "pos1",
15+
"Tag": "tag1"
16+
}
17+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: '{"access_key":"SCWHJNSK2028RFV4SE6K", "secret_key":null, "description":"lmarabese
6+
IAM API key", "created_at":"2023-02-07T12:50:30.653520Z", "updated_at":"2023-02-07T12:50:30.653520Z",
7+
"expires_at":null, "default_project_id":"fa1e3217-dc80-42ac-85c3-3f034b78b552",
8+
"editable":true, "creation_ip":"51.159.46.153", "user_id":"77e7fa85-f305-4ea2-afea-09571a039336"}'
9+
form: {}
10+
headers:
11+
User-Agent:
12+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.22.0; linux; amd64) cli-e2e-test
13+
url: https://api.scaleway.com/iam/v1alpha1/api-keys/SCWXXXXXXXXXXXXXXXXX
14+
method: GET
15+
response:
16+
body: '{"access_key":"SCWHJNSK2028RFV4SE6K", "secret_key":null, "description":"lmarabese
17+
IAM API key", "created_at":"2023-02-07T12:50:30.653520Z", "updated_at":"2023-02-07T12:50:30.653520Z",
18+
"expires_at":null, "default_project_id":"fa1e3217-dc80-42ac-85c3-3f034b78b552",
19+
"editable":true, "creation_ip":"51.159.46.153", "user_id":"77e7fa85-f305-4ea2-afea-09571a039336"}'
20+
headers:
21+
Content-Length:
22+
- "361"
23+
Content-Security-Policy:
24+
- default-src 'none'; frame-ancestors 'none'
25+
Content-Type:
26+
- application/json
27+
Date:
28+
- Thu, 28 Mar 2024 14:38:53 GMT
29+
Server:
30+
- Scaleway API Gateway (fr-par-3;edge02)
31+
Strict-Transport-Security:
32+
- max-age=63072000
33+
X-Content-Type-Options:
34+
- nosniff
35+
X-Frame-Options:
36+
- DENY
37+
X-Request-Id:
38+
- 75f24c8b-f77d-49f5-80ac-47e7a7cd3988
39+
status: 200 OK
40+
code: 200
41+
duration: ""
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
NameID set
4+
Tag tag1
5+
NameID pos1
6+
Tag tag1
7+
NameID pos2
8+
Tag tag1
9+
NameID pos3
10+
Tag tag1
11+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
12+
[
13+
{
14+
"NameID": "set",
15+
"Tag": "tag1"
16+
},
17+
{
18+
"NameID": "pos1",
19+
"Tag": "tag1"
20+
},
21+
{
22+
"NameID": "pos2",
23+
"Tag": "tag1"
24+
},
25+
{
26+
"NameID": "pos3",
27+
"Tag": "tag1"
28+
}
29+
]

0 commit comments

Comments
 (0)