From d143b4204796fa0e4902e899c95418edf834dcd3 Mon Sep 17 00:00:00 2001 From: William Bezuidenhout Date: Wed, 15 Apr 2026 17:00:41 +0200 Subject: [PATCH] migrate abc command to use urfave/cli --- cmd/src/abc.go | 31 ++++++------ cmd/src/abc_variables.go | 31 ------------ cmd/src/abc_variables_delete.go | 88 +++++++++++++++++--------------- cmd/src/abc_variables_set.go | 90 +++++++++++++++++---------------- 4 files changed, 109 insertions(+), 131 deletions(-) delete mode 100644 cmd/src/abc_variables.go diff --git a/cmd/src/abc.go b/cmd/src/abc.go index 80bf4d1c51..5d2b019556 100644 --- a/cmd/src/abc.go +++ b/cmd/src/abc.go @@ -7,24 +7,23 @@ import ( "github.com/urfave/cli/v3" ) -var abcCommand = &cli.Command{ +var abcCommand = clicompat.Wrap(&cli.Command{ Name: "abc", Usage: "manages agentic batch changes", - UsageText: `'src abc' is a tool that manages agentic batch changes. - -Usage: - - src abc command [command options] - -The commands are:`, - OnUsageError: clicompat.OnUsageError, - Description: `Use "src abc [command] -h" for more information about a command.`, - HideHelpCommand: true, - HideVersion: true, Commands: []*cli.Command{ - abcVariablesCommand, + clicompat.Wrap(&cli.Command{ + Name: "variables", + Usage: "manage workflow instance variables", + Commands: []*cli.Command{ + abcVariablesSetCommand, + abcVariablesDeleteCommand, + }, + Action: func(ctx context.Context, cmd *cli.Command) error { + return cli.ShowSubcommandHelp(cmd) + }, + }), }, - Action: func(_ context.Context, c *cli.Command) error { - return cli.ShowSubcommandHelp(c) + Action: func(ctx context.Context, cmd *cli.Command) error { + return cli.ShowSubcommandHelp(cmd) }, -} +}) diff --git a/cmd/src/abc_variables.go b/cmd/src/abc_variables.go deleted file mode 100644 index d1f9a61f12..0000000000 --- a/cmd/src/abc_variables.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "context" - - "github.com/sourcegraph/src-cli/internal/clicompat" - "github.com/urfave/cli/v3" -) - -var abcVariablesCommand = &cli.Command{ - Name: "variables", - Usage: "manage workflow instance variables", - UsageText: `'src abc variables' is a tool that manages workflow instance variables on agentic batch changes. - -Usage: - - src abc variables command [command options] - -The commands are:`, - OnUsageError: clicompat.OnUsageError, - Description: `Use "src abc variables [command] -h" for more information about a command.`, - HideHelpCommand: true, - HideVersion: true, - Commands: []*cli.Command{ - abcVariablesSetCommand, - abcVariablesDeleteCommand, - }, - Action: func(_ context.Context, c *cli.Command) error { - return cli.ShowSubcommandHelp(c) - }, -} diff --git a/cmd/src/abc_variables_delete.go b/cmd/src/abc_variables_delete.go index d888252955..76d1dd4567 100644 --- a/cmd/src/abc_variables_delete.go +++ b/cmd/src/abc_variables_delete.go @@ -3,81 +3,87 @@ package main import ( "context" "fmt" + "io" + "slices" + "github.com/sourcegraph/src-cli/internal/api" "github.com/sourcegraph/src-cli/internal/clicompat" "github.com/sourcegraph/src-cli/internal/cmderrors" "github.com/urfave/cli/v3" ) var abcVariablesDeleteCommand = clicompat.Wrap(&cli.Command{ - Name: "delete", - Usage: "delete workflow instance variables", - Description: `Usage: - - src abc variables delete [command options] [ ...] + Name: "delete", + Usage: "Delete variables on a workflow instance", + UsageText: "src abc variables delete [options] [ ...]", + Description: ` +Delete workflow instance variables Examples: Delete a variable from a workflow instance: - $ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval + $ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== approval Delete multiple variables in one request: - $ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints`, - DisableSliceFlagSeparator: true, + $ src abc variables delete QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var approval --var checkpoints +`, Flags: clicompat.WithAPIFlags( &cli.StringSliceFlag{ Name: "var", Usage: "Variable name to delete. Repeat for multiple names.", }, ), - Action: func(ctx context.Context, c *cli.Command) error { - if c.NArg() == 0 { + Action: func(ctx context.Context, cmd *cli.Command) error { + if !cmd.Args().Present() { return cmderrors.Usage("must provide a workflow instance ID") } - instanceID := c.Args().First() - variableNames, err := parseABCVariableNames(c.Args().Tail(), abcVariableArgs(c.StringSlice("var"))) - if err != nil { - return err - } - - variables := make([]map[string]string, 0, len(variableNames)) - for _, key := range variableNames { - variables = append(variables, map[string]string{ - "key": key, - "value": "null", - }) - } - - apiFlags := clicompat.APIFlagsFromCmd(c) - client := cfg.apiClient(apiFlags, c.Writer) - if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil { - return err - } + instanceID := cmd.Args().First() + client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer) + varArgs := abcVariableArgs(cmd.StringSlice("var")) - if apiFlags.GetCurl() { - return nil + if len(varArgs) == 0 { + return cmderrors.Usage("must provide at least one variable name") } - - fmt.Fprintf(c.Writer, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID) - return nil + return runABCVariablesDelete(ctx, client, instanceID, cmd.Args().Tail(), varArgs, cmd.Writer, cmd.Bool("get-curl")) }, }) func parseABCVariableNames(positional []string, flagged abcVariableArgs) ([]string, error) { variableNames := append([]string{}, positional...) variableNames = append(variableNames, flagged...) - if len(variableNames) == 0 { - return nil, cmderrors.Usage("must provide at least one variable name") - } - for _, name := range variableNames { - if name == "" { - return nil, cmderrors.Usage("variable names must not be empty") - } + if slices.Contains(variableNames, "") { + return nil, cmderrors.Usage("variable names must not be empty") } return variableNames, nil } + +func runABCVariablesDelete(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error { + variableNames, err := parseABCVariableNames(positional, flagged) + if err != nil { + return err + } + + variables := make([]map[string]string, 0, len(variableNames)) + for _, key := range variableNames { + variables = append(variables, map[string]string{ + "key": key, + "value": "null", + }) + } + + if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, variables); err != nil { + return err + } + + if getCurl { + return nil + } + + _, err = fmt.Fprintf(output, "Removed variables %q from workflow instance %q.\n", variableNames, instanceID) + return err +} diff --git a/cmd/src/abc_variables_set.go b/cmd/src/abc_variables_set.go index 6b97cdb9f4..58f5c3b8be 100644 --- a/cmd/src/abc_variables_set.go +++ b/cmd/src/abc_variables_set.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "fmt" + "io" "strings" "github.com/sourcegraph/src-cli/internal/api" @@ -23,70 +24,42 @@ const updateABCWorkflowInstanceVariablesMutation = `mutation UpdateAgenticWorkfl }` var abcVariablesSetCommand = clicompat.Wrap(&cli.Command{ - Name: "set", - Usage: "set workflow instance variables", - Description: `Usage: - - src abc variables set [command options] [= ...] + Name: "set", + UsageText: "src abc variables set [options] [= ...]", + Usage: "Set variables on a workflow instance", + Description: ` +Set workflow instance variables Examples: Set a string variable on a workflow instance: - $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria" + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== prompt="tighten the review criteria" Set multiple variables in one request: - $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]' + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== --var prompt="tighten the review criteria" --var checkpoints='[1,2,3]' Set a structured JSON value: - $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]' + $ src abc variables set QWdlbnRpY1dvcmtmbG93SW5zdGFuY2U6MQ== checkpoints='[1,2,3]' -Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings.`, - DisableSliceFlagSeparator: true, +NOTE: Values are interpreted as JSON literals when valid. Otherwise they are sent as plain strings. +`, Flags: clicompat.WithAPIFlags( &cli.StringSliceFlag{ Name: "var", Usage: "Variable assignment in = form. Repeat to set multiple variables.", }, ), - Action: func(ctx context.Context, c *cli.Command) error { - if c.NArg() == 0 { + Action: func(ctx context.Context, cmd *cli.Command) error { + if !cmd.Args().Present() { return cmderrors.Usage("must provide a workflow instance ID") } - instanceID := c.Args().First() - variables, err := parseABCVariables(c.Args().Tail(), abcVariableArgs(c.StringSlice("var"))) - if err != nil { - return err - } - - graphqlVariables := make([]map[string]string, 0, len(variables)) - for _, variable := range variables { - graphqlVariables = append(graphqlVariables, map[string]string{ - "key": variable.Key, - "value": variable.Value, - }) - } - - apiFlags := clicompat.APIFlagsFromCmd(c) - client := cfg.apiClient(apiFlags, c.Writer) - if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil { - return err - } - - if apiFlags.GetCurl() { - return nil - } - - if len(variables) == 1 { - fmt.Fprintf(c.Writer, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID) - return nil - } - - fmt.Fprintf(c.Writer, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID) - return nil + instanceID := cmd.Args().First() + client := cfg.apiClient(clicompat.APIFlagsFromCmd(cmd), cmd.Writer) + return runABCVariablesSet(ctx, client, instanceID, cmd.Args().Tail(), abcVariableArgs(cmd.StringSlice("var")), cmd.Writer, cmd.Bool("get-curl")) }, }) @@ -142,6 +115,37 @@ func parseABCVariable(raw string) (abcVariable, error) { return abcVariable{Key: name, Value: value}, nil } +func runABCVariablesSet(ctx context.Context, client api.Client, instanceID string, positional []string, flagged abcVariableArgs, output io.Writer, getCurl bool) error { + variables, err := parseABCVariables(positional, flagged) + if err != nil { + return err + } + + graphqlVariables := make([]map[string]string, 0, len(variables)) + for _, variable := range variables { + graphqlVariables = append(graphqlVariables, map[string]string{ + "key": variable.Key, + "value": variable.Value, + }) + } + + if err := updateABCWorkflowInstanceVariables(ctx, client, instanceID, graphqlVariables); err != nil { + return err + } + + if getCurl { + return nil + } + + if len(variables) == 1 { + _, err = fmt.Fprintf(output, "Set variable %q on workflow instance %q.\n", variables[0].Key, instanceID) + return err + } + + _, err = fmt.Fprintf(output, "Updated %d variables on workflow instance %q.\n", len(variables), instanceID) + return err +} + func updateABCWorkflowInstanceVariables(ctx context.Context, client api.Client, instanceID string, variables []map[string]string) error { var result struct { UpdateAgenticWorkflowInstanceVariables struct {