Repository navigation
cmd: refactor in preparation of urfave/cli/v3 migration - #14103
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
There are at least two correctness/documentation issues to address (a broken tracing doc snippet and a potential nil dereference during early cancellation in containerd startup).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR refactors the cmd/* CLI codepaths to make the upcoming github.com/urfave/cli/v3 migration more mechanical, primarily by removing v2-only APIs (cli.Commands, implicit context access) and consistently threading context.Context through CLI setup and client creation.
Changes:
- Replace uses of
cli.Commandswith[]*cli.Commandand remove redundant empty slice defaults for slice flags. - Switch CLI entrypoints to
RunContext, and refactor helpers (commands.AppContext,commands.NewClient) to accept an explicitcontext.Context. - Rename variables to avoid shadowing and to align naming with planned v3 concepts (e.g.,
cliContext→cmd), plus small API tweaks (e.g., pass editor name string instead of*cli.Context).
File summaries
| File | Description |
|---|---|
| docs/tracing.md | Update tracing documentation snippet to match new client/context plumbing. |
| cmd/ctr/main.go | Use RunContext and drop unused plugin command slice. |
| cmd/ctr/commands/version/version.go | Pass explicit context.Context into NewClient; rename handler parameter. |
| cmd/ctr/commands/tasks/tasks_unix.go | Rename cliContext parameter usage to cmd. |
| cmd/ctr/commands/tasks/start.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/tasks/resume.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/tasks/ps.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/tasks/pause.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/tasks/metrics.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/tasks/list.go | Thread explicit ctx/cmd into NewClient; reorder quiet flag read. |
| cmd/ctr/commands/tasks/kill.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/tasks/exec.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/tasks/delete.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/tasks/checkpoint.go | Thread explicit ctx/cmd into NewClient and helper opts. |
| cmd/ctr/commands/tasks/attach.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/snapshots/snapshots.go | Replace cli.Commands and thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/shim/shim.go | Replace cliContext with cmd; remove redundant empty slice defaults. |
| cmd/ctr/commands/shim/pprof.go | Rename cliContext parameter usage to cmd for pprof commands. |
| cmd/ctr/commands/sandboxes/sandboxes.go | Replace cli.Commands and thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/run/run.go | Thread explicit ctx/cmd and rename parameters for v3 preparation. |
| cmd/ctr/commands/run/run_windows.go | Rename cliContext parameter usage to cmd throughout. |
| cmd/ctr/commands/run/run_unix.go | Rename cliContext parameter usage to cmd throughout. |
| cmd/ctr/commands/resolver.go | Rename cliContext parameter usage to cmd in resolver helpers. |
| cmd/ctr/commands/pprof/pprof.go | Rename cliContext parameter usage to cmd and update client func type. |
| cmd/ctr/commands/plugins/plugins.go | Thread explicit ctx/cmd into NewClient and writer usage. |
| cmd/ctr/commands/oci/oci.go | Use cmd.Context + explicit commands.AppContext(ctx, cmd). |
| cmd/ctr/commands/namespaces/namespaces.go | Replace cli.Commands and thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/namespaces/namespaces_other.go | Rename deleteOpts parameter to cmd. |
| cmd/ctr/commands/namespaces/namespaces_linux.go | Rename deleteOpts parameter to cmd and flag lookup. |
| cmd/ctr/commands/leases/leases.go | Replace cli.Commands and thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/install/install.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/info/info.go | Thread explicit ctx/cmd into NewClient. |
| cmd/ctr/commands/images/usage.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/images/unmount.go | Thread explicit ctx/cmd into NewClient and writer usage. |
| cmd/ctr/commands/images/tag.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/images/push.go | Remove redundant slice defaults; thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/images/pull.go | Remove redundant slice defaults; thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/images/mount.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/images/inspect.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/images/import.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/images/images.go | Replace cli.Commands and thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/images/export.go | Remove redundant slice defaults; thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/images/convert.go | Remove redundant slice defaults; thread explicit ctx/cmd throughout. |
| cmd/ctr/commands/images/build_erofs_cache.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/events/events.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/deprecations/deprecations.go | Thread explicit ctx/cmd into NewClient; ignore Setenv error. |
| cmd/ctr/commands/content/prune.go | Replace cli.Commands and thread explicit ctx/cmd. |
| cmd/ctr/commands/content/fetch.go | Thread explicit ctx/cmd into NewClient and NewFetchConfig. |
| cmd/ctr/commands/content/content.go | Replace cli.Commands; thread explicit ctx/cmd; pass editor name string. |
| cmd/ctr/commands/containers/restore.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/containers/containers.go | Thread explicit ctx/cmd into NewClient and args usage. |
| cmd/ctr/commands/containers/checkpoint.go | Thread explicit ctx/cmd into NewClient and flag lookups. |
| cmd/ctr/commands/commands.go | Rename ObjectWithLabelArgs parameter and usages to cmd. |
| cmd/ctr/commands/commands_windows.go | Rename RuntimeOptions parameter to cmd. |
| cmd/ctr/commands/commands_unix.go | Rename RuntimeOptions parameter to cmd and update helpers. |
| cmd/ctr/commands/client.go | Make AppContext/NewClient accept explicit context.Context. |
| cmd/ctr/app/main.go | Construct *cli.App via struct literal; update hooks and command list. |
| cmd/containerd/main.go | Use RunContext for containerd entrypoint. |
| cmd/containerd/command/service_windows.go | Rename applyPlatformFlags parameter to cmd and update uses. |
| cmd/containerd/command/service_unsupported.go | Make applyPlatformFlags a no-op with simplified signature. |
| cmd/containerd/command/publish.go | Rename handler parameter to cmd and use cmd.Context. |
| cmd/containerd/command/oci-hook.go | Rename template context var and use cmd.Args(). |
| cmd/containerd/command/main.go | Construct *cli.App via struct literal; thread explicit context usage. |
| cmd/containerd/command/main_windows.go | Avoid shadowing server by renaming local variable to srv. |
| cmd/containerd/command/main_unix.go | Avoid shadowing server by renaming local variable to srv. |
| cmd/containerd/command/config.go | Rename dumpConfig parameter to cmd and update uses. |
| cmd/containerd-stress/main.go | Construct *cli.App via struct literal and use RunContext. |
| cmd/containerd-stress/density.go | Rename variables (config → cfg) and use cmd consistently. |
Review details
Suppressed comments (1)
docs/tracing.md:88
- The example calls commands.NewClient(ctx, cmd, ...) but the snippet does not define a cli.Context (cmd), making the documentation example unusable. Since this section is about instrumenting the containerd Go client, use containerd.New with dial options instead of ctr's commands.NewClient helper.
- Files reviewed: 68/68 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| case r := <-chsrv: | ||
| if r.err != nil { | ||
| return r.err | ||
| } | ||
| srv = r.s | ||
| } |
There was a problem hiding this comment.
Copilot that's a pre-existing issue; this PR only makes mechanical changes in this code, so I prefer fixing that separately.
357f81f to
0a59ed3
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The updated tracing documentation example references an undefined cmd/internal ctr helper, making the snippet misleading for readers and needing correction before merge.
Review details
Suppressed comments (1)
docs/tracing.md:88
- The tracing example now references
cmd(a*cli.Context) but the snippet doesn’t define it, andcommands.NewClientis an internal helper fromcmd/ctrrather than the containerd Go client API. This makes the documentation example confusing/non-usable for readers outside thectrcodebase. Prefer showingcontainerd.New(...)directly (and closing the client) or update the snippet to explicitly be acli.Action/accept a*cli.Context.
- Files reviewed: 68/68 changed files
- Comments generated: 0 new
- Review effort level: Lite
Signed-off-by: Sebastiaan van Stijn <[email protected]>
The `Commands` type is removed in v3 and already identical to `[]*Command` in v2; remove its uses to ease the transition to v3. Signed-off-by: Sebastiaan van Stijn <[email protected]>
Leave the Value field unset for slice flags without default values. The CLI initializes these flags with an empty value when they are applied, making the explicit initializations unnecessary. Signed-off-by: Sebastiaan van Stijn <[email protected]>
0a59ed3 to
a46073b
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The updated tracing documentation example now references an undefined cmd variable (and is inconsistent with the example’s function signature), so the snippet should be corrected before merging.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 68/68 changed files
- Comments generated: 1
- Review effort level: Lite
| grpc.WithStatsHandler(otelgrpc.NewClientHandler()), | ||
| } | ||
| client, ctx, cancel, err := commands.NewClient(context, containerd.WithDialOpts(dialOpts)) | ||
| client, ctx, cancel, err := commands.NewClient(ctx, cmd, containerd.WithDialOpts(dialOpts)) |
There was a problem hiding this comment.
Pre-existing issue; it's a partial example (see the ...); old example also didn't have context defined;
Lines 78 to 84 in c003f88
This allows explicitly setting up the context inside main, and makes the transition to urfave/cli/v3 more transparent. Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
Signed-off-by: Sebastiaan van Stijn <[email protected]>
In urfave/cli/v3, cli.Context is replaced with cli.Command, making cmd a more logical name. Rename the vars ahead of time to make the v3 migration a more mechanical change. Signed-off-by: Sebastiaan van Stijn <[email protected]>
Although urfave/cli v2 recommends constructing apps with cli.NewApp, App.Run initializes unset fields through App.Setup, making the zero value usable. Use struct literals to define the application fields directly without changing behavior. This prepares for urfave/cli v3, which removes cli.NewApp and replaces cli.App with cli.Command. Signed-off-by: Sebastiaan van Stijn <[email protected]>
urfave/cli/v3 no longer passes the context through the Command; pass it explicitly to prepare for the v3 migration. Signed-off-by: Sebastiaan van Stijn <[email protected]>
urfave/cli/v3 no longer passes the context through the Command; pass it explicitly to prepare for the v3 migration. Signed-off-by: Sebastiaan van Stijn <[email protected]>
a46073b to
861dc7e
Compare
There was a problem hiding this comment.
🟢 Approval recommended
The changes are mechanical, consistently applied across call sites, and align with the stated v3-migration preparation goals without introducing evident behavioral regressions.
Review details
- Files reviewed: 68/68 changed files
- Comments generated: 0 new
- Review effort level: Lite
cmd/ctr: remove unused pluginCmds
cmd: remove uses of urfave/cli.Commands
The
Commandstype is removed in v3 and already identical to[]*Commandin v2; remove its uses to ease the transition to v3.
cmd: remove redundant empty slice flag values
Leave the Value field unset for slice flags without default values. The CLI
initializes these flags with an empty value when they are applied, making
the explicit initializations unnecessary.
cmd: use urfave/cli RunContext
This allows explicitly setting up the context inside main, and makes the
transition to urfave/cli/v3 more transparent.
cmd: rename some vars that shadowed
cmd: edit: pass editor name instead of cli.Context
cmd: rename cliContext -> cmd in preparation of v3 migration
In urfave/cli/v3, cli.Context is replaced with cli.Command, making cmd
a more logical name. Rename the vars ahead of time to make the v3
migration a more mechanical change.
cmd: initialize CLI apps with struct literals
Although urfave/cli v2 recommends constructing apps with cli.NewApp,
App.Run initializes unset fields through App.Setup, making the zero
value usable. Use struct literals to define the application fields
directly without changing behavior.
This prepares for urfave/cli v3, which removes cli.NewApp and replaces
cli.App with cli.Command.
cmd: commands.AppContext: explicitly pass context
urfave/cli/v3 no longer passes the context through the Command; pass
it explicitly to prepare for the v3 migration.
cmd: commands.NewClient: explicitly pass context
urfave/cli/v3 no longer passes the context through the Command; pass
it explicitly to prepare for the v3 migration.