Skip to content

Commit 8f68061

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

3 files changed

Lines changed: 154 additions & 148 deletions

File tree

cmd/src/abc.go

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,106 @@ import (
44
"context"
55

66
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/sourcegraph/src-cli/internal/cmderrors"
78
"github.com/urfave/cli/v3"
89
)
910

10-
var abcCommand = &cli.Command{
11+
var abcCommand = clicompat.Wrap(&cli.Command{
1112
Name: "abc",
1213
Usage: "manages agentic batch changes",
13-
UsageText: `'src abc' is a tool that manages agentic batch changes.
14+
Commands: []*cli.Command{
15+
clicompat.Wrap(&cli.Command{
16+
Name: "variables",
17+
Usage: "manage workflow instance variables",
18+
Commands: []*cli.Command{
19+
abcVariablesSetCommand,
20+
abcVariablesDeleteCommand,
21+
},
22+
Action: func(ctx context.Context, cmd *cli.Command) error {
23+
return cli.ShowSubcommandHelp(cmd)
24+
},
25+
}),
26+
},
27+
Action: func(ctx context.Context, cmd *cli.Command) error {
28+
return cli.ShowSubcommandHelp(cmd)
29+
},
30+
})
1431

15-
Usage:
32+
var abcVariablesSetCommand = clicompat.Wrap(&cli.Command{
33+
Name: "set",
34+
UsageText: "src abc variables set [options] <workflow-instance-id> [<name>=<value> ...]",
35+
Usage: "Set variables on a workflow instance",
36+
Description: `
37+
Set workflow instance variables
1638
17-
src abc command [command options]
39+
Examples:
1840
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,
24-
Commands: []*cli.Command{
25-
abcVariablesCommand,
41+
Set a string variable on a workflow instance:
42+
43+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
44+
45+
Set multiple variables in one request:
46+
47+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
48+
49+
Set a structured JSON value:
50+
51+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]'
52+
53+
NOTE: Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.
54+
`,
55+
Flags: clicompat.WithAPIFlags(
56+
&cli.StringSliceFlag{
57+
Name: "var",
58+
Usage: "Variable assignment in <name>=<value> form. Repeat to set multiple variables.",
59+
},
60+
),
61+
Action: func(ctx context.Context, cmd *cli.Command) error {
62+
if !cmd.Args().Present() {
63+
return cmderrors.Usage("must provide a workflow instance ID")
64+
}
65+
66+
instanceID := cmd.Args().First()
67+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
68+
return runABCVariablesSet(ctx, client, instanceID, cmd.Args().Tail(), abcVariableArgs(cmd.StringSlice("var")), cmd.Writer, cmd.Bool("get-curl"))
2669
},
27-
Action: func(_ context.Context, c *cli.Command) error {
28-
return cli.ShowSubcommandHelp(c)
70+
})
71+
72+
var abcVariablesDeleteCommand = clicompat.Wrap(&cli.Command{
73+
Name: "delete",
74+
Usage: "Delete variables on a workflow instance",
75+
UsageText: "src abc variables delete [options] <workflow-instance-id> [<name> ...]",
76+
Description: `
77+
Delete workflow instance variables
78+
79+
Examples:
80+
81+
Delete a variable from a workflow instance:
82+
83+
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
84+
85+
Delete multiple variables in one request:
86+
87+
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints
88+
`,
89+
Flags: clicompat.WithAPIFlags(
90+
&cli.StringSliceFlag{
91+
Name: "var",
92+
Usage: "Variable name to delete. Repeat for multiple names.",
93+
},
94+
),
95+
Action: func(ctx context.Context, cmd *cli.Command) error {
96+
if !cmd.Args().Present() {
97+
return cmderrors.Usage("must provide a workflow instance ID")
98+
}
99+
100+
instanceID := cmd.Args().First()
101+
client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer)
102+
varArgs := abcVariableArgs(cmd.StringSlice("var"))
103+
104+
if len(varArgs) == 0 {
105+
return cmderrors.Usage("must provide at least one variable name")
106+
}
107+
return runABCVariablesDelete(ctx, client, instanceID, cmd.Args().Tail(), varArgs, cmd.Writer, cmd.Bool("get-curl"))
29108
},
30-
}
109+
})

cmd/src/abc_variables_delete.go

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

7-
"github.com/sourcegraph/src-cli/internal/clicompat"
9+
"github.com/sourcegraph/src-cli/internal/api"
810
"github.com/sourcegraph/src-cli/internal/cmderrors"
9-
"github.com/urfave/cli/v3"
1011
)
1112

12-
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> ...]
18-
19-
Examples:
20-
21-
Delete a variable from a workflow instance:
22-
23-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
24-
25-
Delete multiple variables in one request:
26-
27-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints`,
28-
DisableSliceFlagSeparator: true,
29-
Flags: clicompat.WithAPIFlags(
30-
&cli.StringSliceFlag{
31-
Name: "var",
32-
Usage: "Variable name to delete. Repeat for multiple names.",
33-
},
34-
),
35-
Action: func(ctx context.Context, c *cli.Command) error {
36-
if c.NArg() == 0 {
37-
return cmderrors.Usage("must provide a workflow instance ID")
38-
}
39-
40-
instanceID := c.Args().First()
41-
variableNames, err := parseABCVariableNames(c.Args().Tail(), abcVariableArgs(c.StringSlice("var")))
42-
if err != nil {
43-
return err
44-
}
13+
func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) {
14+
variableNames := append([]string{}, positional...)
15+
variableNames = append(variableNames, flagged...)
4516

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-
}
17+
if slices.Contains(variableNames, "") {
18+
return nil, cmderrors.Usage("variable names must not be empty")
19+
}
5320

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-
}
21+
return variableNames, nil
22+
}
5923

60-
if apiFlags.GetCurl() {
61-
return nil
62-
}
24+
func runABCVariablesDelete(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error {
25+
variableNames, err := parseABCVariableNames(positional, flagged)
26+
if err != nil {
27+
return err
28+
}
6329

64-
fmt.Fprintf(c.Writer, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
65-
return nil
66-
},
67-
})
30+
variables := make([]map[string]string, 0, len(variableNames))
31+
for _, key := range variableNames {
32+
variables = append(variables, map[string]string{
33+
"key": key,
34+
"value": "null",
35+
})
36+
}
6837

69-
func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) {
70-
variableNames := append([]string{}, positional...)
71-
variableNames = append(variableNames, flagged...)
72-
if len(variableNames) == 0 {
73-
return nil, cmderrors.Usage("must provide at least one variable name")
38+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil {
39+
return err
7440
}
7541

76-
for _, name := range variableNames {
77-
if name == "" {
78-
return nil, cmderrors.Usage("variable names must not be empty")
79-
}
42+
if getCurl {
43+
return nil
8044
}
8145

82-
return variableNames, nil
46+
_, err = fmt.Fprintf(output, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
47+
return err
8348
}

cmd/src/abc_variables_set.go

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

1011
"github.com/sourcegraph/src-cli/internal/api"
11-
"github.com/sourcegraph/src-cli/internal/clicompat"
1212
"github.com/sourcegraph/src-cli/internal/cmderrors"
13-
"github.com/urfave/cli/v3"
1413
)
1514

1615
const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkflowInstanceVariables(
@@ -22,74 +21,6 @@ const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkfl
2221
}
2322
}`
2423

25-
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> ...]
31-
32-
Examples:
33-
34-
Set a string variable on a workflow instance:
35-
36-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
37-
38-
Set multiple variables in one request:
39-
40-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
41-
42-
Set a structured JSON value:
43-
44-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]'
45-
46-
Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.`,
47-
DisableSliceFlagSeparator: true,
48-
Flags: clicompat.WithAPIFlags(
49-
&cli.StringSliceFlag{
50-
Name: "var",
51-
Usage: "Variable assignment in <name>=<value> form. Repeat to set multiple variables.",
52-
},
53-
),
54-
Action: func(ctx context.Context, c *cli.Command) error {
55-
if c.NArg() == 0 {
56-
return cmderrors.Usage("must provide a workflow instance ID")
57-
}
58-
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
90-
},
91-
})
92-
9324
type abcVariableArgs []string
9425

9526
func (a *abcVariableArgs) String() string {
@@ -142,6 +73,37 @@ func parseABCVariable(raw string) (abcVariable, error) {
14273
return abcVariable{Key: name, Value: value}, nil
14374
}
14475

76+
func runABCVariablesSet(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error {
77+
variables, err := parseABCVariables(positional, flagged)
78+
if err != nil {
79+
return err
80+
}
81+
82+
graphqlVariables := make([]map[string]string, 0, len(variables))
83+
for _, variable := range variables {
84+
graphqlVariables = append(graphqlVariables, map[string]string{
85+
"key": variable.Key,
86+
"value": variable.Value,
87+
})
88+
}
89+
90+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil {
91+
return err
92+
}
93+
94+
if getCurl {
95+
return nil
96+
}
97+
98+
if len(variables) == 1 {
99+
_, err = fmt.Fprintf(output, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID)
100+
return err
101+
}
102+
103+
_, err = fmt.Fprintf(output, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID)
104+
return err
105+
}
106+
145107
func updateABCWorkflowInstanceVariables(ctx context.Context, client api.Client, instanceID string, variables []map[string]string) error {
146108
var result struct {
147109
UpdateAgenticWorkflowInstanceVariables struct {

0 commit comments

Comments
 (0)