Skip to content

Commit 38992fa

Browse files
committed
migrate to urfave/cli
1 parent 08e26ec commit 38992fa

5 files changed

Lines changed: 100 additions & 134 deletions

File tree

cmd/src/abc.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,30 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
6-
)
4+
"context"
75

8-
var abcCommands commander
6+
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/urfave/cli/v3"
8+
)
99

10-
func init() {
11-
usage := `'src abc' is a tool that manages agentic batch changes.
10+
var abcCommand = clicompat.WithLegacyHelp(&cli.Command{
11+
Name: "abc",
12+
Usage: "manages agentic batch changes",
13+
UsageText: `'src abc' is a tool that manages agentic batch changes.
1214
1315
Usage:
1416
1517
src abc command [command options]
1618
17-
The commands are:
18-
19-
variables manage workflow instance variables
20-
21-
Use "src abc [command] -h" for more information about a command.
22-
`
23-
24-
flagSet := flag.NewFlagSet("abc", flag.ExitOnError)
25-
usageFunc := func() {
26-
fmt.Println(usage)
27-
}
28-
handler := func(args []string) error {
29-
abcCommands.run(flagSet, "src abc", usage, args)
19+
The commands are:`,
20+
Description: `Use "src abc [command] -h" for more information about a command.`,
21+
HideHelpCommand: true,
22+
HideVersion: true,
23+
Commands: []*cli.Command{
24+
abcVariablesCommand,
25+
},
26+
Action: func(ctx context.Context, c *cli.Command) error {
27+
cli.HelpPrinter(c.Root().Writer, c.CustomRootCommandHelpTemplate, c)
3028
return nil
31-
}
32-
33-
commands = append(commands, &command{
34-
flagSet: flagSet,
35-
handler: handler,
36-
usageFunc: usageFunc,
37-
})
38-
}
29+
},
30+
})

cmd/src/abc_variables.go

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
6-
)
4+
"context"
75

8-
var abcVariablesCommands commander
6+
"github.com/sourcegraph/src-cli/internal/clicompat"
7+
"github.com/urfave/cli/v3"
8+
)
99

10-
func init() {
11-
usage := `'src abc variables' is a tool that manages workflow instance variables on agentic batch changes.
10+
var abcVariablesCommand = clicompat.WithLegacyHelp(&cli.Command{
11+
Name: "variables",
12+
Usage: "manage workflow instance variables",
13+
UsageText: `'src abc variables' is a tool that manages workflow instance variables on agentic batch changes.
1214
1315
Usage:
1416
1517
src abc variables command [command options]
1618
17-
The commands are:
18-
19-
set set workflow instance variables
20-
delete delete workflow instance variables
21-
22-
Use "src abc variables [command] -h" for more information about a command.
23-
`
24-
25-
flagSet := flag.NewFlagSet("variables", flag.ExitOnError)
26-
usageFunc := func() {
27-
fmt.Println(usage)
28-
}
29-
handler := func(args []string) error {
30-
abcVariablesCommands.run(flagSet, "src abc variables", usage, args)
19+
The commands are:`,
20+
Description: `Use "src abc variables [command] -h" for more information about a command.`,
21+
HideHelpCommand: true,
22+
HideVersion: true,
23+
Commands: []*cli.Command{
24+
abcVariablesSetCommand,
25+
abcVariablesDeleteCommand,
26+
},
27+
Action: func(ctx context.Context, c *cli.Command) error {
28+
cli.HelpPrinter(c.Root().Writer, c.CustomRootCommandHelpTemplate, c)
3129
return nil
32-
}
33-
34-
abcCommands = append(abcCommands, &command{
35-
flagSet: flagSet,
36-
handler: handler,
37-
usageFunc: usageFunc,
38-
})
39-
}
30+
},
31+
})

cmd/src/abc_variables_delete.go

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,43 @@ package main
22

33
import (
44
"context"
5-
"flag"
65
"fmt"
76

8-
"github.com/sourcegraph/src-cli/internal/api"
7+
"github.com/sourcegraph/src-cli/internal/clicompat"
98
"github.com/sourcegraph/src-cli/internal/cmderrors"
9+
"github.com/urfave/cli/v3"
1010
)
1111

12-
func init() {
13-
usage := `
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+
1419
Examples:
1520
1621
Delete a variable from a workflow instance:
1722
18-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
23+
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval
1924
2025
Delete multiple variables in one request:
2126
22-
$ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints
23-
`
24-
25-
flagSet := flag.NewFlagSet("delete", flag.ExitOnError)
26-
var variableArgs abcVariableArgs
27-
flagSet.Var(&variableArgs, "var", "Variable name to delete. Repeat for multiple names.")
28-
usageFunc := func() {
29-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src abc variables %s':\n", flagSet.Name())
30-
flagSet.PrintDefaults()
31-
fmt.Println(usage)
32-
}
33-
apiFlags := api.NewFlags(flagSet)
34-
35-
handler := func(args []string) error {
36-
if len(args) == 0 {
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 {
3737
return cmderrors.Usage("must provide a workflow instance ID")
3838
}
3939

40-
instanceID := args[0]
41-
variableArgs = nil
42-
if err := flagSet.Parse(args[1:]); err != nil {
43-
return err
44-
}
45-
46-
variableNames, err := parseABCVariableNames(flagSet.Args(), variableArgs)
40+
instanceID := c.Args().First()
41+
variableNames, err := parseABCVariableNames(c.Args().Tail(), abcVariableArgs(c.StringSlice("var")))
4742
if err != nil {
4843
return err
4944
}
@@ -56,25 +51,20 @@ Examples:
5651
})
5752
}
5853

59-
client := cfg.apiClient(apiFlags, flagSet.Output())
60-
if err := updateABCWorkflowInstanceVariables(context.Background(), client, instanceID, variables); err != nil {
54+
apiFlags := clicompat.APIFlagsFromCmd(c)
55+
client := cfg.apiClient(apiFlags, c.Writer)
56+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil {
6157
return err
6258
}
6359

6460
if apiFlags.GetCurl() {
6561
return nil
6662
}
6763

68-
fmt.Printf("Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
64+
fmt.Fprintf(c.Writer, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID)
6965
return nil
70-
}
71-
72-
abcVariablesCommands = append(abcVariablesCommands, &command{
73-
flagSet: flagSet,
74-
handler: handler,
75-
usageFunc: usageFunc,
76-
})
77-
}
66+
},
67+
})
7868

7969
func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) {
8070
variableNames := append([]string{}, positional...)

cmd/src/abc_variables_set.go

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"bytes"
55
"context"
66
"encoding/json"
7-
"flag"
87
"fmt"
98
"strings"
109

1110
"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"
1314
)
1415

1516
const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkflowInstanceVariables(
@@ -21,47 +22,42 @@ const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkfl
2122
}
2223
}`
2324

24-
func init() {
25-
usage := `
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+
2632
Examples:
2733
2834
Set a string variable on a workflow instance:
2935
30-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
36+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria"
3137
3238
Set multiple variables in one request:
3339
34-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
40+
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]'
3541
3642
Set a structured JSON value:
3743
38-
$ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]'
39-
40-
Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.
41-
`
42-
43-
flagSet := flag.NewFlagSet("set", flag.ExitOnError)
44-
var variableArgs abcVariableArgs
45-
flagSet.Var(&variableArgs, "var", "Variable assignment in <name>=<value> form. Repeat to set multiple variables.")
46-
usageFunc := func() {
47-
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src abc variables %s':\n", flagSet.Name())
48-
flagSet.PrintDefaults()
49-
fmt.Println(usage)
50-
}
51-
apiFlags := api.NewFlags(flagSet)
52-
53-
handler := func(args []string) error {
54-
if len(args) == 0 {
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 {
5556
return cmderrors.Usage("must provide a workflow instance ID")
5657
}
5758

58-
instanceID := args[0]
59-
variableArgs = nil
60-
if err := flagSet.Parse(args[1:]); err != nil {
61-
return err
62-
}
63-
64-
variables, err := parseABCVariables(flagSet.Args(), variableArgs)
59+
instanceID := c.Args().First()
60+
variables, err := parseABCVariables(c.Args().Tail(), abcVariableArgs(c.StringSlice("var")))
6561
if err != nil {
6662
return err
6763
}
@@ -74,8 +70,9 @@ Values are interpreted as JSON literals when valid. Otherwise they are sent as p
7470
})
7571
}
7672

77-
client := cfg.apiClient(apiFlags, flagSet.Output())
78-
if err := updateABCWorkflowInstanceVariables(context.Background(), client, instanceID, graphqlVariables); err != nil {
73+
apiFlags := clicompat.APIFlagsFromCmd(c)
74+
client := cfg.apiClient(apiFlags, c.Writer)
75+
if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil {
7976
return err
8077
}
8178

@@ -84,20 +81,14 @@ Values are interpreted as JSON literals when valid. Otherwise they are sent as p
8481
}
8582

8683
if len(variables) == 1 {
87-
fmt.Printf("Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID)
84+
fmt.Fprintf(c.Writer, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID)
8885
return nil
8986
}
9087

91-
fmt.Printf("Updated %d variables on workflow instance %q.\n", len(variables), instanceID)
88+
fmt.Fprintf(c.Writer, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID)
9289
return nil
93-
}
94-
95-
abcVariablesCommands = append(abcVariablesCommands, &command{
96-
flagSet: flagSet,
97-
handler: handler,
98-
usageFunc: usageFunc,
99-
})
100-
}
90+
},
91+
})
10192

10293
type abcVariableArgs []string
10394

cmd/src/run_migration_compat.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
var migratedCommands = map[string]*cli.Command{
19+
"abc": abcCommand,
1920
"version": versionCommand,
2021
}
2122

0 commit comments

Comments
 (0)