Skip to content

Commit 7f7204a

Browse files
authored
Merge pull request containerd#3913 from apostasie/refactor-cmd-1
Refactor: suggesting we normalize all command variables to cmd
2 parents 5f11371 + b41e8c9 commit 7f7204a

File tree

88 files changed

+527
-527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+527
-527
lines changed

cmd/nerdctl/builder/builder.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ import (
3131
)
3232

3333
func NewBuilderCommand() *cobra.Command {
34-
var builderCommand = &cobra.Command{
34+
var cmd = &cobra.Command{
3535
Annotations: map[string]string{helpers.Category: helpers.Management},
3636
Use: "builder",
3737
Short: "Manage builds",
3838
RunE: helpers.UnknownSubcommandAction,
3939
SilenceUsage: true,
4040
SilenceErrors: true,
4141
}
42-
builderCommand.AddCommand(
42+
cmd.AddCommand(
4343
NewBuildCommand(),
4444
newBuilderPruneCommand(),
4545
newBuilderDebugCommand(),
4646
)
47-
return builderCommand
47+
return cmd
4848
}
4949

5050
func newBuilderPruneCommand() *cobra.Command {
5151
shortHelp := `Clean up BuildKit build cache`
52-
var buildPruneCommand = &cobra.Command{
52+
var cmd = &cobra.Command{
5353
Use: "prune",
5454
Args: cobra.NoArgs,
5555
Short: shortHelp,
@@ -58,11 +58,11 @@ func newBuilderPruneCommand() *cobra.Command {
5858
SilenceErrors: true,
5959
}
6060

61-
helpers.AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
61+
helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
6262

63-
buildPruneCommand.Flags().BoolP("all", "a", false, "Remove all unused build cache, not just dangling ones")
64-
buildPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
65-
return buildPruneCommand
63+
cmd.Flags().BoolP("all", "a", false, "Remove all unused build cache, not just dangling ones")
64+
cmd.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
65+
return cmd
6666
}
6767

6868
func builderPruneAction(cmd *cobra.Command, _ []string) error {
@@ -133,21 +133,21 @@ func processBuilderPruneOptions(cmd *cobra.Command) (types.BuilderPruneOptions,
133133

134134
func newBuilderDebugCommand() *cobra.Command {
135135
shortHelp := `Debug Dockerfile`
136-
var buildDebugCommand = &cobra.Command{
136+
var cmd = &cobra.Command{
137137
Use: "debug",
138138
Short: shortHelp,
139139
PreRunE: helpers.CheckExperimental("`nerdctl builder debug`"),
140140
RunE: builderDebugAction,
141141
SilenceUsage: true,
142142
SilenceErrors: true,
143143
}
144-
buildDebugCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
145-
buildDebugCommand.Flags().String("target", "", "Set the target build stage to build")
146-
buildDebugCommand.Flags().StringArray("build-arg", nil, "Set build-time variables")
147-
buildDebugCommand.Flags().String("image", "", "Image to use for debugging stage")
148-
buildDebugCommand.Flags().StringArray("ssh", nil, "Allow forwarding SSH agent to the build. Format: default|<id>[=<socket>|<key>[,<key>]]")
149-
buildDebugCommand.Flags().StringArray("secret", nil, "Expose secret value to the build. Format: id=secretname,src=filepath")
150-
return buildDebugCommand
144+
cmd.Flags().StringP("file", "f", "", "Name of the Dockerfile")
145+
cmd.Flags().String("target", "", "Set the target build stage to build")
146+
cmd.Flags().StringArray("build-arg", nil, "Set build-time variables")
147+
cmd.Flags().String("image", "", "Image to use for debugging stage")
148+
cmd.Flags().StringArray("ssh", nil, "Allow forwarding SSH agent to the build. Format: default|<id>[=<socket>|<key>[,<key>]]")
149+
cmd.Flags().StringArray("secret", nil, "Expose secret value to the build. Format: id=secretname,src=filepath")
150+
return cmd
151151
}
152152

153153
func builderDebugAction(cmd *cobra.Command, args []string) error {

cmd/nerdctl/builder/builder_build.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
)
3636

3737
func NewBuildCommand() *cobra.Command {
38-
var buildCommand = &cobra.Command{
38+
var cmd = &cobra.Command{
3939
Use: "build [flags] PATH",
4040
Short: "Build an image from a Dockerfile. Needs buildkitd to be running.",
4141
Long: `Build an image from a Dockerfile. Needs buildkitd to be running.
@@ -44,44 +44,44 @@ If Dockerfile is not present and -f is not specified, it will look for Container
4444
SilenceUsage: true,
4545
SilenceErrors: true,
4646
}
47-
helpers.AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
48-
buildCommand.Flags().StringArray("add-host", nil, "Add a custom host-to-IP mapping (format: \"host:ip\")")
49-
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
50-
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
51-
buildCommand.Flags().String("target", "", "Set the target build stage to build")
52-
buildCommand.Flags().StringArray("build-arg", nil, "Set build-time variables")
53-
buildCommand.Flags().Bool("no-cache", false, "Do not use cache when building the image")
54-
buildCommand.Flags().StringP("output", "o", "", "Output destination (format: type=local,dest=path)")
55-
buildCommand.Flags().String("progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
56-
buildCommand.Flags().String("provenance", "", "Shorthand for \"--attest=type=provenance\"")
57-
buildCommand.Flags().Bool("pull", false, "On true, always attempt to pull latest image version from remote. Default uses buildkit's default.")
58-
buildCommand.Flags().StringArray("secret", nil, "Secret file to expose to the build: id=mysecret,src=/local/secret")
59-
buildCommand.Flags().StringArray("allow", nil, "Allow extra privileged entitlement, e.g. network.host, security.insecure")
60-
buildCommand.RegisterFlagCompletionFunc("allow", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
47+
helpers.AddStringFlag(cmd, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
48+
cmd.Flags().StringArray("add-host", nil, "Add a custom host-to-IP mapping (format: \"host:ip\")")
49+
cmd.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
50+
cmd.Flags().StringP("file", "f", "", "Name of the Dockerfile")
51+
cmd.Flags().String("target", "", "Set the target build stage to build")
52+
cmd.Flags().StringArray("build-arg", nil, "Set build-time variables")
53+
cmd.Flags().Bool("no-cache", false, "Do not use cache when building the image")
54+
cmd.Flags().StringP("output", "o", "", "Output destination (format: type=local,dest=path)")
55+
cmd.Flags().String("progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
56+
cmd.Flags().String("provenance", "", "Shorthand for \"--attest=type=provenance\"")
57+
cmd.Flags().Bool("pull", false, "On true, always attempt to pull latest image version from remote. Default uses buildkit's default.")
58+
cmd.Flags().StringArray("secret", nil, "Secret file to expose to the build: id=mysecret,src=/local/secret")
59+
cmd.Flags().StringArray("allow", nil, "Allow extra privileged entitlement, e.g. network.host, security.insecure")
60+
cmd.RegisterFlagCompletionFunc("allow", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
6161
return []string{"network.host", "security.insecure"}, cobra.ShellCompDirectiveNoFileComp
6262
})
63-
buildCommand.Flags().StringArray("attest", nil, "Attestation parameters (format: \"type=sbom,generator=image\")")
64-
buildCommand.Flags().StringArray("ssh", nil, "SSH agent socket or keys to expose to the build (format: default|<id>[=<socket>|<key>[,<key>]])")
65-
buildCommand.Flags().BoolP("quiet", "q", false, "Suppress the build output and print image ID on success")
66-
buildCommand.Flags().String("sbom", "", "Shorthand for \"--attest=type=sbom\"")
67-
buildCommand.Flags().StringArray("cache-from", nil, "External cache sources (eg. user/app:cache, type=local,src=path/to/dir)")
68-
buildCommand.Flags().StringArray("cache-to", nil, "Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)")
69-
buildCommand.Flags().Bool("rm", true, "Remove intermediate containers after a successful build")
70-
buildCommand.Flags().String("network", "default", "Set type of network for build (format:network=default|none|host)")
71-
buildCommand.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
63+
cmd.Flags().StringArray("attest", nil, "Attestation parameters (format: \"type=sbom,generator=image\")")
64+
cmd.Flags().StringArray("ssh", nil, "SSH agent socket or keys to expose to the build (format: default|<id>[=<socket>|<key>[,<key>]])")
65+
cmd.Flags().BoolP("quiet", "q", false, "Suppress the build output and print image ID on success")
66+
cmd.Flags().String("sbom", "", "Shorthand for \"--attest=type=sbom\"")
67+
cmd.Flags().StringArray("cache-from", nil, "External cache sources (eg. user/app:cache, type=local,src=path/to/dir)")
68+
cmd.Flags().StringArray("cache-to", nil, "Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)")
69+
cmd.Flags().Bool("rm", true, "Remove intermediate containers after a successful build")
70+
cmd.Flags().String("network", "default", "Set type of network for build (format:network=default|none|host)")
71+
cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
7272
return []string{"default", "host", "none"}, cobra.ShellCompDirectiveNoFileComp
7373
})
7474
// #region platform flags
7575
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
76-
buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
77-
buildCommand.RegisterFlagCompletionFunc("platform", completion.Platforms)
78-
buildCommand.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)")
76+
cmd.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
77+
cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
78+
cmd.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)")
7979
// #endregion
8080

81-
buildCommand.Flags().String("iidfile", "", "Write the image ID to the file")
82-
buildCommand.Flags().StringArray("label", nil, "Set metadata for an image")
81+
cmd.Flags().String("iidfile", "", "Write the image ID to the file")
82+
cmd.Flags().StringArray("label", nil, "Set metadata for an image")
8383

84-
return buildCommand
84+
return cmd
8585
}
8686

8787
func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBuildOptions, error) {

cmd/nerdctl/compose/compose.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
)
2525

2626
func NewComposeCommand() *cobra.Command {
27-
var composeCommand = &cobra.Command{
27+
var cmd = &cobra.Command{
2828
Use: "compose [flags] COMMAND",
2929
Short: "Compose",
3030
RunE: helpers.UnknownSubcommandAction,
@@ -33,14 +33,14 @@ func NewComposeCommand() *cobra.Command {
3333
TraverseChildren: true, // required for global short hands like -f
3434
}
3535
// `-f` is a nonPersistentAlias, as it conflicts with `nerdctl compose logs --follow`
36-
helpers.AddPersistentStringArrayFlag(composeCommand, "file", nil, []string{"f"}, nil, "", "Specify an alternate compose file")
37-
composeCommand.PersistentFlags().String("project-directory", "", "Specify an alternate working directory")
38-
composeCommand.PersistentFlags().StringP("project-name", "p", "", "Specify an alternate project name")
39-
composeCommand.PersistentFlags().String("env-file", "", "Specify an alternate environment file")
40-
composeCommand.PersistentFlags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")
41-
composeCommand.PersistentFlags().StringArray("profile", []string{}, "Specify a profile to enable")
36+
helpers.AddPersistentStringArrayFlag(cmd, "file", nil, []string{"f"}, nil, "", "Specify an alternate compose file")
37+
cmd.PersistentFlags().String("project-directory", "", "Specify an alternate working directory")
38+
cmd.PersistentFlags().StringP("project-name", "p", "", "Specify an alternate project name")
39+
cmd.PersistentFlags().String("env-file", "", "Specify an alternate environment file")
40+
cmd.PersistentFlags().String("ipfs-address", "", "multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs)")
41+
cmd.PersistentFlags().StringArray("profile", []string{}, "Specify a profile to enable")
4242

43-
composeCommand.AddCommand(
43+
cmd.AddCommand(
4444
newComposeUpCommand(),
4545
newComposeLogsCommand(),
4646
newComposeConfigCommand(),
@@ -66,7 +66,7 @@ func NewComposeCommand() *cobra.Command {
6666
newComposeCreateCommand(),
6767
)
6868

69-
return composeCommand
69+
return cmd
7070
}
7171

7272
func getComposeOptions(cmd *cobra.Command, debugFull, experimental bool) (composer.Options, error) {

cmd/nerdctl/compose/compose_build.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ import (
2626
)
2727

2828
func newComposeBuildCommand() *cobra.Command {
29-
var composeBuildCommand = &cobra.Command{
29+
var cmd = &cobra.Command{
3030
Use: "build [flags] [SERVICE...]",
3131
Short: "Build or rebuild services",
3232
RunE: composeBuildAction,
3333
SilenceUsage: true,
3434
SilenceErrors: true,
3535
}
36-
composeBuildCommand.Flags().StringArray("build-arg", nil, "Set build-time variables for services.")
37-
composeBuildCommand.Flags().Bool("no-cache", false, "Do not use cache when building the image.")
38-
composeBuildCommand.Flags().String("progress", "", "Set type of progress output (auto, plain, tty). Use plain to show container output")
36+
cmd.Flags().StringArray("build-arg", nil, "Set build-time variables for services.")
37+
cmd.Flags().Bool("no-cache", false, "Do not use cache when building the image.")
38+
cmd.Flags().String("progress", "", "Set type of progress output (auto, plain, tty). Use plain to show container output")
3939

40-
return composeBuildCommand
40+
return cmd
4141
}
4242

4343
func composeBuildAction(cmd *cobra.Command, args []string) error {

cmd/nerdctl/compose/compose_config.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ import (
2828
)
2929

3030
func newComposeConfigCommand() *cobra.Command {
31-
var composeConfigCommand = &cobra.Command{
31+
var cmd = &cobra.Command{
3232
Use: "config",
3333
Short: "Validate and view the Compose file",
3434
RunE: composeConfigAction,
3535
SilenceUsage: true,
3636
SilenceErrors: true,
3737
}
38-
composeConfigCommand.Flags().BoolP("quiet", "q", false, "Only validate the configuration, don't print anything.")
39-
composeConfigCommand.Flags().Bool("services", false, "Print the service names, one per line.")
40-
composeConfigCommand.Flags().Bool("volumes", false, "Print the volume names, one per line.")
41-
composeConfigCommand.Flags().String("hash", "", "Print the service config hash, one per line.")
42-
composeConfigCommand.RegisterFlagCompletionFunc("hash", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
38+
cmd.Flags().BoolP("quiet", "q", false, "Only validate the configuration, don't print anything.")
39+
cmd.Flags().Bool("services", false, "Print the service names, one per line.")
40+
cmd.Flags().Bool("volumes", false, "Print the volume names, one per line.")
41+
cmd.Flags().String("hash", "", "Print the service config hash, one per line.")
42+
cmd.RegisterFlagCompletionFunc("hash", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
4343
return []string{"\"*\""}, cobra.ShellCompDirectiveNoFileComp
4444
})
45-
return composeConfigCommand
45+
return cmd
4646
}
4747

4848
func composeConfigAction(cmd *cobra.Command, args []string) error {

cmd/nerdctl/compose/compose_cp.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ import (
3131
func newComposeCopyCommand() *cobra.Command {
3232
usage := `cp [OPTIONS] SERVICE:SRC_PATH DEST_PATH|-
3333
nerdctl compose cp [OPTIONS] SRC_PATH|- SERVICE:DEST_PATH`
34-
var composeCpCommand = &cobra.Command{
34+
var cmd = &cobra.Command{
3535
Use: usage,
3636
Short: "Copy files/folders between a service container and the local filesystem",
3737
Args: cobra.ExactArgs(2),
3838
RunE: composeCopyAction,
3939
SilenceUsage: true,
4040
SilenceErrors: true,
4141
}
42-
composeCpCommand.Flags().Bool("dry-run", false, "Execute command in dry run mode")
43-
composeCpCommand.Flags().BoolP("follow-link", "L", false, "Always follow symbol link in SRC_PATH")
44-
composeCpCommand.Flags().Int("index", 0, "index of the container if service has multiple replicas")
45-
return composeCpCommand
42+
cmd.Flags().Bool("dry-run", false, "Execute command in dry run mode")
43+
cmd.Flags().BoolP("follow-link", "L", false, "Always follow symbol link in SRC_PATH")
44+
cmd.Flags().Int("index", 0, "index of the container if service has multiple replicas")
45+
return cmd
4646
}
4747

4848
func composeCopyAction(cmd *cobra.Command, args []string) error {

cmd/nerdctl/compose/compose_create.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ import (
2828
)
2929

3030
func newComposeCreateCommand() *cobra.Command {
31-
var composeCreateCommand = &cobra.Command{
31+
var cmd = &cobra.Command{
3232
Use: "create [flags] [SERVICE...]",
3333
Short: "Creates containers for one or more services",
3434
RunE: composeCreateAction,
3535
SilenceUsage: true,
3636
SilenceErrors: true,
3737
}
38-
composeCreateCommand.Flags().Bool("build", false, "Build images before starting containers.")
39-
composeCreateCommand.Flags().Bool("no-build", false, "Don't build an image even if it's missing, conflict with --build.")
40-
composeCreateCommand.Flags().Bool("force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
41-
composeCreateCommand.Flags().Bool("no-recreate", false, "Don't recreate containers if they exist, conflict with --force-recreate.")
42-
composeCreateCommand.Flags().String("pull", "missing", "Pull images before running. (support always|missing|never)")
43-
return composeCreateCommand
38+
cmd.Flags().Bool("build", false, "Build images before starting containers.")
39+
cmd.Flags().Bool("no-build", false, "Don't build an image even if it's missing, conflict with --build.")
40+
cmd.Flags().Bool("force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
41+
cmd.Flags().Bool("no-recreate", false, "Don't recreate containers if they exist, conflict with --force-recreate.")
42+
cmd.Flags().String("pull", "missing", "Pull images before running. (support always|missing|never)")
43+
return cmd
4444
}
4545

4646
func composeCreateAction(cmd *cobra.Command, args []string) error {

cmd/nerdctl/compose/compose_down.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ import (
2626
)
2727

2828
func newComposeDownCommand() *cobra.Command {
29-
var composeDownCommand = &cobra.Command{
29+
var cmd = &cobra.Command{
3030
Use: "down",
3131
Short: "Remove containers and associated resources",
3232
Args: cobra.NoArgs,
3333
RunE: composeDownAction,
3434
SilenceUsage: true,
3535
SilenceErrors: true,
3636
}
37-
composeDownCommand.Flags().BoolP("volumes", "v", false, "Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
38-
composeDownCommand.Flags().Bool("remove-orphans", false, "Remove containers for services not defined in the Compose file.")
39-
return composeDownCommand
37+
cmd.Flags().BoolP("volumes", "v", false, "Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
38+
cmd.Flags().Bool("remove-orphans", false, "Remove containers for services not defined in the Compose file.")
39+
return cmd
4040
}
4141

4242
func composeDownAction(cmd *cobra.Command, args []string) error {

0 commit comments

Comments
 (0)