-
Notifications
You must be signed in to change notification settings - Fork 68
feat/cli: migrate version subcommand to use urface/cli #1292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
burmudar
wants to merge
12
commits into
main
Choose a base branch
from
wb/urfave-cli-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e4e8a80
add runLegacy
burmudar cb25b26
use go 1.26
burmudar 81df0f2
add util methods to work urfave/cli flags in api
burmudar d69e79c
tweak help template to match legacy help
burmudar 09b487b
split command runner into legacy and migrated
burmudar f4ff6ca
migrate version to use urface cli
burmudar 302e9ff
add onUsageError func and rename funcs to be 'Wrap*'
burmudar 8cac955
use Wrap from clicompat instead
burmudar 9d0554e
remove init
burmudar a8a6336
update comments and method names of clicompat api flags
burmudar 27fb40d
move runMigrated to be in main and work with os.Args
burmudar 349b951
fix lint
burmudar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "flag" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "sort" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/clicompat" | ||
| "github.com/sourcegraph/src-cli/internal/cmderrors" | ||
| "github.com/urfave/cli/v3" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/lib/errors" | ||
| ) | ||
|
|
||
| var migratedCommands = map[string]*cli.Command{ | ||
| "version": versionCommand, | ||
| } | ||
|
|
||
| func maybeRunMigratedCommand() (isMigrated bool, exitCode int, err error) { | ||
| // need to figure out if a migrated command has been requested | ||
| flag.Parse() | ||
| subCommand := flag.CommandLine.Arg(0) | ||
| _, isMigrated = migratedCommands[subCommand] | ||
| if !isMigrated { | ||
| return | ||
| } | ||
| cfg, err = readConfig() | ||
| if err != nil { | ||
| log.Fatal("reading config: ", err) | ||
| } | ||
|
|
||
| exitCode, err = runMigrated() | ||
| return | ||
| } | ||
|
|
||
| // migratedRootCommand constructs a root 'src' command and adds | ||
| // MigratedCommands as subcommands to it | ||
| func migratedRootCommand() *cli.Command { | ||
| names := make([]string, 0, len(migratedCommands)) | ||
| for name := range migratedCommands { | ||
| names = append(names, name) | ||
| } | ||
| sort.Strings(names) | ||
|
|
||
| commands := make([]*cli.Command, 0, len(names)) | ||
| for _, name := range names { | ||
| commands = append(commands, migratedCommands[name]) | ||
| } | ||
|
|
||
| return clicompat.WrapRoot(&cli.Command{ | ||
| Name: "src", | ||
| HideVersion: true, | ||
| Commands: commands, | ||
| }) | ||
| } | ||
|
|
||
| // runMigrated runs the command within urfave/cli framework | ||
| func runMigrated() (int, error) { | ||
| ctx := context.Background() | ||
|
|
||
| err := migratedRootCommand().Run(ctx, os.Args) | ||
| if errors.HasType[*cmderrors.UsageError](err) { | ||
| return 2, nil | ||
| } | ||
| var exitErr cli.ExitCoder | ||
| if errors.AsInterface(err, &exitErr) { | ||
| return exitErr.ExitCode(), err | ||
| } | ||
| return 0, err | ||
| } | ||
|
|
||
| // runLegacy runs the command using the original commander framework | ||
| func runLegacy(cmd *command, flagSet *flag.FlagSet) (int, error) { | ||
| // Parse subcommand flags. | ||
| args := flagSet.Args()[1:] | ||
| if err := cmd.flagSet.Parse(args); err != nil { | ||
| fmt.Printf("Error parsing subcommand flags: %s\n", err) | ||
| panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err)) | ||
| } | ||
|
|
||
| // Execute the subcommand. | ||
| if err := cmd.handler(flagSet.Args()[1:]); err != nil { | ||
| if _, ok := err.(*cmderrors.UsageError); ok { | ||
| log.Printf("error: %s\n\n", err) | ||
| cmd.flagSet.SetOutput(os.Stderr) | ||
| flag.CommandLine.SetOutput(os.Stderr) | ||
| cmd.flagSet.Usage() | ||
| return 2, nil | ||
| } | ||
| if e, ok := err.(*cmderrors.ExitCodeError); ok { | ||
| if e.HasError() { | ||
| log.Println(e) | ||
| } | ||
| return e.Code(), nil | ||
| } | ||
| return 1, err | ||
| } | ||
| return 0, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package clicompat | ||
|
|
||
| import ( | ||
| "os" | ||
|
|
||
| "github.com/sourcegraph/src-cli/internal/api" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
||
| // WithAPIFlags appends the standard API-related flags used by legacy src | ||
| func WithAPIFlags(baseFlags ...cli.Flag) []cli.Flag { | ||
| var flagTable = []struct { | ||
| name string | ||
| value bool | ||
| text string | ||
| }{ | ||
| {"dump-requests", false, "Log GraphQL requests and responses to stdout"}, | ||
| {"get-curl", false, "Print the curl command for executing this query and exit (WARNING: includes printing your access token!)"}, | ||
| {"trace", false, "Log the trace ID for requests. See https://docs.sourcegraph.com/admin/observability/tracing"}, | ||
| {"insecure-skip-verify", false, "Skip validation of TLS certificates against trusted chains"}, | ||
| {"user-agent-telemetry", defaultAPIUserAgentTelemetry(), "Include the operating system and architecture in the User-Agent sent with requests to Sourcegraph"}, | ||
| } | ||
|
|
||
| flags := append([]cli.Flag{}, baseFlags...) | ||
| for _, item := range flagTable { | ||
| flags = append(flags, &cli.BoolFlag{ | ||
| Name: item.name, | ||
| Value: item.value, | ||
| Usage: item.text, | ||
| }) | ||
| } | ||
|
|
||
| return flags | ||
| } | ||
|
|
||
| // APIFlagsFromCmd reads the shared API-related flags from a command into api.Flags | ||
| func APIFlagsFromCmd(cmd *cli.Command) *api.Flags { | ||
| return api.NewFlagsFromValues( | ||
| cmd.Bool("dump-requests"), | ||
| cmd.Bool("get-curl"), | ||
| cmd.Bool("trace"), | ||
| cmd.Bool("insecure-skip-verify"), | ||
| cmd.Bool("user-agent-telemetry"), | ||
| ) | ||
| } | ||
|
|
||
| func defaultAPIUserAgentTelemetry() bool { | ||
| return os.Getenv("SRC_DISABLE_USER_AGENT_TELEMETRY") == "" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kinda confused how this all worked before. .Args is the non flag arguments, not the raw argv.