Skip to content

Commit adae048

Browse files
committed
delete support multiple vars
1 parent ef3c689 commit adae048

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

cmd/src/abc_variables_delete.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ Examples:
1616
Delete a variable from a workflow instance:
1717
1818
$ src abc QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== variables delete approval
19+
20+
Delete multiple variables in one request:
21+
22+
$ src abc QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== variables delete --var approval --var checkpoints
1923
`
2024

2125
flagSet := flag.NewFlagSet("delete", flag.ExitOnError)
26+
var variableArgs abcVariableArgs
27+
flagSet.Var(&variableArgs, "var", "Variable name to delete. Repeat to delete multiple variables.")
2228
usageFunc := func() {
2329
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src abc <workflow-instance-id> variables %s':\n", flagSet.Name())
2430
flagSet.PrintDefaults()
@@ -32,27 +38,39 @@ Examples:
3238
}
3339

3440
instanceID := args[0]
41+
variableArgs = nil
3542
if err := flagSet.Parse(args[1:]); err != nil {
3643
return err
3744
}
38-
if flagSet.NArg() != 1 {
39-
return cmderrors.Usage("must provide exactly one variable name")
45+
46+
variableNames, err := parseABCVariableNames(flagSet.Args(), variableArgs)
47+
if err != nil {
48+
return err
49+
}
50+
51+
variables := make([]map[string]string, 0, len(variableNames))
52+
for _, key := range variableNames {
53+
variables = append(variables, map[string]string{
54+
"key": key,
55+
"value": "null",
56+
})
4057
}
4158

42-
key := flagSet.Arg(0)
4359
client := cfg.apiClient(apiFlags, flagSet.Output())
44-
if err := updateABCWorkflowInstanceVariables(context.Background(), client, instanceID, []map[string]string{{
45-
"key": key,
46-
"value": "null",
47-
}}); err != nil {
60+
if err := updateABCWorkflowInstanceVariables(context.Background(), client, instanceID, variables); err != nil {
4861
return err
4962
}
5063

5164
if apiFlags.GetCurl() {
5265
return nil
5366
}
5467

55-
fmt.Printf("Removed variable %q from workflow instance %q.\n", key, instanceID)
68+
if len(variableNames) == 1 {
69+
fmt.Printf("Removed variable %q from workflow instance %q.\n", variableNames[0], instanceID)
70+
return nil
71+
}
72+
73+
fmt.Printf("Removed %d variables from workflow instance %q.\n", len(variableNames), instanceID)
5674
return nil
5775
}
5876

@@ -62,3 +80,19 @@ Examples:
6280
usageFunc: usageFunc,
6381
})
6482
}
83+
84+
func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) {
85+
variableNames := append([]string{}, positional...)
86+
variableNames = append(variableNames, flagged...)
87+
if len(variableNames) == 0 {
88+
return nil, cmderrors.Usage("must provide at least one variable name")
89+
}
90+
91+
for _, name := range variableNames {
92+
if name == "" {
93+
return nil, cmderrors.Usage("variable names must not be empty")
94+
}
95+
}
96+
97+
return variableNames, nil
98+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/sourcegraph/src-cli/internal/cmderrors"
7+
)
8+
9+
func TestParseABCVariableNames(t *testing.T) {
10+
t.Parallel()
11+
12+
variableNames, err := parseABCVariableNames(
13+
[]string{"approval"},
14+
abcVariableArgs{"checkpoints", "prompt"},
15+
)
16+
if err != nil {
17+
t.Fatalf("parseABCVariableNames returned error: %v", err)
18+
}
19+
20+
if len(variableNames) != 3 {
21+
t.Fatalf("len(variableNames) = %d, want 3", len(variableNames))
22+
}
23+
if variableNames[0] != "approval" || variableNames[1] != "checkpoints" || variableNames[2] != "prompt" {
24+
t.Fatalf("variableNames = %#v, want [approval checkpoints prompt]", variableNames)
25+
}
26+
}
27+
28+
func TestParseABCVariableNamesRequiresAtLeastOneName(t *testing.T) {
29+
t.Parallel()
30+
31+
_, err := parseABCVariableNames(nil, nil)
32+
if err == nil {
33+
t.Fatal("parseABCVariableNames returned nil error, want usage error")
34+
}
35+
if _, ok := err.(*cmderrors.UsageError); !ok {
36+
t.Fatalf("parseABCVariableNames error = %T, want *cmderrors.UsageError", err)
37+
}
38+
}
39+
40+
func TestParseABCVariableNamesRejectsEmptyNames(t *testing.T) {
41+
t.Parallel()
42+
43+
_, err := parseABCVariableNames([]string{"approval", ""}, nil)
44+
if err == nil {
45+
t.Fatal("parseABCVariableNames returned nil error, want usage error")
46+
}
47+
if _, ok := err.(*cmderrors.UsageError); !ok {
48+
t.Fatalf("parseABCVariableNames error = %T, want *cmderrors.UsageError", err)
49+
}
50+
}

0 commit comments

Comments
 (0)