Skip to content

Commit d143b42

Browse files
committed
migrate abc command to use urfave/cli
1 parent e2bd681 commit d143b42

4 files changed

Lines changed: 109 additions & 131 deletions

File tree

cmd/src/abc.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@ import (
77
"github.com/urfave/cli/v3"
88
)
99

10-
var abcCommand = &cli.Command{
10+
var abcCommand = clicompat.Wrap(&cli.Command{
1111
Name: "abc",
1212
Usage: "manages agentic batch changes",
13-
UsageText: `'src abc' is a tool that manages agentic batch changes.
14-
15-
Usage:
16-
17-
src abc command [command options]
18-
19-
The commands are:`,
20-
OnUsageError: clicompat.OnUsageError,
21-
Description: `Use "src abc [command] -h" for more information about a command.`,
22-
HideHelpCommand: true,
23-
HideVersion: true,
2413
Commands: []*cli.Command{
25-
abcVariablesCommand,
14+
clicompat.Wrap(&cli.Command{
15+
Name: "variables",
16+
Usage: "manage workflow instance variables",
17+
Commands: []*cli.Command{
18+
abcVariablesSetCommand,
19+
abcVariablesDeleteCommand,
20+
},
21+
Action: func(ctx context.Context, cmd *cli.Command) error {
22+
return cli.ShowSubcommandHelp(cmd)
23+
},
24+
}),
2625
},
27-
Action: func(_ context.Context, c *cli.Command) error {
28-
return cli.ShowSubcommandHelp(c)
26+
Action: func(ctx context.Context, cmd *cli.Command) error {
27+
return cli.ShowSubcommandHelp(cmd)
2928
},
30-
}
29+
})

cmd/src/abc_variables.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

cmd/src/abc_variables_delete.go

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,87 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"slices"
68

9+
"github.com/sourcegraph/src-cli/internal/api"
710
"github.com/sourcegraph/src-cli/internal/clicompat"
811
"github.com/sourcegraph/src-cli/internal/cmderrors"
912
"github.com/urfave/cli/v3"
1013
)
1114

1215
var abcVariablesDeleteCommand = clicompat.Wrap(&cli.Command{
13-
Name: "delete",
14-
Usage: "delete workflow instance variables",
15-
Description: `Usage:
16-
17-
src abc variables delete [command options] <workflow-instance-id> [<name> ...]
16+
Name: "delete",
17+
Usage: "Delete variables on a workflow instance",
18+
UsageText: "src abc variables delete [options] <workflow-instance-id> [<name> ...]",
19+
Description: `
20+
Delete workflow instance variables
1821
1922
Examples:
2023
2124
Delete a variable from a workflow instance:
2225
23-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
26+
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
2427
2528
Delete multiple variables in one request:
2629
27-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints`,
28-
DisableSliceFlagSeparator: true,
30+
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints
31+
`,
2932
Flags: clicompat.WithAPIFlags(
3033
&cli.StringSliceFlag{
3134
Name: "var",
3235
Usage: "Variable name to delete. Repeat for multiple names.",
3336
},
3437
),
35-
Action: func(ctx context.Context, c *cli.Command) error {
36-
if c.NArg() == 0 {
38+
Action: func(ctx context.Context, cmd *cli.Command) error {
39+
if !cmd.Args().Present() {
3740
return cmderrors.Usage("must provide a workflow instance ID")
3841
}
3942

40-
instanceID := c.Args().First()
41-
variableNames, err := parseABCVariableNames(c.Args().Tail(), abcVariableArgs(c.StringSlice("var")))
42-
if err != nil {
43-
return err
44-
}
45-
46-
variables := make([]map[string]string, 0, len(variableNames))
47-
for _, key := range variableNames {
48-
variables = append(variables, map[string]string{
49-
"key": key,
50-
"value": "null",
51-
})
52-
}
53-
54-
apiFlags := clicompat.APIFlagsFromCmd(c)
55-
client := cfg.apiClient(apiFlags, c.Writer)
56-
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil {
57-
return err
58-
}
43+
instanceID := cmd.Args().First()
44+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
45+
varArgs := abcVariableArgs(cmd.StringSlice("var"))
5946

60-
if apiFlags.GetCurl() {
61-
return nil
47+
if len(varArgs) == 0 {
48+
return cmderrors.Usage("must provide at least one variable name")
6249
}
63-
64-
fmt.Fprintf(c.Writer, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
65-
return nil
50+
return runABCVariablesDelete(ctx, client, instanceID, cmd.Args().Tail(), varArgs, cmd.Writer, cmd.Bool("get-curl"))
6651
},
6752
})
6853

6954
func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) {
7055
variableNames := append([]string{}, positional...)
7156
variableNames = append(variableNames, flagged...)
72-
if len(variableNames) == 0 {
73-
return nil, cmderrors.Usage("must provide at least one variable name")
74-
}
7557

76-
for _, name := range variableNames {
77-
if name == "" {
78-
return nil, cmderrors.Usage("variable names must not be empty")
79-
}
58+
if slices.Contains(variableNames, "") {
59+
return nil, cmderrors.Usage("variable names must not be empty")
8060
}
8161

8262
return variableNames, nil
8363
}
64+
65+
func runABCVariablesDelete(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error {
66+
variableNames, err := parseABCVariableNames(positional, flagged)
67+
if err != nil {
68+
return err
69+
}
70+
71+
variables := make([]map[string]string, 0, len(variableNames))
72+
for _, key := range variableNames {
73+
variables = append(variables, map[string]string{
74+
"key": key,
75+
"value": "null",
76+
})
77+
}
78+
79+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil {
80+
return err
81+
}
82+
83+
if getCurl {
84+
return nil
85+
}
86+
87+
_, err = fmt.Fprintf(output, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
88+
return err
89+
}

cmd/src/abc_variables_set.go

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"strings"
910

1011
"github.com/sourcegraph/src-cli/internal/api"
@@ -23,70 +24,42 @@ const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkfl
2324
}`
2425

2526
var abcVariablesSetCommand = clicompat.Wrap(&cli.Command{
26-
Name: "set",
27-
Usage: "set workflow instance variables",
28-
Description: `Usage:
29-
30-
src abc variables set [command options] <workflow-instance-id> [<name>=<value> ...]
27+
Name: "set",
28+
UsageText: "src abc variables set [options] <workflow-instance-id> [<name>=<value> ...]",
29+
Usage: "Set variables on a workflow instance",
30+
Description: `
31+
Set workflow instance variables
3132
3233
Examples:
3334
3435
Set a string variable on a workflow instance:
3536
36-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
37+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
3738
3839
Set multiple variables in one request:
3940
40-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
41+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
4142
4243
Set a structured JSON value:
4344
44-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]'
45+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]'
4546
46-
Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.`,
47-
DisableSliceFlagSeparator: true,
47+
NOTE: Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.
48+
`,
4849
Flags: clicompat.WithAPIFlags(
4950
&cli.StringSliceFlag{
5051
Name: "var",
5152
Usage: "Variable assignment in <name>=<value> form. Repeat to set multiple variables.",
5253
},
5354
),
54-
Action: func(ctx context.Context, c *cli.Command) error {
55-
if c.NArg() == 0 {
55+
Action: func(ctx context.Context, cmd *cli.Command) error {
56+
if !cmd.Args().Present() {
5657
return cmderrors.Usage("must provide a workflow instance ID")
5758
}
5859

59-
instanceID := c.Args().First()
60-
variables, err := parseABCVariables(c.Args().Tail(), abcVariableArgs(c.StringSlice("var")))
61-
if err != nil {
62-
return err
63-
}
64-
65-
graphqlVariables := make([]map[string]string, 0, len(variables))
66-
for _, variable := range variables {
67-
graphqlVariables = append(graphqlVariables, map[string]string{
68-
"key": variable.Key,
69-
"value": variable.Value,
70-
})
71-
}
72-
73-
apiFlags := clicompat.APIFlagsFromCmd(c)
74-
client := cfg.apiClient(apiFlags, c.Writer)
75-
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil {
76-
return err
77-
}
78-
79-
if apiFlags.GetCurl() {
80-
return nil
81-
}
82-
83-
if len(variables) == 1 {
84-
fmt.Fprintf(c.Writer, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID)
85-
return nil
86-
}
87-
88-
fmt.Fprintf(c.Writer, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID)
89-
return nil
60+
instanceID := cmd.Args().First()
61+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
62+
return runABCVariablesSet(ctx, client, instanceID, cmd.Args().Tail(), abcVariableArgs(cmd.StringSlice("var")), cmd.Writer, cmd.Bool("get-curl"))
9063
},
9164
})
9265

@@ -142,6 +115,37 @@ func parseABCVariable(raw string) (abcVariable, error) {
142115
return abcVariable{Key: name, Value: value}, nil
143116
}
144117

118+
func runABCVariablesSet(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error {
119+
variables, err := parseABCVariables(positional, flagged)
120+
if err != nil {
121+
return err
122+
}
123+
124+
graphqlVariables := make([]map[string]string, 0, len(variables))
125+
for _, variable := range variables {
126+
graphqlVariables = append(graphqlVariables, map[string]string{
127+
"key": variable.Key,
128+
"value": variable.Value,
129+
})
130+
}
131+
132+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil {
133+
return err
134+
}
135+
136+
if getCurl {
137+
return nil
138+
}
139+
140+
if len(variables) == 1 {
141+
_, err = fmt.Fprintf(output, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID)
142+
return err
143+
}
144+
145+
_, err = fmt.Fprintf(output, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID)
146+
return err
147+
}
148+
145149
func updateABCWorkflowInstanceVariables(ctx context.Context, client api.Client, instanceID string, variables []map[string]string) error {
146150
var result struct {
147151
UpdateAgenticWorkflowInstanceVariables struct {

0 commit comments

Comments
 (0)