From 51af369069fc41c66be248cad2c8b797af0724f5 Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 11:31:53 -0700 Subject: [PATCH 01/16] Add Confirm helper Signed-off-by: apostasie --- cmd/nerdctl/builder.go | 14 ++++-------- cmd/nerdctl/compose_rm.go | 10 ++++----- cmd/nerdctl/container_prune.go | 14 ++---------- cmd/nerdctl/helpers/prompt.go | 39 ++++++++++++++++++++++++++++++++++ cmd/nerdctl/image_prune.go | 15 ++++--------- 5 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 cmd/nerdctl/helpers/prompt.go diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder.go index a5f82727ecf..f10b0817d00 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder.go @@ -25,6 +25,7 @@ import ( "github.com/docker/go-units" "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/builder" ) @@ -71,23 +72,16 @@ func builderPruneAction(cmd *cobra.Command, _ []string) error { } if !options.Force { - var ( - confirm string - msg string - ) + var msg string if options.All { msg = "This will remove all build cache." } else { msg = "This will remove any dangling build cache." } - msg += " Are you sure you want to continue? [y/N] " - fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg) - fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) - - if strings.ToLower(confirm) != "y" { - return nil + if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed { + return err } } diff --git a/cmd/nerdctl/compose_rm.go b/cmd/nerdctl/compose_rm.go index 778345e993a..f249770881c 100644 --- a/cmd/nerdctl/compose_rm.go +++ b/cmd/nerdctl/compose_rm.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -51,18 +52,15 @@ func composeRemoveAction(cmd *cobra.Command, args []string) error { return err } if !force { - var confirm string services := "all" if len(args) != 0 { services = strings.Join(args, ",") } + msg := fmt.Sprintf("This will remove all stopped containers from services: %s.", services) - msg += "\nAre you sure you want to continue? [y/N] " - fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg) - fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) - if strings.ToLower(confirm) != "y" { - return nil + if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed { + return err } } diff --git a/cmd/nerdctl/container_prune.go b/cmd/nerdctl/container_prune.go index fc2bdcc9457..35793a45409 100644 --- a/cmd/nerdctl/container_prune.go +++ b/cmd/nerdctl/container_prune.go @@ -17,11 +17,9 @@ package main import ( - "fmt" - "strings" - "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -59,15 +57,7 @@ func grantPrunePermission(cmd *cobra.Command) (bool, error) { } if !force { - var confirm string - msg := "This will remove all stopped containers." - msg += "\nAre you sure you want to continue? [y/N] " - fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg) - fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) - - if strings.ToLower(confirm) != "y" { - return false, nil - } + return helpers.Confirm(cmd, "WARNING! This will remove all stopped containers.") } return true, nil } diff --git a/cmd/nerdctl/helpers/prompt.go b/cmd/nerdctl/helpers/prompt.go new file mode 100644 index 00000000000..9254699f7a6 --- /dev/null +++ b/cmd/nerdctl/helpers/prompt.go @@ -0,0 +1,39 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package helpers + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" +) + +func Confirm(cmd *cobra.Command, message string) (bool, error) { + message += "\nAre you sure you want to continue? [y/N] " + _, err := fmt.Fprint(cmd.OutOrStdout(), message) + if err != nil { + return false, err + } + + var confirm string + _, err = fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) + if err != nil { + return false, err + } + return strings.ToLower(confirm) == "y", err +} diff --git a/cmd/nerdctl/image_prune.go b/cmd/nerdctl/image_prune.go index 4508efaaa10..507dea9566e 100644 --- a/cmd/nerdctl/image_prune.go +++ b/cmd/nerdctl/image_prune.go @@ -18,10 +18,10 @@ package main import ( "fmt" - "strings" "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -82,22 +82,15 @@ func imagePruneAction(cmd *cobra.Command, _ []string) error { } if !options.Force { - var ( - confirm string - msg string - ) + var msg string if !options.All { msg = "This will remove all dangling images." } else { msg = "This will remove all images without at least one container associated to them." } - msg += "\nAre you sure you want to continue? [y/N] " - fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg) - fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm) - - if strings.ToLower(confirm) != "y" { - return nil + if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed { + return err } } From b8b5d707983adb83c198693e3245a16c849fb2d7 Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 11:49:33 -0700 Subject: [PATCH 02/16] Move away flagutil under helpers Signed-off-by: apostasie --- cmd/nerdctl/builder.go | 4 ++-- cmd/nerdctl/builder_build.go | 3 ++- cmd/nerdctl/completion.go | 9 +++++---- cmd/nerdctl/compose_build.go | 3 ++- cmd/nerdctl/compose_config.go | 3 ++- cmd/nerdctl/compose_cp.go | 3 ++- cmd/nerdctl/compose_create.go | 3 ++- cmd/nerdctl/compose_down.go | 3 ++- cmd/nerdctl/compose_exec.go | 3 ++- cmd/nerdctl/compose_images.go | 3 ++- cmd/nerdctl/compose_kill.go | 3 ++- cmd/nerdctl/compose_logs.go | 3 ++- cmd/nerdctl/compose_pause.go | 5 +++-- cmd/nerdctl/compose_port.go | 3 ++- cmd/nerdctl/compose_ps.go | 3 ++- cmd/nerdctl/compose_pull.go | 3 ++- cmd/nerdctl/compose_push.go | 3 ++- cmd/nerdctl/compose_restart.go | 3 ++- cmd/nerdctl/compose_rm.go | 2 +- cmd/nerdctl/compose_run.go | 3 ++- cmd/nerdctl/compose_start.go | 3 ++- cmd/nerdctl/compose_stop.go | 3 ++- cmd/nerdctl/compose_top.go | 3 ++- cmd/nerdctl/compose_up.go | 3 ++- cmd/nerdctl/container_attach.go | 3 ++- cmd/nerdctl/container_commit.go | 3 ++- cmd/nerdctl/container_cp_linux.go | 3 ++- cmd/nerdctl/container_create.go | 5 +++-- cmd/nerdctl/container_diff.go | 3 ++- cmd/nerdctl/container_exec.go | 3 ++- cmd/nerdctl/container_inspect.go | 3 ++- cmd/nerdctl/container_kill.go | 3 ++- cmd/nerdctl/container_list.go | 3 ++- cmd/nerdctl/container_logs.go | 3 ++- cmd/nerdctl/container_pause.go | 3 ++- cmd/nerdctl/container_port.go | 3 ++- cmd/nerdctl/container_prune.go | 2 +- cmd/nerdctl/container_remove.go | 3 ++- cmd/nerdctl/container_rename.go | 3 ++- cmd/nerdctl/container_restart.go | 3 ++- cmd/nerdctl/container_start.go | 3 ++- cmd/nerdctl/container_stats.go | 3 ++- cmd/nerdctl/container_stop.go | 3 ++- cmd/nerdctl/container_top.go | 3 ++- cmd/nerdctl/container_unpause.go | 3 ++- cmd/nerdctl/container_update.go | 3 ++- cmd/nerdctl/container_wait.go | 3 ++- cmd/nerdctl/{ => helpers}/flagutil.go | 29 +++------------------------ cmd/nerdctl/image_convert.go | 3 ++- cmd/nerdctl/image_cryptutil.go | 3 ++- cmd/nerdctl/image_history.go | 3 ++- cmd/nerdctl/image_inspect.go | 3 ++- cmd/nerdctl/image_list.go | 3 ++- cmd/nerdctl/image_load.go | 3 ++- cmd/nerdctl/image_prune.go | 2 +- cmd/nerdctl/image_pull.go | 5 +++-- cmd/nerdctl/image_push.go | 26 +++++++++++++++++++++++- cmd/nerdctl/image_remove.go | 3 ++- cmd/nerdctl/image_save.go | 3 ++- cmd/nerdctl/image_tag.go | 3 ++- cmd/nerdctl/inspect.go | 3 ++- cmd/nerdctl/internal_oci_hook.go | 3 ++- cmd/nerdctl/login.go | 3 ++- cmd/nerdctl/main.go | 5 +++-- cmd/nerdctl/main_unix.go | 5 +++-- cmd/nerdctl/namespace.go | 3 ++- cmd/nerdctl/namespace_create.go | 3 ++- cmd/nerdctl/namespace_inspect.go | 3 ++- cmd/nerdctl/namespace_remove.go | 3 ++- cmd/nerdctl/namespace_update.go | 3 ++- cmd/nerdctl/network_create.go | 3 ++- cmd/nerdctl/network_inspect.go | 3 ++- cmd/nerdctl/network_list.go | 3 ++- cmd/nerdctl/network_prune.go | 3 ++- cmd/nerdctl/network_remove.go | 3 ++- cmd/nerdctl/system_events.go | 3 ++- cmd/nerdctl/system_info.go | 3 ++- cmd/nerdctl/system_prune.go | 3 ++- cmd/nerdctl/version.go | 3 ++- cmd/nerdctl/volume_create.go | 3 ++- cmd/nerdctl/volume_inspect.go | 3 ++- cmd/nerdctl/volume_list.go | 3 ++- cmd/nerdctl/volume_prune.go | 3 ++- cmd/nerdctl/volume_remove.go | 3 ++- 84 files changed, 197 insertions(+), 118 deletions(-) rename cmd/nerdctl/{ => helpers}/flagutil.go (81%) diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder.go index f10b0817d00..9a6bb5c5c98 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder.go @@ -102,7 +102,7 @@ func builderPruneAction(cmd *cobra.Command, _ []string) error { } func processBuilderPruneOptions(cmd *cobra.Command) (types.BuilderPruneOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.BuilderPruneOptions{}, err } @@ -151,7 +151,7 @@ func newBuilderDebugCommand() *cobra.Command { } func builderDebugAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/builder_build.go b/cmd/nerdctl/builder_build.go index 46e04ed00b4..e496a16fc98 100644 --- a/cmd/nerdctl/builder_build.go +++ b/cmd/nerdctl/builder_build.go @@ -25,6 +25,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/buildkitutil" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -82,7 +83,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container } func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBuildOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.BuilderBuildOptions{}, err } diff --git a/cmd/nerdctl/completion.go b/cmd/nerdctl/completion.go index 398723b3ee1..bf03fcdd7a9 100644 --- a/cmd/nerdctl/completion.go +++ b/cmd/nerdctl/completion.go @@ -24,13 +24,14 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/labels" "github.com/containerd/nerdctl/v2/pkg/netutil" ) func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -53,7 +54,7 @@ func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirec } func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -102,7 +103,7 @@ func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd. // shellCompleteNetworkNames includes {"bridge","host","none"} func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -134,7 +135,7 @@ func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, } func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/nerdctl/compose_build.go b/cmd/nerdctl/compose_build.go index d607d17b2f4..2c8ad6c34a6 100644 --- a/cmd/nerdctl/compose_build.go +++ b/cmd/nerdctl/compose_build.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -40,7 +41,7 @@ func newComposeBuildCommand() *cobra.Command { } func composeBuildAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_config.go b/cmd/nerdctl/compose_config.go index c920c77f441..4467d3f20df 100644 --- a/cmd/nerdctl/compose_config.go +++ b/cmd/nerdctl/compose_config.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -45,7 +46,7 @@ func newComposeConfigCommand() *cobra.Command { } func composeConfigAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_cp.go b/cmd/nerdctl/compose_cp.go index 976a5617ed9..90e7cb8309e 100644 --- a/cmd/nerdctl/compose_cp.go +++ b/cmd/nerdctl/compose_cp.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -45,7 +46,7 @@ func newComposeCopyCommand() *cobra.Command { } func composeCopyAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_create.go b/cmd/nerdctl/compose_create.go index e420984e933..14a8021460a 100644 --- a/cmd/nerdctl/compose_create.go +++ b/cmd/nerdctl/compose_create.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -43,7 +44,7 @@ func newComposeCreateCommand() *cobra.Command { } func composeCreateAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_down.go b/cmd/nerdctl/compose_down.go index 6bcc152963d..9e29281282f 100644 --- a/cmd/nerdctl/compose_down.go +++ b/cmd/nerdctl/compose_down.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -39,7 +40,7 @@ func newComposeDownCommand() *cobra.Command { } func composeDownAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_exec.go b/cmd/nerdctl/compose_exec.go index 863ff1eb281..36958da83ff 100644 --- a/cmd/nerdctl/compose_exec.go +++ b/cmd/nerdctl/compose_exec.go @@ -23,6 +23,7 @@ import ( "github.com/moby/term" "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -61,7 +62,7 @@ func newComposeExecCommand() *cobra.Command { } func composeExecAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_images.go b/cmd/nerdctl/compose_images.go index 852cae4e437..fcb2a373ea0 100644 --- a/cmd/nerdctl/compose_images.go +++ b/cmd/nerdctl/compose_images.go @@ -29,6 +29,7 @@ import ( "github.com/containerd/containerd/v2/core/snapshots" "github.com/containerd/containerd/v2/pkg/progress" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/formatter" @@ -51,7 +52,7 @@ func newComposeImagesCommand() *cobra.Command { } func composeImagesAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_kill.go b/cmd/nerdctl/compose_kill.go index f6d6a1d6352..1166e24a371 100644 --- a/cmd/nerdctl/compose_kill.go +++ b/cmd/nerdctl/compose_kill.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -37,7 +38,7 @@ func newComposeKillCommand() *cobra.Command { } func composeKillAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_logs.go b/cmd/nerdctl/compose_logs.go index 79af0310b6d..c038d712866 100644 --- a/cmd/nerdctl/compose_logs.go +++ b/cmd/nerdctl/compose_logs.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -41,7 +42,7 @@ func newComposeLogsCommand() *cobra.Command { } func composeLogsAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_pause.go b/cmd/nerdctl/compose_pause.go index b280d5cf950..6250214a269 100644 --- a/cmd/nerdctl/compose_pause.go +++ b/cmd/nerdctl/compose_pause.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" ) @@ -36,7 +37,7 @@ func newComposePauseCommand() *cobra.Command { } func composePauseAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } @@ -71,7 +72,7 @@ func newComposeUnpauseCommand() *cobra.Command { } func composeUnpauseAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_port.go b/cmd/nerdctl/compose_port.go index 95035e833dd..e654f45f9f1 100644 --- a/cmd/nerdctl/compose_port.go +++ b/cmd/nerdctl/compose_port.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -43,7 +44,7 @@ func newComposePortCommand() *cobra.Command { } func composePortAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_ps.go b/cmd/nerdctl/compose_ps.go index b58f3cfc0ff..684428ad8c8 100644 --- a/cmd/nerdctl/compose_ps.go +++ b/cmd/nerdctl/compose_ps.go @@ -32,6 +32,7 @@ import ( gocni "github.com/containerd/go-cni" "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/containerutil" @@ -74,7 +75,7 @@ type composeContainerPrintable struct { } func composePsAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_pull.go b/cmd/nerdctl/compose_pull.go index 738c0b955e9..dc90dc17843 100644 --- a/cmd/nerdctl/compose_pull.go +++ b/cmd/nerdctl/compose_pull.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -37,7 +38,7 @@ func newComposePullCommand() *cobra.Command { } func composePullAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_push.go b/cmd/nerdctl/compose_push.go index cfa310a3e98..6aca0c64e5a 100644 --- a/cmd/nerdctl/compose_push.go +++ b/cmd/nerdctl/compose_push.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -36,7 +37,7 @@ func newComposePushCommand() *cobra.Command { } func composePushAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_restart.go b/cmd/nerdctl/compose_restart.go index a68f6e52408..170a3fc4b62 100644 --- a/cmd/nerdctl/compose_restart.go +++ b/cmd/nerdctl/compose_restart.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -37,7 +38,7 @@ func newComposeRestartCommand() *cobra.Command { } func composeRestartAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_rm.go b/cmd/nerdctl/compose_rm.go index f249770881c..b0c32b47c1f 100644 --- a/cmd/nerdctl/compose_rm.go +++ b/cmd/nerdctl/compose_rm.go @@ -43,7 +43,7 @@ func newComposeRemoveCommand() *cobra.Command { } func composeRemoveAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_run.go b/cmd/nerdctl/compose_run.go index 122e6be23bf..9f1ffa76c0b 100644 --- a/cmd/nerdctl/compose_run.go +++ b/cmd/nerdctl/compose_run.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -69,7 +70,7 @@ func newComposeRunCommand() *cobra.Command { } func composeRunAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_start.go b/cmd/nerdctl/compose_start.go index f0ce42cfe29..08ca46b33da 100644 --- a/cmd/nerdctl/compose_start.go +++ b/cmd/nerdctl/compose_start.go @@ -27,6 +27,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/errdefs" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/containerutil" @@ -46,7 +47,7 @@ func newComposeStartCommand() *cobra.Command { } func composeStartAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_stop.go b/cmd/nerdctl/compose_stop.go index 07bd789178e..7c769276700 100644 --- a/cmd/nerdctl/compose_stop.go +++ b/cmd/nerdctl/compose_stop.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -37,7 +38,7 @@ func newComposeStopCommand() *cobra.Command { } func composeStopAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_top.go b/cmd/nerdctl/compose_top.go index a3356b8face..f6466f2329b 100644 --- a/cmd/nerdctl/compose_top.go +++ b/cmd/nerdctl/compose_top.go @@ -23,6 +23,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" @@ -44,7 +45,7 @@ func newComposeTopCommand() *cobra.Command { } func composeTopAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/compose_up.go b/cmd/nerdctl/compose_up.go index a38c4f9dab7..b0c8fddab5e 100644 --- a/cmd/nerdctl/compose_up.go +++ b/cmd/nerdctl/compose_up.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/compose" "github.com/containerd/nerdctl/v2/pkg/composer" @@ -51,7 +52,7 @@ func newComposeUpCommand() *cobra.Command { } func composeUpAction(cmd *cobra.Command, services []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_attach.go b/cmd/nerdctl/container_attach.go index abb4d90db33..031915c46a7 100644 --- a/cmd/nerdctl/container_attach.go +++ b/cmd/nerdctl/container_attach.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -58,7 +59,7 @@ Caveats: } func processContainerAttachOptions(cmd *cobra.Command) (types.ContainerAttachOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerAttachOptions{}, err } diff --git a/cmd/nerdctl/container_commit.go b/cmd/nerdctl/container_commit.go index 96c79cbc66d..42a6539f246 100644 --- a/cmd/nerdctl/container_commit.go +++ b/cmd/nerdctl/container_commit.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -42,7 +43,7 @@ func newCommitCommand() *cobra.Command { } func processCommitCommandOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerCommitOptions{}, err } diff --git a/cmd/nerdctl/container_cp_linux.go b/cmd/nerdctl/container_cp_linux.go index 3a2c2aa84e1..184cbc66292 100644 --- a/cmd/nerdctl/container_cp_linux.go +++ b/cmd/nerdctl/container_cp_linux.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -78,7 +79,7 @@ func cpAction(cmd *cobra.Command, args []string) error { } func processCpOptions(cmd *cobra.Command, args []string) (types.ContainerCpOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerCpOptions{}, err } diff --git a/cmd/nerdctl/container_create.go b/cmd/nerdctl/container_create.go index 49ed0af26bb..304ae65db87 100644 --- a/cmd/nerdctl/container_create.go +++ b/cmd/nerdctl/container_create.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -61,7 +62,7 @@ func processContainerCreateOptions(cmd *cobra.Command) (types.ContainerCreateOpt Stderr: cmd.ErrOrStderr(), } - opt.GOptions, err = processRootCmdFlags(cmd) + opt.GOptions, err = helpers.ProcessRootCmdFlags(cmd) if err != nil { return opt, err } @@ -404,7 +405,7 @@ func processContainerCreateOptions(cmd *cobra.Command) (types.ContainerCreateOpt // #endregion // #region for image pull and verify options - imageVerifyOpt, err := processImageVerifyOptions(cmd) + imageVerifyOpt, err := helpers.ProcessImageVerifyOptions(cmd) if err != nil { return opt, err } diff --git a/cmd/nerdctl/container_diff.go b/cmd/nerdctl/container_diff.go index 08cd376d43c..1606345a8a7 100644 --- a/cmd/nerdctl/container_diff.go +++ b/cmd/nerdctl/container_diff.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/log" "github.com/containerd/platforms" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/idgen" @@ -55,7 +56,7 @@ func newDiffCommand() *cobra.Command { } func processContainerDiffOptions(cmd *cobra.Command) (types.ContainerDiffOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerDiffOptions{}, err } diff --git a/cmd/nerdctl/container_exec.go b/cmd/nerdctl/container_exec.go index 8e3b5d15d95..a9e68e53b9e 100644 --- a/cmd/nerdctl/container_exec.go +++ b/cmd/nerdctl/container_exec.go @@ -25,6 +25,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -56,7 +57,7 @@ func newExecCommand() *cobra.Command { } func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerExecOptions{}, err } diff --git a/cmd/nerdctl/container_inspect.go b/cmd/nerdctl/container_inspect.go index e178724617e..e2b11946c57 100644 --- a/cmd/nerdctl/container_inspect.go +++ b/cmd/nerdctl/container_inspect.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -56,7 +57,7 @@ var validModeType = map[string]bool{ } func processContainerInspectOptions(cmd *cobra.Command) (opt types.ContainerInspectOptions, err error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return } diff --git a/cmd/nerdctl/container_kill.go b/cmd/nerdctl/container_kill.go index 75154e63c3b..846c94c111a 100644 --- a/cmd/nerdctl/container_kill.go +++ b/cmd/nerdctl/container_kill.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -41,7 +42,7 @@ func newKillCommand() *cobra.Command { } func killAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_list.go b/cmd/nerdctl/container_list.go index f4f43be161a..d92de87a5de 100644 --- a/cmd/nerdctl/container_list.go +++ b/cmd/nerdctl/container_list.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -58,7 +59,7 @@ func newPsCommand() *cobra.Command { } func processOptions(cmd *cobra.Command) (types.ContainerListOptions, FormattingAndPrintingOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerListOptions{}, FormattingAndPrintingOptions{}, err } diff --git a/cmd/nerdctl/container_logs.go b/cmd/nerdctl/container_logs.go index 2e66c91e09e..4f9a2a2a39b 100644 --- a/cmd/nerdctl/container_logs.go +++ b/cmd/nerdctl/container_logs.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -55,7 +56,7 @@ The following containers are supported: } func processContainerLogsOptions(cmd *cobra.Command) (types.ContainerLogsOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerLogsOptions{}, err } diff --git a/cmd/nerdctl/container_pause.go b/cmd/nerdctl/container_pause.go index 8176e8c2f16..fba77b27a0a 100644 --- a/cmd/nerdctl/container_pause.go +++ b/cmd/nerdctl/container_pause.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -40,7 +41,7 @@ func newPauseCommand() *cobra.Command { } func processContainerPauseOptions(cmd *cobra.Command) (types.ContainerPauseOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerPauseOptions{}, err } diff --git a/cmd/nerdctl/container_port.go b/cmd/nerdctl/container_port.go index df98022ad73..dd9d7a73fd8 100644 --- a/cmd/nerdctl/container_port.go +++ b/cmd/nerdctl/container_port.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/containerutil" "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" @@ -43,7 +44,7 @@ func newPortCommand() *cobra.Command { } func portAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_prune.go b/cmd/nerdctl/container_prune.go index 35793a45409..960542157d5 100644 --- a/cmd/nerdctl/container_prune.go +++ b/cmd/nerdctl/container_prune.go @@ -39,7 +39,7 @@ func newContainerPruneCommand() *cobra.Command { } func processContainerPruneOptions(cmd *cobra.Command) (types.ContainerPruneOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerPruneOptions{}, err } diff --git a/cmd/nerdctl/container_remove.go b/cmd/nerdctl/container_remove.go index bdd415f836e..4e80f5a138a 100644 --- a/cmd/nerdctl/container_remove.go +++ b/cmd/nerdctl/container_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -41,7 +42,7 @@ func newRmCommand() *cobra.Command { } func rmAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container_rename.go index 915e1d2eb00..9e0400f7e1a 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container_rename.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -38,7 +39,7 @@ func newRenameCommand() *cobra.Command { } func processContainerRenameOptions(cmd *cobra.Command) (types.ContainerRenameOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerRenameOptions{}, err } diff --git a/cmd/nerdctl/container_restart.go b/cmd/nerdctl/container_restart.go index caf05341746..a29ac4dac1f 100644 --- a/cmd/nerdctl/container_restart.go +++ b/cmd/nerdctl/container_restart.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -41,7 +42,7 @@ func newRestartCommand() *cobra.Command { } func processContainerRestartOptions(cmd *cobra.Command) (types.ContainerRestartOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerRestartOptions{}, err } diff --git a/cmd/nerdctl/container_start.go b/cmd/nerdctl/container_start.go index 793624ecc3e..562a63567a1 100644 --- a/cmd/nerdctl/container_start.go +++ b/cmd/nerdctl/container_start.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -46,7 +47,7 @@ func newStartCommand() *cobra.Command { } func processContainerStartOptions(cmd *cobra.Command) (types.ContainerStartOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerStartOptions{}, err } diff --git a/cmd/nerdctl/container_stats.go b/cmd/nerdctl/container_stats.go index eaec1307c45..d07c5334698 100644 --- a/cmd/nerdctl/container_stats.go +++ b/cmd/nerdctl/container_stats.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -49,7 +50,7 @@ func addStatsFlags(cmd *cobra.Command) { } func processStatsCommandFlags(cmd *cobra.Command) (types.ContainerStatsOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerStatsOptions{}, err } diff --git a/cmd/nerdctl/container_stop.go b/cmd/nerdctl/container_stop.go index 571b9e29e87..fee0183c92c 100644 --- a/cmd/nerdctl/container_stop.go +++ b/cmd/nerdctl/container_stop.go @@ -23,6 +23,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -43,7 +44,7 @@ func newStopCommand() *cobra.Command { } func processContainerStopOptions(cmd *cobra.Command) (types.ContainerStopOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerStopOptions{}, err } diff --git a/cmd/nerdctl/container_top.go b/cmd/nerdctl/container_top.go index 168a4e978b0..5376895d96b 100644 --- a/cmd/nerdctl/container_top.go +++ b/cmd/nerdctl/container_top.go @@ -24,6 +24,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -48,7 +49,7 @@ func newTopCommand() *cobra.Command { func topAction(cmd *cobra.Command, args []string) error { // NOTE: rootless container does not rely on cgroupv1. // more details about possible ways to resolve this concern: #223 - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_unpause.go b/cmd/nerdctl/container_unpause.go index 98e3e9cb500..7fec1c06a6a 100644 --- a/cmd/nerdctl/container_unpause.go +++ b/cmd/nerdctl/container_unpause.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -40,7 +41,7 @@ func newUnpauseCommand() *cobra.Command { } func processContainerUnpauseOptions(cmd *cobra.Command) (types.ContainerUnpauseOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerUnpauseOptions{}, err } diff --git a/cmd/nerdctl/container_update.go b/cmd/nerdctl/container_update.go index 8f020071bec..f6b9357c661 100644 --- a/cmd/nerdctl/container_update.go +++ b/cmd/nerdctl/container_update.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/log" "github.com/containerd/typeurl/v2" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" nerdctlContainer "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -89,7 +90,7 @@ func setUpdateFlags(cmd *cobra.Command) { } func updateAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_wait.go b/cmd/nerdctl/container_wait.go index a4d962bf4ef..03b33abee58 100644 --- a/cmd/nerdctl/container_wait.go +++ b/cmd/nerdctl/container_wait.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -40,7 +41,7 @@ func newWaitCommand() *cobra.Command { } func processContainerWaitOptions(cmd *cobra.Command) (types.ContainerWaitOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ContainerWaitOptions{}, err } diff --git a/cmd/nerdctl/flagutil.go b/cmd/nerdctl/helpers/flagutil.go similarity index 81% rename from cmd/nerdctl/flagutil.go rename to cmd/nerdctl/helpers/flagutil.go index c88e7597af9..9ca11d449fd 100644 --- a/cmd/nerdctl/flagutil.go +++ b/cmd/nerdctl/helpers/flagutil.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package helpers import ( "github.com/spf13/cobra" @@ -22,20 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/api/types" ) -func processImageSignOptions(cmd *cobra.Command) (opt types.ImageSignOptions, err error) { - if opt.Provider, err = cmd.Flags().GetString("sign"); err != nil { - return - } - if opt.CosignKey, err = cmd.Flags().GetString("cosign-key"); err != nil { - return - } - if opt.NotationKeyName, err = cmd.Flags().GetString("notation-key-name"); err != nil { - return - } - return -} - -func processImageVerifyOptions(cmd *cobra.Command) (opt types.ImageVerifyOptions, err error) { +func ProcessImageVerifyOptions(cmd *cobra.Command) (opt types.ImageVerifyOptions, err error) { if opt.Provider, err = cmd.Flags().GetString("verify"); err != nil { return } @@ -57,17 +44,7 @@ func processImageVerifyOptions(cmd *cobra.Command) (opt types.ImageVerifyOptions return } -func processSociOptions(cmd *cobra.Command) (opt types.SociOptions, err error) { - if opt.SpanSize, err = cmd.Flags().GetInt64("soci-span-size"); err != nil { - return - } - if opt.MinLayerSize, err = cmd.Flags().GetInt64("soci-min-layer-size"); err != nil { - return - } - return -} - -func processRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error) { +func ProcessRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error) { debug, err := cmd.Flags().GetBool("debug") if err != nil { return types.GlobalCommandOptions{}, err diff --git a/cmd/nerdctl/image_convert.go b/cmd/nerdctl/image_convert.go index 355a2e9737a..ddefa598dc6 100644 --- a/cmd/nerdctl/image_convert.go +++ b/cmd/nerdctl/image_convert.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -103,7 +104,7 @@ func newImageConvertCommand() *cobra.Command { } func processImageConvertOptions(cmd *cobra.Command) (types.ImageConvertOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageConvertOptions{}, err } diff --git a/cmd/nerdctl/image_cryptutil.go b/cmd/nerdctl/image_cryptutil.go index 28b38dd1ff2..adee05a6b55 100644 --- a/cmd/nerdctl/image_cryptutil.go +++ b/cmd/nerdctl/image_cryptutil.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -54,7 +55,7 @@ func registerImgcryptFlags(cmd *cobra.Command, encrypt bool) { } func processImgCryptOptions(cmd *cobra.Command, args []string, encrypt bool) (types.ImageCryptOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageCryptOptions{}, err } diff --git a/cmd/nerdctl/image_history.go b/cmd/nerdctl/image_history.go index 4f77e8a819b..e22cb0601a3 100644 --- a/cmd/nerdctl/image_history.go +++ b/cmd/nerdctl/image_history.go @@ -35,6 +35,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker" @@ -78,7 +79,7 @@ type historyPrintable struct { } func historyAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/image_inspect.go b/cmd/nerdctl/image_inspect.go index 9cc7fbdc81f..0b0222cb043 100644 --- a/cmd/nerdctl/image_inspect.go +++ b/cmd/nerdctl/image_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -53,7 +54,7 @@ func newImageInspectCommand() *cobra.Command { } func processImageInspectOptions(cmd *cobra.Command, platform *string) (types.ImageInspectOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageInspectOptions{}, err } diff --git a/cmd/nerdctl/image_list.go b/cmd/nerdctl/image_list.go index 561b0c00956..71616a53989 100644 --- a/cmd/nerdctl/image_list.go +++ b/cmd/nerdctl/image_list.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -69,7 +70,7 @@ Properties: } func processImageListOptions(cmd *cobra.Command, args []string) (types.ImageListOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageListOptions{}, err } diff --git a/cmd/nerdctl/image_load.go b/cmd/nerdctl/image_load.go index 5e8e5a4228d..ed09919aaf0 100644 --- a/cmd/nerdctl/image_load.go +++ b/cmd/nerdctl/image_load.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -52,7 +53,7 @@ func processLoadCommandFlags(cmd *cobra.Command) (types.ImageLoadOptions, error) if err != nil { return types.ImageLoadOptions{}, err } - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageLoadOptions{}, err } diff --git a/cmd/nerdctl/image_prune.go b/cmd/nerdctl/image_prune.go index 507dea9566e..0480a6a544d 100644 --- a/cmd/nerdctl/image_prune.go +++ b/cmd/nerdctl/image_prune.go @@ -44,7 +44,7 @@ func newImagePruneCommand() *cobra.Command { } func processImagePruneOptions(cmd *cobra.Command) (types.ImagePruneOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImagePruneOptions{}, err } diff --git a/cmd/nerdctl/image_pull.go b/cmd/nerdctl/image_pull.go index afe8717aa4c..f54a007d08e 100644 --- a/cmd/nerdctl/image_pull.go +++ b/cmd/nerdctl/image_pull.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -71,7 +72,7 @@ func newPullCommand() *cobra.Command { } func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImagePullOptions{}, err } @@ -112,7 +113,7 @@ func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error) return types.ImagePullOptions{}, err } - verifyOptions, err := processImageVerifyOptions(cmd) + verifyOptions, err := helpers.ProcessImageVerifyOptions(cmd) if err != nil { return types.ImagePullOptions{}, err } diff --git a/cmd/nerdctl/image_push.go b/cmd/nerdctl/image_push.go index e04f8b5ef41..02e88885a40 100644 --- a/cmd/nerdctl/image_push.go +++ b/cmd/nerdctl/image_push.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -71,7 +72,7 @@ func newPushCommand() *cobra.Command { } func processImagePushOptions(cmd *cobra.Command) (types.ImagePushOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImagePushOptions{}, err } @@ -146,3 +147,26 @@ func pushShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] // show image names return shellCompleteImageNames(cmd) } + +func processImageSignOptions(cmd *cobra.Command) (opt types.ImageSignOptions, err error) { + if opt.Provider, err = cmd.Flags().GetString("sign"); err != nil { + return + } + if opt.CosignKey, err = cmd.Flags().GetString("cosign-key"); err != nil { + return + } + if opt.NotationKeyName, err = cmd.Flags().GetString("notation-key-name"); err != nil { + return + } + return +} + +func processSociOptions(cmd *cobra.Command) (opt types.SociOptions, err error) { + if opt.SpanSize, err = cmd.Flags().GetInt64("soci-span-size"); err != nil { + return + } + if opt.MinLayerSize, err = cmd.Flags().GetInt64("soci-min-layer-size"); err != nil { + return + } + return +} diff --git a/cmd/nerdctl/image_remove.go b/cmd/nerdctl/image_remove.go index f61a39ac6cc..5e677f642d5 100644 --- a/cmd/nerdctl/image_remove.go +++ b/cmd/nerdctl/image_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -41,7 +42,7 @@ func newRmiCommand() *cobra.Command { } func processImageRemoveOptions(cmd *cobra.Command) (types.ImageRemoveOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageRemoveOptions{}, err } diff --git a/cmd/nerdctl/image_save.go b/cmd/nerdctl/image_save.go index dc4e7a86a0a..9c744bdb324 100644 --- a/cmd/nerdctl/image_save.go +++ b/cmd/nerdctl/image_save.go @@ -23,6 +23,7 @@ import ( "github.com/mattn/go-isatty" "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -52,7 +53,7 @@ func newSaveCommand() *cobra.Command { } func processImageSaveOptions(cmd *cobra.Command) (types.ImageSaveOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageSaveOptions{}, err } diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image_tag.go index abf96ab28cf..ddc3995ad51 100644 --- a/cmd/nerdctl/image_tag.go +++ b/cmd/nerdctl/image_tag.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/image" @@ -38,7 +39,7 @@ func newTagCommand() *cobra.Command { } func tagAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/inspect.go b/cmd/nerdctl/inspect.go index 150b8e7dfcd..dad2b35ded8 100644 --- a/cmd/nerdctl/inspect.go +++ b/cmd/nerdctl/inspect.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -69,7 +70,7 @@ func addInspectFlags(cmd *cobra.Command) { } func inspectAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/internal_oci_hook.go b/cmd/nerdctl/internal_oci_hook.go index 455b41daf7e..4ebad5d2383 100644 --- a/cmd/nerdctl/internal_oci_hook.go +++ b/cmd/nerdctl/internal_oci_hook.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/ocihook" ) @@ -38,7 +39,7 @@ func newInternalOCIHookCommandCommand() *cobra.Command { } func internalOCIHookAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/login.go b/cmd/nerdctl/login.go index 7772692f5ee..047b712d529 100644 --- a/cmd/nerdctl/login.go +++ b/cmd/nerdctl/login.go @@ -25,6 +25,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/login" ) @@ -45,7 +46,7 @@ func newLoginCommand() *cobra.Command { } func processLoginOptions(cmd *cobra.Command) (types.LoginCommandOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.LoginCommandOptions{}, err } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index e4af1c72fbe..f875052a09f 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -32,6 +32,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/config" ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" "github.com/containerd/nerdctl/v2/pkg/errutil" @@ -208,7 +209,7 @@ Config file ($NERDCTL_TOML): %s } rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } @@ -565,7 +566,7 @@ func AddPersistentStringArrayFlag(cmd *cobra.Command, name string, aliases, nonP func checkExperimental(feature string) func(cmd *cobra.Command, args []string) error { return func(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/main_unix.go b/cmd/nerdctl/main_unix.go index af6bb4d2434..e66bdfb8950 100644 --- a/cmd/nerdctl/main_unix.go +++ b/cmd/nerdctl/main_unix.go @@ -23,13 +23,14 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/infoutil" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } @@ -55,7 +56,7 @@ func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete s } func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError } diff --git a/cmd/nerdctl/namespace.go b/cmd/nerdctl/namespace.go index 3faa08f09ea..3086c9d06a6 100644 --- a/cmd/nerdctl/namespace.go +++ b/cmd/nerdctl/namespace.go @@ -28,6 +28,7 @@ import ( "github.com/containerd/containerd/v2/pkg/namespaces" "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/mountutil/volumestore" ) @@ -65,7 +66,7 @@ func newNamespaceLsCommand() *cobra.Command { } func namespaceLsAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/namespace_create.go b/cmd/nerdctl/namespace_create.go index ee77ba54daf..61700c56d19 100644 --- a/cmd/nerdctl/namespace_create.go +++ b/cmd/nerdctl/namespace_create.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/namespace" @@ -39,7 +40,7 @@ func newNamespaceCreateCommand() *cobra.Command { } func processNamespaceCreateCommandOption(cmd *cobra.Command) (types.NamespaceCreateOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.NamespaceCreateOptions{}, err } diff --git a/cmd/nerdctl/namespace_inspect.go b/cmd/nerdctl/namespace_inspect.go index 5df289b35f1..580ed4689e9 100644 --- a/cmd/nerdctl/namespace_inspect.go +++ b/cmd/nerdctl/namespace_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/namespace" @@ -41,7 +42,7 @@ func newNamespaceInspectCommand() *cobra.Command { } func processNamespaceInspectOptions(cmd *cobra.Command) (types.NamespaceInspectOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.NamespaceInspectOptions{}, err } diff --git a/cmd/nerdctl/namespace_remove.go b/cmd/nerdctl/namespace_remove.go index aa9a887a9b0..6ba48fe7200 100644 --- a/cmd/nerdctl/namespace_remove.go +++ b/cmd/nerdctl/namespace_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/namespace" @@ -39,7 +40,7 @@ func newNamespaceRmCommand() *cobra.Command { } func processNamespaceRemoveOptions(cmd *cobra.Command) (types.NamespaceRemoveOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.NamespaceRemoveOptions{}, err } diff --git a/cmd/nerdctl/namespace_update.go b/cmd/nerdctl/namespace_update.go index 782d43d7b4f..6debd47cae4 100644 --- a/cmd/nerdctl/namespace_update.go +++ b/cmd/nerdctl/namespace_update.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/namespace" @@ -38,7 +39,7 @@ func newNamespacelabelUpdateCommand() *cobra.Command { } func processNamespaceUpdateCommandOption(cmd *cobra.Command) (types.NamespaceUpdateOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.NamespaceUpdateOptions{}, err } diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network_create.go index 9a33cb5c67d..a14f30777a4 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network_create.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/network" "github.com/containerd/nerdctl/v2/pkg/identifiers" @@ -52,7 +53,7 @@ func newNetworkCreateCommand() *cobra.Command { } func networkCreateAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/network_inspect.go b/cmd/nerdctl/network_inspect.go index ad3bb6eaa91..a1952e761c3 100644 --- a/cmd/nerdctl/network_inspect.go +++ b/cmd/nerdctl/network_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/network" ) @@ -45,7 +46,7 @@ func newNetworkInspectCommand() *cobra.Command { } func networkInspectAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/network_list.go b/cmd/nerdctl/network_list.go index de0f35e5d54..8cc8ecde0c8 100644 --- a/cmd/nerdctl/network_list.go +++ b/cmd/nerdctl/network_list.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/network" ) @@ -43,7 +44,7 @@ func newNetworkLsCommand() *cobra.Command { } func networkLsAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/network_prune.go b/cmd/nerdctl/network_prune.go index 6e1430b22e7..3c68263c0ef 100644 --- a/cmd/nerdctl/network_prune.go +++ b/cmd/nerdctl/network_prune.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/network" @@ -43,7 +44,7 @@ func newNetworkPruneCommand() *cobra.Command { } func networkPruneAction(cmd *cobra.Command, _ []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/network_remove.go b/cmd/nerdctl/network_remove.go index 80eb48ac98d..dac95ed6af4 100644 --- a/cmd/nerdctl/network_remove.go +++ b/cmd/nerdctl/network_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/network" @@ -41,7 +42,7 @@ func newNetworkRmCommand() *cobra.Command { } func networkRmAction(cmd *cobra.Command, args []string) error { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/system_events.go b/cmd/nerdctl/system_events.go index 65eaacff4ba..bc4dcfb57f6 100644 --- a/cmd/nerdctl/system_events.go +++ b/cmd/nerdctl/system_events.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/system" @@ -45,7 +46,7 @@ func newEventsCommand() *cobra.Command { } func processSystemEventsOptions(cmd *cobra.Command) (types.SystemEventsOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.SystemEventsOptions{}, err } diff --git a/cmd/nerdctl/system_info.go b/cmd/nerdctl/system_info.go index b2562319a97..0d461c99a77 100644 --- a/cmd/nerdctl/system_info.go +++ b/cmd/nerdctl/system_info.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/system" @@ -45,7 +46,7 @@ func newInfoCommand() *cobra.Command { } func processInfoOptions(cmd *cobra.Command) (types.SystemInfoOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.SystemInfoOptions{}, err } diff --git a/cmd/nerdctl/system_prune.go b/cmd/nerdctl/system_prune.go index ed3e8cd370f..e8e384cf5fb 100644 --- a/cmd/nerdctl/system_prune.go +++ b/cmd/nerdctl/system_prune.go @@ -24,6 +24,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/system" @@ -45,7 +46,7 @@ func newSystemPruneCommand() *cobra.Command { } func processSystemPruneOptions(cmd *cobra.Command) (types.SystemPruneOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.SystemPruneOptions{}, err } diff --git a/cmd/nerdctl/version.go b/cmd/nerdctl/version.go index 6eb2528dce5..627c8b1940a 100644 --- a/cmd/nerdctl/version.go +++ b/cmd/nerdctl/version.go @@ -27,6 +27,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/infoutil" @@ -53,7 +54,7 @@ func newVersionCommand() *cobra.Command { func versionAction(cmd *cobra.Command, args []string) error { var w io.Writer = os.Stdout var tmpl *template.Template - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/volume_create.go b/cmd/nerdctl/volume_create.go index fefc7d2c0c4..95ef82d8b52 100644 --- a/cmd/nerdctl/volume_create.go +++ b/cmd/nerdctl/volume_create.go @@ -23,6 +23,7 @@ import ( "github.com/containerd/errdefs" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" ) @@ -41,7 +42,7 @@ func newVolumeCreateCommand() *cobra.Command { } func processVolumeCreateOptions(cmd *cobra.Command) (types.VolumeCreateOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.VolumeCreateOptions{}, err } diff --git a/cmd/nerdctl/volume_inspect.go b/cmd/nerdctl/volume_inspect.go index 9c76242b7f3..f38b5269744 100644 --- a/cmd/nerdctl/volume_inspect.go +++ b/cmd/nerdctl/volume_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" ) @@ -42,7 +43,7 @@ func newVolumeInspectCommand() *cobra.Command { } func processVolumeInspectOptions(cmd *cobra.Command) (types.VolumeInspectOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.VolumeInspectOptions{}, err } diff --git a/cmd/nerdctl/volume_list.go b/cmd/nerdctl/volume_list.go index b0a7f0783e1..70842abd16a 100644 --- a/cmd/nerdctl/volume_list.go +++ b/cmd/nerdctl/volume_list.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" @@ -46,7 +47,7 @@ func newVolumeLsCommand() *cobra.Command { } func processVolumeLsOptions(cmd *cobra.Command) (types.VolumeListOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.VolumeListOptions{}, err } diff --git a/cmd/nerdctl/volume_prune.go b/cmd/nerdctl/volume_prune.go index 1cc8d1168ce..1833d67dd99 100644 --- a/cmd/nerdctl/volume_prune.go +++ b/cmd/nerdctl/volume_prune.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" @@ -42,7 +43,7 @@ func newVolumePruneCommand() *cobra.Command { } func processVolumePruneOptions(cmd *cobra.Command) (types.VolumePruneOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.VolumePruneOptions{}, err } diff --git a/cmd/nerdctl/volume_remove.go b/cmd/nerdctl/volume_remove.go index 4acc340e172..67c142ec50f 100644 --- a/cmd/nerdctl/volume_remove.go +++ b/cmd/nerdctl/volume_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" @@ -41,7 +42,7 @@ func newVolumeRmCommand() *cobra.Command { } func processVolumeRmOptions(cmd *cobra.Command) (types.VolumeRemoveOptions, error) { - globalOptions, err := processRootCmdFlags(cmd) + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.VolumeRemoveOptions{}, err } From 38bca1f32e0cd30755a5c6619ad26650a611fd1b Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 12:02:39 -0700 Subject: [PATCH 03/16] Move completion to sub package Signed-off-by: apostasie --- cmd/nerdctl/apparmor_unload_linux.go | 3 +- cmd/nerdctl/builder_build.go | 3 +- cmd/nerdctl/{ => completion}/completion.go | 26 ++++-- cmd/nerdctl/completion/completion_freebsd.go | 23 +++++ .../{ => completion}/completion_linux.go | 17 +++- .../{ => completion}/completion_linux_test.go | 2 +- cmd/nerdctl/completion/completion_test.go | 27 ++++++ cmd/nerdctl/completion/completion_unix.go | 88 +++++++++++++++++++ cmd/nerdctl/completion/completion_windows.go | 40 +++++++++ cmd/nerdctl/container_attach.go | 3 +- cmd/nerdctl/container_commit.go | 3 +- cmd/nerdctl/container_diff.go | 3 +- cmd/nerdctl/container_exec.go | 3 +- cmd/nerdctl/container_inspect.go | 3 +- cmd/nerdctl/container_kill.go | 3 +- cmd/nerdctl/container_logs.go | 3 +- cmd/nerdctl/container_pause.go | 3 +- cmd/nerdctl/container_port.go | 3 +- cmd/nerdctl/container_remove.go | 3 +- cmd/nerdctl/container_rename.go | 3 +- cmd/nerdctl/container_run.go | 7 +- cmd/nerdctl/container_run_linux.go | 4 +- cmd/nerdctl/container_start.go | 3 +- cmd/nerdctl/container_stats.go | 3 +- cmd/nerdctl/container_stop.go | 3 +- cmd/nerdctl/container_top.go | 3 +- cmd/nerdctl/container_unpause.go | 3 +- cmd/nerdctl/container_update.go | 3 +- cmd/nerdctl/container_wait.go | 3 +- cmd/nerdctl/image_convert.go | 5 +- cmd/nerdctl/image_cryptutil.go | 5 +- cmd/nerdctl/image_history.go | 3 +- cmd/nerdctl/image_inspect.go | 5 +- cmd/nerdctl/image_list.go | 3 +- cmd/nerdctl/image_load.go | 3 +- cmd/nerdctl/image_pull.go | 3 +- cmd/nerdctl/image_push.go | 5 +- cmd/nerdctl/image_remove.go | 3 +- cmd/nerdctl/image_save.go | 5 +- cmd/nerdctl/image_tag.go | 3 +- cmd/nerdctl/inspect.go | 5 +- cmd/nerdctl/main.go | 9 +- cmd/nerdctl/main_freebsd.go | 4 - cmd/nerdctl/main_linux.go | 12 --- cmd/nerdctl/main_unix.go | 60 ------------- cmd/nerdctl/main_windows.go | 12 --- cmd/nerdctl/network_create.go | 5 +- cmd/nerdctl/network_create_unix.go | 15 +--- cmd/nerdctl/network_create_windows.go | 15 +--- cmd/nerdctl/network_inspect.go | 3 +- cmd/nerdctl/network_remove.go | 3 +- cmd/nerdctl/volume_inspect.go | 3 +- cmd/nerdctl/volume_list.go | 10 --- cmd/nerdctl/volume_remove.go | 3 +- 54 files changed, 308 insertions(+), 188 deletions(-) rename cmd/nerdctl/{ => completion}/completion.go (81%) create mode 100644 cmd/nerdctl/completion/completion_freebsd.go rename cmd/nerdctl/{ => completion}/completion_linux.go (62%) rename cmd/nerdctl/{ => completion}/completion_linux_test.go (99%) create mode 100644 cmd/nerdctl/completion/completion_test.go create mode 100644 cmd/nerdctl/completion/completion_unix.go create mode 100644 cmd/nerdctl/completion/completion_windows.go diff --git a/cmd/nerdctl/apparmor_unload_linux.go b/cmd/nerdctl/apparmor_unload_linux.go index 8ab8e1bf2c6..1fc82bc52bd 100644 --- a/cmd/nerdctl/apparmor_unload_linux.go +++ b/cmd/nerdctl/apparmor_unload_linux.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/pkg/cmd/apparmor" "github.com/containerd/nerdctl/v2/pkg/defaults" ) @@ -47,5 +48,5 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error { } func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteApparmorProfiles(cmd) + return completion.ShellCompleteApparmorProfiles(cmd) } diff --git a/cmd/nerdctl/builder_build.go b/cmd/nerdctl/builder_build.go index e496a16fc98..db2a20318bd 100644 --- a/cmd/nerdctl/builder_build.go +++ b/cmd/nerdctl/builder_build.go @@ -25,6 +25,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/buildkitutil" @@ -72,7 +73,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")") - buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + buildCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) buildCommand.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)") // #endregion diff --git a/cmd/nerdctl/completion.go b/cmd/nerdctl/completion/completion.go similarity index 81% rename from cmd/nerdctl/completion.go rename to cmd/nerdctl/completion/completion.go index bf03fcdd7a9..8a5a447a835 100644 --- a/cmd/nerdctl/completion.go +++ b/cmd/nerdctl/completion/completion.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package completion import ( "context" @@ -25,12 +25,15 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" + "github.com/containerd/nerdctl/v2/pkg/cmd/volume" + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" "github.com/containerd/nerdctl/v2/pkg/labels" "github.com/containerd/nerdctl/v2/pkg/netutil" ) -func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func ShellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -53,7 +56,7 @@ func shellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirec return candidates, cobra.ShellCompDirectiveNoFileComp } -func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { +func ShellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -101,8 +104,8 @@ func shellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd. return candidates, cobra.ShellCompDirectiveNoFileComp } -// shellCompleteNetworkNames includes {"bridge","host","none"} -func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { +// ShellCompleteNetworkNames includes {"bridge","host","none"} +func ShellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -134,7 +137,7 @@ func shellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, return candidates, cobra.ShellCompDirectiveNoFileComp } -func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func ShellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -150,7 +153,7 @@ func shellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDire return candidates, cobra.ShellCompDirectiveNoFileComp } -func shellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func ShellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { candidates := []string{ "amd64", "arm64", @@ -163,3 +166,12 @@ func shellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string } return candidates, cobra.ShellCompDirectiveNoFileComp } + +func getVolumes(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (map[string]native.Volume, error) { + volumeSize, err := cmd.Flags().GetBool("size") + if err != nil { + // The `nerdctl volume rm` does not have the flag `size`, so set it to false as the default value. + volumeSize = false + } + return volume.Volumes(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address, volumeSize, nil) +} diff --git a/cmd/nerdctl/completion/completion_freebsd.go b/cmd/nerdctl/completion/completion_freebsd.go new file mode 100644 index 00000000000..5ebfd57f1b6 --- /dev/null +++ b/cmd/nerdctl/completion/completion_freebsd.go @@ -0,0 +1,23 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package completion + +import "github.com/spf13/cobra" + +func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return nil, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/nerdctl/completion_linux.go b/cmd/nerdctl/completion/completion_linux.go similarity index 62% rename from cmd/nerdctl/completion_linux.go rename to cmd/nerdctl/completion/completion_linux.go index 12dd8297b3e..f94a2a018a3 100644 --- a/cmd/nerdctl/completion_linux.go +++ b/cmd/nerdctl/completion/completion_linux.go @@ -14,15 +14,17 @@ limitations under the License. */ -package main +package completion import ( "github.com/spf13/cobra" "github.com/containerd/nerdctl/v2/pkg/apparmorutil" + ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" + "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) -func shellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func ShellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { profiles, err := apparmorutil.Profiles() if err != nil { return nil, cobra.ShellCompDirectiveError @@ -33,3 +35,14 @@ func shellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCom } return names, cobra.ShellCompDirectiveNoFileComp } + +func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + candidates := []string{"cgroupfs"} + if ncdefaults.IsSystemdAvailable() { + candidates = append(candidates, "systemd") + } + if rootlessutil.IsRootless() { + candidates = append(candidates, "none") + } + return candidates, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/nerdctl/completion_linux_test.go b/cmd/nerdctl/completion/completion_linux_test.go similarity index 99% rename from cmd/nerdctl/completion_linux_test.go rename to cmd/nerdctl/completion/completion_linux_test.go index 13d96163d61..8abb5483964 100644 --- a/cmd/nerdctl/completion_linux_test.go +++ b/cmd/nerdctl/completion/completion_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package completion import ( "testing" diff --git a/cmd/nerdctl/completion/completion_test.go b/cmd/nerdctl/completion/completion_test.go new file mode 100644 index 00000000000..f37df496baf --- /dev/null +++ b/cmd/nerdctl/completion/completion_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package completion + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/completion/completion_unix.go b/cmd/nerdctl/completion/completion_unix.go new file mode 100644 index 00000000000..1379c825f1c --- /dev/null +++ b/cmd/nerdctl/completion/completion_unix.go @@ -0,0 +1,88 @@ +//go:build unix + +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package completion + +import ( + "github.com/spf13/cobra" + + "github.com/containerd/log" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/pkg/clientutil" + "github.com/containerd/nerdctl/v2/pkg/infoutil" + "github.com/containerd/nerdctl/v2/pkg/rootlessutil" +) + +func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + candidates := []string{"bridge", "macvlan", "ipvlan"} + return candidates, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return []string{"default", "host-local", "dhcp"}, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + if rootlessutil.IsRootlessParent() { + _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) + return nil, cobra.ShellCompDirectiveNoFileComp + } + + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + defer cancel() + nsService := client.NamespaceService() + nsList, err := nsService.List(ctx) + if err != nil { + log.L.Warn(err) + return nil, cobra.ShellCompDirectiveError + } + var candidates []string + candidates = append(candidates, nsList...) + return candidates, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + if rootlessutil.IsRootlessParent() { + _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) + return nil, cobra.ShellCompDirectiveNoFileComp + } + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + defer cancel() + snapshotterPlugins, err := infoutil.GetSnapshotterNames(ctx, client.IntrospectionService()) + if err != nil { + return nil, cobra.ShellCompDirectiveError + } + var candidates []string + candidates = append(candidates, snapshotterPlugins...) + return candidates, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/nerdctl/completion/completion_windows.go b/cmd/nerdctl/completion/completion_windows.go new file mode 100644 index 00000000000..1739017b95b --- /dev/null +++ b/cmd/nerdctl/completion/completion_windows.go @@ -0,0 +1,40 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package completion + +import "github.com/spf13/cobra" + +func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return nil, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return nil, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return nil, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + candidates := []string{"nat"} + return candidates, cobra.ShellCompDirectiveNoFileComp +} + +func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return []string{"default"}, cobra.ShellCompDirectiveNoFileComp +} diff --git a/cmd/nerdctl/container_attach.go b/cmd/nerdctl/container_attach.go index 031915c46a7..011184fbb78 100644 --- a/cmd/nerdctl/container_attach.go +++ b/cmd/nerdctl/container_attach.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -95,5 +96,5 @@ func attachShellComplete(cmd *cobra.Command, args []string, toComplete string) ( statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_commit.go b/cmd/nerdctl/container_commit.go index 42a6539f246..e531150a8b2 100644 --- a/cmd/nerdctl/container_commit.go +++ b/cmd/nerdctl/container_commit.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -93,7 +94,7 @@ func commitAction(cmd *cobra.Command, args []string) error { func commitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_diff.go b/cmd/nerdctl/container_diff.go index 1606345a8a7..051c84e1ec7 100644 --- a/cmd/nerdctl/container_diff.go +++ b/cmd/nerdctl/container_diff.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/log" "github.com/containerd/platforms" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -226,5 +227,5 @@ func appendChanges(changes []fs.Change, new fs.Change) []fs.Change { func diffShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_exec.go b/cmd/nerdctl/container_exec.go index a9e68e53b9e..c1bcb9180d5 100644 --- a/cmd/nerdctl/container_exec.go +++ b/cmd/nerdctl/container_exec.go @@ -25,6 +25,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -157,7 +158,7 @@ func execShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_inspect.go b/cmd/nerdctl/container_inspect.go index e2b11946c57..230986a70d8 100644 --- a/cmd/nerdctl/container_inspect.go +++ b/cmd/nerdctl/container_inspect.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -104,5 +105,5 @@ func containerInspectAction(cmd *cobra.Command, args []string) error { func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_kill.go b/cmd/nerdctl/container_kill.go index 846c94c111a..85e5c75cc82 100644 --- a/cmd/nerdctl/container_kill.go +++ b/cmd/nerdctl/container_kill.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -71,5 +72,5 @@ func killShellComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobr statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Stopped && st != containerd.Created && st != containerd.Unknown } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_logs.go b/cmd/nerdctl/container_logs.go index 4f9a2a2a39b..6569f30da93 100644 --- a/cmd/nerdctl/container_logs.go +++ b/cmd/nerdctl/container_logs.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -116,7 +117,7 @@ func logsAction(cmd *cobra.Command, args []string) error { func logsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names (TODO: only show containers with logs) - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } // Attempts to parse the argument given to `-n/--tail` as a uint. diff --git a/cmd/nerdctl/container_pause.go b/cmd/nerdctl/container_pause.go index fba77b27a0a..8cbea33b2b9 100644 --- a/cmd/nerdctl/container_pause.go +++ b/cmd/nerdctl/container_pause.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -71,5 +72,5 @@ func pauseShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_port.go b/cmd/nerdctl/container_port.go index dd9d7a73fd8..984464871f4 100644 --- a/cmd/nerdctl/container_port.go +++ b/cmd/nerdctl/container_port.go @@ -24,6 +24,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/containerutil" @@ -100,5 +101,5 @@ func portAction(cmd *cobra.Command, args []string) error { } func portShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_remove.go b/cmd/nerdctl/container_remove.go index 4e80f5a138a..9adf2ee7319 100644 --- a/cmd/nerdctl/container_remove.go +++ b/cmd/nerdctl/container_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -72,5 +73,5 @@ func rmAction(cmd *cobra.Command, args []string) error { func rmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container_rename.go index 9e0400f7e1a..f9938d64d3b 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container_rename.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -62,5 +63,5 @@ func renameAction(cmd *cobra.Command, args []string) error { return container.Rename(ctx, client, args[0], args[1], options) } func renameShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_run.go b/cmd/nerdctl/container_run.go index 1a8b46c1646..514b0262268 100644 --- a/cmd/nerdctl/container_run.go +++ b/cmd/nerdctl/container_run.go @@ -27,6 +27,7 @@ import ( "github.com/containerd/console" "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/pkg/annotations" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -105,18 +106,18 @@ func setCreateFlags(cmd *cobra.Command) { // #region platform flags cmd.Flags().String("platform", "", "Set platform (e.g. \"amd64\", \"arm64\")") // not a slice, and there is no --all-platforms - cmd.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + cmd.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) // #endregion // #region network flags // network (net) is defined as StringSlice, not StringArray, to allow specifying "--network=cni1,cni2" cmd.Flags().StringSlice("network", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|"container:"|)`) cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteNetworkNames(cmd, []string{}) + return completion.ShellCompleteNetworkNames(cmd, []string{}) }) cmd.Flags().StringSlice("net", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|)`) cmd.RegisterFlagCompletionFunc("net", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteNetworkNames(cmd, []string{}) + return completion.ShellCompleteNetworkNames(cmd, []string{}) }) // dns is defined as StringSlice, not StringArray, to allow specifying "--dns=1.1.1.1,8.8.8.8" (compatible with Podman) cmd.Flags().StringSlice("dns", nil, "Set custom DNS servers") diff --git a/cmd/nerdctl/container_run_linux.go b/cmd/nerdctl/container_run_linux.go index 28d3f543160..c4059796317 100644 --- a/cmd/nerdctl/container_run_linux.go +++ b/cmd/nerdctl/container_run_linux.go @@ -22,6 +22,8 @@ import ( "github.com/spf13/cobra" "github.com/containerd/containerd/v2/pkg/cap" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" ) func capShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { @@ -36,7 +38,7 @@ func capShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s func runShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_start.go b/cmd/nerdctl/container_start.go index 562a63567a1..446108f3b7f 100644 --- a/cmd/nerdctl/container_start.go +++ b/cmd/nerdctl/container_start.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -87,5 +88,5 @@ func startShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Running && st != containerd.Unknown } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_stats.go b/cmd/nerdctl/container_stats.go index d07c5334698..a28c118a0a0 100644 --- a/cmd/nerdctl/container_stats.go +++ b/cmd/nerdctl/container_stats.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -106,5 +107,5 @@ func statsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_stop.go b/cmd/nerdctl/container_stop.go index fee0183c92c..7789eccee76 100644 --- a/cmd/nerdctl/container_stop.go +++ b/cmd/nerdctl/container_stop.go @@ -23,6 +23,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -85,5 +86,5 @@ func stopShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Stopped && st != containerd.Created && st != containerd.Unknown } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_top.go b/cmd/nerdctl/container_top.go index 5376895d96b..2c1910bf1d2 100644 --- a/cmd/nerdctl/container_top.go +++ b/cmd/nerdctl/container_top.go @@ -24,6 +24,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -77,5 +78,5 @@ func topShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_unpause.go b/cmd/nerdctl/container_unpause.go index 7fec1c06a6a..043791bb12a 100644 --- a/cmd/nerdctl/container_unpause.go +++ b/cmd/nerdctl/container_unpause.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -71,5 +72,5 @@ func unpauseShellComplete(cmd *cobra.Command, args []string, toComplete string) statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Paused } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_update.go b/cmd/nerdctl/container_update.go index f6b9357c661..4adedd36968 100644 --- a/cmd/nerdctl/container_update.go +++ b/cmd/nerdctl/container_update.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/log" "github.com/containerd/typeurl/v2" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -398,5 +399,5 @@ func copySpec(spec *runtimespec.Spec) (*runtimespec.Spec, error) { } func updateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return shellCompleteContainerNames(cmd, nil) + return completion.ShellCompleteContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_wait.go b/cmd/nerdctl/container_wait.go index 03b33abee58..7756ce9d393 100644 --- a/cmd/nerdctl/container_wait.go +++ b/cmd/nerdctl/container_wait.go @@ -21,6 +21,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -71,5 +72,5 @@ func waitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return shellCompleteContainerNames(cmd, statusFilterFn) + return completion.ShellCompleteContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/image_convert.go b/cmd/nerdctl/image_convert.go index ddefa598dc6..49b7c7c40ce 100644 --- a/cmd/nerdctl/image_convert.go +++ b/cmd/nerdctl/image_convert.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -96,7 +97,7 @@ func newImageConvertCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" imageConvertCommand.Flags().StringSlice("platform", []string{}, "Convert content for a specific platform") - imageConvertCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + imageConvertCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) imageConvertCommand.Flags().Bool("all-platforms", false, "Convert content for all platforms") // #endregion @@ -298,5 +299,5 @@ func imageConvertAction(cmd *cobra.Command, args []string) error { func imageConvertShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_cryptutil.go b/cmd/nerdctl/image_cryptutil.go index adee05a6b55..db4a2cb172e 100644 --- a/cmd/nerdctl/image_cryptutil.go +++ b/cmd/nerdctl/image_cryptutil.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -37,7 +38,7 @@ func registerImgcryptFlags(cmd *cobra.Command, encrypt bool) { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" flags.StringSlice("platform", []string{}, "Convert content for a specific platform") - cmd.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + cmd.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) flags.Bool("all-platforms", false, "Convert content for all platforms") // #endregion @@ -124,5 +125,5 @@ func getImgcryptAction(encrypt bool) func(cmd *cobra.Command, args []string) err func imgcryptShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_history.go b/cmd/nerdctl/image_history.go index e22cb0601a3..78ef813a506 100644 --- a/cmd/nerdctl/image_history.go +++ b/cmd/nerdctl/image_history.go @@ -35,6 +35,7 @@ import ( containerd "github.com/containerd/containerd/v2/client" "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/formatter" @@ -271,5 +272,5 @@ func (x *historyPrinter) printHistory(printable historyPrintable) error { func historyShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_inspect.go b/cmd/nerdctl/image_inspect.go index 0b0222cb043..d86be752100 100644 --- a/cmd/nerdctl/image_inspect.go +++ b/cmd/nerdctl/image_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -47,7 +48,7 @@ func newImageInspectCommand() *cobra.Command { // #region platform flags imageInspectCommand.Flags().String("platform", "", "Inspect a specific platform") // not a slice, and there is no --all-platforms - imageInspectCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + imageInspectCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) // #endregion return imageInspectCommand @@ -99,5 +100,5 @@ func imageInspectAction(cmd *cobra.Command, args []string) error { func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_list.go b/cmd/nerdctl/image_list.go index 71616a53989..e8851e17ef4 100644 --- a/cmd/nerdctl/image_list.go +++ b/cmd/nerdctl/image_list.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -147,7 +148,7 @@ func imagesAction(cmd *cobra.Command, args []string) error { func imagesShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/image_load.go b/cmd/nerdctl/image_load.go index ed09919aaf0..2c361335dbc 100644 --- a/cmd/nerdctl/image_load.go +++ b/cmd/nerdctl/image_load.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -41,7 +42,7 @@ func newLoadCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" loadCommand.Flags().StringSlice("platform", []string{}, "Import content for a specific platform") - loadCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + loadCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) loadCommand.Flags().Bool("all-platforms", false, "Import content for all platforms") // #endregion diff --git a/cmd/nerdctl/image_pull.go b/cmd/nerdctl/image_pull.go index f54a007d08e..9da715044a9 100644 --- a/cmd/nerdctl/image_pull.go +++ b/cmd/nerdctl/image_pull.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -44,7 +45,7 @@ func newPullCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" pullCommand.Flags().StringSlice("platform", nil, "Pull content for a specific platform") - pullCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + pullCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) pullCommand.Flags().Bool("all-platforms", false, "Pull content for all platforms") // #endregion diff --git a/cmd/nerdctl/image_push.go b/cmd/nerdctl/image_push.go index 02e88885a40..374a47d4c75 100644 --- a/cmd/nerdctl/image_push.go +++ b/cmd/nerdctl/image_push.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -42,7 +43,7 @@ func newPushCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" pushCommand.Flags().StringSlice("platform", []string{}, "Push content for a specific platform") - pushCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + pushCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) pushCommand.Flags().Bool("all-platforms", false, "Push content for all platforms") // #endregion @@ -145,7 +146,7 @@ func pushAction(cmd *cobra.Command, args []string) error { func pushShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } func processImageSignOptions(cmd *cobra.Command) (opt types.ImageSignOptions, err error) { diff --git a/cmd/nerdctl/image_remove.go b/cmd/nerdctl/image_remove.go index 5e677f642d5..0173b2b34e0 100644 --- a/cmd/nerdctl/image_remove.go +++ b/cmd/nerdctl/image_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -81,5 +82,5 @@ func rmiAction(cmd *cobra.Command, args []string) error { func rmiShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_save.go b/cmd/nerdctl/image_save.go index 9c744bdb324..3dc0eb539a8 100644 --- a/cmd/nerdctl/image_save.go +++ b/cmd/nerdctl/image_save.go @@ -23,6 +23,7 @@ import ( "github.com/mattn/go-isatty" "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -45,7 +46,7 @@ func newSaveCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" saveCommand.Flags().StringSlice("platform", []string{}, "Export content for a specific platform") - saveCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms) + saveCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) saveCommand.Flags().Bool("all-platforms", false, "Export content for all platforms") // #endregion @@ -110,5 +111,5 @@ func saveAction(cmd *cobra.Command, args []string) error { func saveShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image_tag.go index ddc3995ad51..d0bc52003c5 100644 --- a/cmd/nerdctl/image_tag.go +++ b/cmd/nerdctl/image_tag.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -62,7 +63,7 @@ func tagAction(cmd *cobra.Command, args []string) error { func tagShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) < 2 { // show image names - return shellCompleteImageNames(cmd) + return completion.ShellCompleteImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/inspect.go b/cmd/nerdctl/inspect.go index dad2b35ded8..e74860a5371 100644 --- a/cmd/nerdctl/inspect.go +++ b/cmd/nerdctl/inspect.go @@ -22,6 +22,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -166,8 +167,8 @@ func inspectAction(cmd *cobra.Command, args []string) error { func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - containers, _ := shellCompleteContainerNames(cmd, nil) + containers, _ := completion.ShellCompleteContainerNames(cmd, nil) // show image names - images, _ := shellCompleteImageNames(cmd) + images, _ := completion.ShellCompleteImageNames(cmd) return append(containers, images...), cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index f875052a09f..b326d382456 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -32,6 +32,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/config" ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" @@ -162,15 +163,15 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet, AddPersistentStringFlag(rootCmd, "address", []string{"a", "H"}, nil, []string{"host"}, aliasToBeInherited, cfg.Address, "CONTAINERD_ADDRESS", `containerd address, optionally with "unix://" prefix`) // -n is aliases (conflicts with nerdctl logs -n) AddPersistentStringFlag(rootCmd, "namespace", []string{"n"}, nil, nil, aliasToBeInherited, cfg.Namespace, "CONTAINERD_NAMESPACE", `containerd namespace, such as "moby" for Docker, "k8s.io" for Kubernetes`) - rootCmd.RegisterFlagCompletionFunc("namespace", shellCompleteNamespaceNames) + rootCmd.RegisterFlagCompletionFunc("namespace", completion.ShellCompleteNamespaceNames) AddPersistentStringFlag(rootCmd, "snapshotter", nil, nil, []string{"storage-driver"}, aliasToBeInherited, cfg.Snapshotter, "CONTAINERD_SNAPSHOTTER", "containerd snapshotter") - rootCmd.RegisterFlagCompletionFunc("snapshotter", shellCompleteSnapshotterNames) - rootCmd.RegisterFlagCompletionFunc("storage-driver", shellCompleteSnapshotterNames) + rootCmd.RegisterFlagCompletionFunc("snapshotter", completion.ShellCompleteSnapshotterNames) + rootCmd.RegisterFlagCompletionFunc("storage-driver", completion.ShellCompleteSnapshotterNames) AddPersistentStringFlag(rootCmd, "cni-path", nil, nil, nil, aliasToBeInherited, cfg.CNIPath, "CNI_PATH", "cni plugins binary directory") AddPersistentStringFlag(rootCmd, "cni-netconfpath", nil, nil, nil, aliasToBeInherited, cfg.CNINetConfPath, "NETCONFPATH", "cni config directory") rootCmd.PersistentFlags().String("data-root", cfg.DataRoot, "Root directory of persistent nerdctl state (managed by nerdctl, not by containerd)") rootCmd.PersistentFlags().String("cgroup-manager", cfg.CgroupManager, `Cgroup manager to use ("cgroupfs"|"systemd")`) - rootCmd.RegisterFlagCompletionFunc("cgroup-manager", shellCompleteCgroupManagerNames) + rootCmd.RegisterFlagCompletionFunc("cgroup-manager", completion.ShellCompleteCgroupManagerNames) rootCmd.PersistentFlags().Bool("insecure-registry", cfg.InsecureRegistry, "skips verifying HTTPS certs, and allows falling back to plain HTTP") // hosts-dir is defined as StringSlice, not StringArray, to allow specifying "--hosts-dir=/etc/containerd/certs.d,/etc/docker/certs.d" rootCmd.PersistentFlags().StringSlice("hosts-dir", cfg.HostsDir, "A directory that contains /hosts.toml (containerd style) or /{ca.cert, cert.pem, key.pem} (docker style)") diff --git a/cmd/nerdctl/main_freebsd.go b/cmd/nerdctl/main_freebsd.go index 30ed7a9c1ea..46880c81e87 100644 --- a/cmd/nerdctl/main_freebsd.go +++ b/cmd/nerdctl/main_freebsd.go @@ -24,10 +24,6 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { return false } -func shellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveNoFileComp -} - func addApparmorCommand(rootCmd *cobra.Command) { // NOP } diff --git a/cmd/nerdctl/main_linux.go b/cmd/nerdctl/main_linux.go index 1512c05a678..2e0c0a853e2 100644 --- a/cmd/nerdctl/main_linux.go +++ b/cmd/nerdctl/main_linux.go @@ -19,7 +19,6 @@ package main import ( "github.com/spf13/cobra" - ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/strutil" ) @@ -63,17 +62,6 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { return true } -func shellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - candidates := []string{"cgroupfs"} - if ncdefaults.IsSystemdAvailable() { - candidates = append(candidates, "systemd") - } - if rootlessutil.IsRootless() { - candidates = append(candidates, "none") - } - return candidates, cobra.ShellCompDirectiveNoFileComp -} - func addApparmorCommand(rootCmd *cobra.Command) { rootCmd.AddCommand(newApparmorCommand()) } diff --git a/cmd/nerdctl/main_unix.go b/cmd/nerdctl/main_unix.go index e66bdfb8950..e29c3c6075c 100644 --- a/cmd/nerdctl/main_unix.go +++ b/cmd/nerdctl/main_unix.go @@ -17,63 +17,3 @@ */ package main - -import ( - "github.com/spf13/cobra" - - "github.com/containerd/log" - - "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" - "github.com/containerd/nerdctl/v2/pkg/clientutil" - "github.com/containerd/nerdctl/v2/pkg/infoutil" - "github.com/containerd/nerdctl/v2/pkg/rootlessutil" -) - -func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - globalOptions, err := helpers.ProcessRootCmdFlags(cmd) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } - if rootlessutil.IsRootlessParent() { - _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) - return nil, cobra.ShellCompDirectiveNoFileComp - } - - client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } - defer cancel() - nsService := client.NamespaceService() - nsList, err := nsService.List(ctx) - if err != nil { - log.L.Warn(err) - return nil, cobra.ShellCompDirectiveError - } - var candidates []string - candidates = append(candidates, nsList...) - return candidates, cobra.ShellCompDirectiveNoFileComp -} - -func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - globalOptions, err := helpers.ProcessRootCmdFlags(cmd) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } - if rootlessutil.IsRootlessParent() { - _ = rootlessutil.ParentMain(globalOptions.HostGatewayIP) - return nil, cobra.ShellCompDirectiveNoFileComp - } - client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } - defer cancel() - snapshotterPlugins, err := infoutil.GetSnapshotterNames(ctx, client.IntrospectionService()) - if err != nil { - return nil, cobra.ShellCompDirectiveError - } - var candidates []string - candidates = append(candidates, snapshotterPlugins...) - return candidates, cobra.ShellCompDirectiveNoFileComp -} diff --git a/cmd/nerdctl/main_windows.go b/cmd/nerdctl/main_windows.go index e8c48782a20..46880c81e87 100644 --- a/cmd/nerdctl/main_windows.go +++ b/cmd/nerdctl/main_windows.go @@ -24,18 +24,6 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { return false } -func shellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveNoFileComp -} - -func shellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveNoFileComp -} - -func shellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return nil, cobra.ShellCompDirectiveNoFileComp -} - func addApparmorCommand(rootCmd *cobra.Command) { // NOP } diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network_create.go index a14f30777a4..e9c12522eb6 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network_create.go @@ -21,6 +21,7 @@ import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/network" @@ -39,10 +40,10 @@ func newNetworkCreateCommand() *cobra.Command { SilenceErrors: true, } networkCreateCommand.Flags().StringP("driver", "d", DefaultNetworkDriver, "Driver to manage the Network") - networkCreateCommand.RegisterFlagCompletionFunc("driver", shellCompleteNetworkDrivers) + networkCreateCommand.RegisterFlagCompletionFunc("driver", completion.ShellCompleteNetworkDrivers) networkCreateCommand.Flags().StringArrayP("opt", "o", nil, "Set driver specific options") networkCreateCommand.Flags().String("ipam-driver", "default", "IP Address Management Driver") - networkCreateCommand.RegisterFlagCompletionFunc("ipam-driver", shellCompleteIPAMDrivers) + networkCreateCommand.RegisterFlagCompletionFunc("ipam-driver", completion.ShellCompleteIPAMDrivers) networkCreateCommand.Flags().StringArray("ipam-opt", nil, "Set IPAM driver specific options") networkCreateCommand.Flags().StringArray("subnet", nil, `Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"`) networkCreateCommand.Flags().String("gateway", "", `Gateway for the master subnet`) diff --git a/cmd/nerdctl/network_create_unix.go b/cmd/nerdctl/network_create_unix.go index 5abc60accc4..a04b6fb790a 100644 --- a/cmd/nerdctl/network_create_unix.go +++ b/cmd/nerdctl/network_create_unix.go @@ -18,17 +18,4 @@ package main -import "github.com/spf13/cobra" - -const ( - DefaultNetworkDriver = "bridge" -) - -func shellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - candidates := []string{"bridge", "macvlan", "ipvlan"} - return candidates, cobra.ShellCompDirectiveNoFileComp -} - -func shellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return []string{"default", "host-local", "dhcp"}, cobra.ShellCompDirectiveNoFileComp -} +const DefaultNetworkDriver = "bridge" diff --git a/cmd/nerdctl/network_create_windows.go b/cmd/nerdctl/network_create_windows.go index 2d6acf7e92b..9c11f3c3e81 100644 --- a/cmd/nerdctl/network_create_windows.go +++ b/cmd/nerdctl/network_create_windows.go @@ -16,17 +16,4 @@ package main -import "github.com/spf13/cobra" - -const ( - DefaultNetworkDriver = "nat" -) - -func shellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - candidates := []string{"nat"} - return candidates, cobra.ShellCompDirectiveNoFileComp -} - -func shellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return []string{"default"}, cobra.ShellCompDirectiveNoFileComp -} +const DefaultNetworkDriver = "nat" diff --git a/cmd/nerdctl/network_inspect.go b/cmd/nerdctl/network_inspect.go index a1952e761c3..927a50d1a9a 100644 --- a/cmd/nerdctl/network_inspect.go +++ b/cmd/nerdctl/network_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/network" @@ -70,5 +71,5 @@ func networkInspectAction(cmd *cobra.Command, args []string) error { func networkInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show network names, including "bridge" exclude := []string{"host", "none"} - return shellCompleteNetworkNames(cmd, exclude) + return completion.ShellCompleteNetworkNames(cmd, exclude) } diff --git a/cmd/nerdctl/network_remove.go b/cmd/nerdctl/network_remove.go index dac95ed6af4..e68b12b37cb 100644 --- a/cmd/nerdctl/network_remove.go +++ b/cmd/nerdctl/network_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -65,5 +66,5 @@ func networkRmAction(cmd *cobra.Command, args []string) error { func networkRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show network names, including "bridge" exclude := []string{netutil.DefaultNetworkName, "host", "none"} - return shellCompleteNetworkNames(cmd, exclude) + return completion.ShellCompleteNetworkNames(cmd, exclude) } diff --git a/cmd/nerdctl/volume_inspect.go b/cmd/nerdctl/volume_inspect.go index f38b5269744..88cf067496f 100644 --- a/cmd/nerdctl/volume_inspect.go +++ b/cmd/nerdctl/volume_inspect.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" @@ -73,5 +74,5 @@ func volumeInspectAction(cmd *cobra.Command, args []string) error { func volumeInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show volume names - return shellCompleteVolumeNames(cmd) + return completion.ShellCompleteVolumeNames(cmd) } diff --git a/cmd/nerdctl/volume_list.go b/cmd/nerdctl/volume_list.go index 70842abd16a..6c15ee4da4d 100644 --- a/cmd/nerdctl/volume_list.go +++ b/cmd/nerdctl/volume_list.go @@ -22,7 +22,6 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/volume" - "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" ) func newVolumeLsCommand() *cobra.Command { @@ -84,12 +83,3 @@ func volumeLsAction(cmd *cobra.Command, args []string) error { } return volume.List(options) } - -func getVolumes(cmd *cobra.Command, globalOptions types.GlobalCommandOptions) (map[string]native.Volume, error) { - volumeSize, err := cmd.Flags().GetBool("size") - if err != nil { - // The `nerdctl volume rm` does not have the flag `size`, so set it to false as the default value. - volumeSize = false - } - return volume.Volumes(globalOptions.Namespace, globalOptions.DataRoot, globalOptions.Address, volumeSize, nil) -} diff --git a/cmd/nerdctl/volume_remove.go b/cmd/nerdctl/volume_remove.go index 67c142ec50f..8b1b5907f39 100644 --- a/cmd/nerdctl/volume_remove.go +++ b/cmd/nerdctl/volume_remove.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" @@ -74,5 +75,5 @@ func volumeRmAction(cmd *cobra.Command, args []string) error { func volumeRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show volume names - return shellCompleteVolumeNames(cmd) + return completion.ShellCompleteVolumeNames(cmd) } From ec530147e2390a99c0be7e85593b060e467a4dfe Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 12:37:29 -0700 Subject: [PATCH 04/16] Move constants Categroy/Management to helpers Signed-off-by: apostasie --- cmd/nerdctl/apparmor_linux.go | 4 +++- cmd/nerdctl/builder.go | 2 +- cmd/nerdctl/container.go | 4 +++- cmd/nerdctl/helpers/consts.go | 22 ++++++++++++++++++++++ cmd/nerdctl/image.go | 4 +++- cmd/nerdctl/ipfs.go | 4 +++- cmd/nerdctl/ipfs_registry.go | 4 +++- cmd/nerdctl/main.go | 11 +++-------- cmd/nerdctl/namespace.go | 2 +- cmd/nerdctl/network.go | 4 +++- cmd/nerdctl/network_create.go | 2 +- cmd/nerdctl/system.go | 8 ++++++-- cmd/nerdctl/volume.go | 4 +++- 13 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 cmd/nerdctl/helpers/consts.go diff --git a/cmd/nerdctl/apparmor_linux.go b/cmd/nerdctl/apparmor_linux.go index e39985e9294..1d833289e1d 100644 --- a/cmd/nerdctl/apparmor_linux.go +++ b/cmd/nerdctl/apparmor_linux.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newApparmorCommand() *cobra.Command { cmd := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "apparmor", Short: "Manage AppArmor profiles", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder.go index 9a6bb5c5c98..397b9f92812 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder.go @@ -32,7 +32,7 @@ import ( func newBuilderCommand() *cobra.Command { var builderCommand = &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "builder", Short: "Manage builds", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/container.go b/cmd/nerdctl/container.go index 05cef331ab6..892fbdf0fda 100644 --- a/cmd/nerdctl/container.go +++ b/cmd/nerdctl/container.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newContainerCommand() *cobra.Command { containerCommand := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "container", Short: "Manage containers", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/helpers/consts.go b/cmd/nerdctl/helpers/consts.go new file mode 100644 index 00000000000..ff1cc06ad99 --- /dev/null +++ b/cmd/nerdctl/helpers/consts.go @@ -0,0 +1,22 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package helpers + +const ( + Category = "category" + Management = "management" +) diff --git a/cmd/nerdctl/image.go b/cmd/nerdctl/image.go index afae1936335..c14a81c9bf0 100644 --- a/cmd/nerdctl/image.go +++ b/cmd/nerdctl/image.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newImageCommand() *cobra.Command { cmd := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "image", Short: "Manage images", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/ipfs.go b/cmd/nerdctl/ipfs.go index 871077e1e5f..3c5c4b85368 100644 --- a/cmd/nerdctl/ipfs.go +++ b/cmd/nerdctl/ipfs.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newIPFSCommand() *cobra.Command { cmd := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "ipfs", Short: "Distributing images on IPFS", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/ipfs_registry.go b/cmd/nerdctl/ipfs_registry.go index 3211ad6ed8c..7a090e13261 100644 --- a/cmd/nerdctl/ipfs_registry.go +++ b/cmd/nerdctl/ipfs_registry.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newIPFSRegistryCommand() *cobra.Command { cmd := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "registry", Short: "Manage read-only registry backed by IPFS", PreRunE: checkExperimental("ipfs"), diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index b326d382456..4dc26af179e 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -42,11 +42,6 @@ import ( "github.com/containerd/nerdctl/v2/pkg/version" ) -const ( - Category = "category" - Management = "management" -) - var ( // To print Bold Text Bold = color.New(color.Bold).SprintfFunc() @@ -75,7 +70,7 @@ func usage(c *cobra.Command) error { if f.Hidden { continue } - if f.Annotations[Category] == Management { + if f.Annotations[helpers.Category] == helpers.Management { managementCommands = append(managementCommands, f) } else { nonManagementCommands = append(nonManagementCommands, f) @@ -103,7 +98,7 @@ func usage(c *cobra.Command) error { t += "\n" return t } - s += printCommands("Management commands", managementCommands) + s += printCommands("helpers.Management commands", managementCommands) s += printCommands("Commands", nonManagementCommands) s += Bold("Flags") + ":\n" @@ -293,7 +288,7 @@ Config file ($NERDCTL_TOML): %s newTopCommand(), newStatsCommand(), - // #region Management + // #region helpers.Management newContainerCommand(), newImageCommand(), newNetworkCommand(), diff --git a/cmd/nerdctl/namespace.go b/cmd/nerdctl/namespace.go index 3086c9d06a6..d96bc1e384c 100644 --- a/cmd/nerdctl/namespace.go +++ b/cmd/nerdctl/namespace.go @@ -35,7 +35,7 @@ import ( func newNamespaceCommand() *cobra.Command { namespaceCommand := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "namespace", Aliases: []string{"ns"}, Short: "Manage containerd namespaces", diff --git a/cmd/nerdctl/network.go b/cmd/nerdctl/network.go index cc9b38d5f51..6fc3db11feb 100644 --- a/cmd/nerdctl/network.go +++ b/cmd/nerdctl/network.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newNetworkCommand() *cobra.Command { networkCommand := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "network", Short: "Manage networks", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network_create.go index e9c12522eb6..84b39af357a 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network_create.go @@ -42,7 +42,7 @@ func newNetworkCreateCommand() *cobra.Command { networkCreateCommand.Flags().StringP("driver", "d", DefaultNetworkDriver, "Driver to manage the Network") networkCreateCommand.RegisterFlagCompletionFunc("driver", completion.ShellCompleteNetworkDrivers) networkCreateCommand.Flags().StringArrayP("opt", "o", nil, "Set driver specific options") - networkCreateCommand.Flags().String("ipam-driver", "default", "IP Address Management Driver") + networkCreateCommand.Flags().String("ipam-driver", "default", "IP Address helpers.Management Driver") networkCreateCommand.RegisterFlagCompletionFunc("ipam-driver", completion.ShellCompleteIPAMDrivers) networkCreateCommand.Flags().StringArray("ipam-opt", nil, "Set IPAM driver specific options") networkCreateCommand.Flags().StringArray("subnet", nil, `Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"`) diff --git a/cmd/nerdctl/system.go b/cmd/nerdctl/system.go index bb9635be295..facd7b36ab5 100644 --- a/cmd/nerdctl/system.go +++ b/cmd/nerdctl/system.go @@ -16,11 +16,15 @@ package main -import "github.com/spf13/cobra" +import ( + "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" +) func newSystemCommand() *cobra.Command { var systemCommand = &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "system", Short: "Manage containerd", RunE: unknownSubcommandAction, diff --git a/cmd/nerdctl/volume.go b/cmd/nerdctl/volume.go index b34d1cd471d..ffdb3b9f471 100644 --- a/cmd/nerdctl/volume.go +++ b/cmd/nerdctl/volume.go @@ -18,11 +18,13 @@ package main import ( "github.com/spf13/cobra" + + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) func newVolumeCommand() *cobra.Command { volumeCommand := &cobra.Command{ - Annotations: map[string]string{Category: Management}, + Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "volume", Short: "Manage volumes", RunE: unknownSubcommandAction, From 061d0cb1711ad2c1160cd8159e2e0477089bbe5e Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 12:40:36 -0700 Subject: [PATCH 05/16] Move UnknownSubcommandAction to helpers Signed-off-by: apostasie --- cmd/nerdctl/apparmor_linux.go | 2 +- cmd/nerdctl/builder.go | 2 +- cmd/nerdctl/compose.go | 3 ++- cmd/nerdctl/container.go | 2 +- cmd/nerdctl/helpers/cobra.go | 43 +++++++++++++++++++++++++++++++++++ cmd/nerdctl/image.go | 2 +- cmd/nerdctl/ipfs.go | 2 +- cmd/nerdctl/ipfs_registry.go | 2 +- cmd/nerdctl/main.go | 21 +---------------- cmd/nerdctl/namespace.go | 2 +- cmd/nerdctl/network.go | 2 +- cmd/nerdctl/system.go | 2 +- cmd/nerdctl/volume.go | 2 +- 13 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 cmd/nerdctl/helpers/cobra.go diff --git a/cmd/nerdctl/apparmor_linux.go b/cmd/nerdctl/apparmor_linux.go index 1d833289e1d..c94a80258ff 100644 --- a/cmd/nerdctl/apparmor_linux.go +++ b/cmd/nerdctl/apparmor_linux.go @@ -27,7 +27,7 @@ func newApparmorCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "apparmor", Short: "Manage AppArmor profiles", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder.go index 397b9f92812..6bfb99e456a 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder.go @@ -35,7 +35,7 @@ func newBuilderCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "builder", Short: "Manage builds", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/compose.go b/cmd/nerdctl/compose.go index 927f2024dd3..5c3a30368c0 100644 --- a/cmd/nerdctl/compose.go +++ b/cmd/nerdctl/compose.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/composer" ) @@ -26,7 +27,7 @@ func newComposeCommand() *cobra.Command { var composeCommand = &cobra.Command{ Use: "compose [flags] COMMAND", Short: "Compose", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, TraverseChildren: true, // required for global short hands like -f diff --git a/cmd/nerdctl/container.go b/cmd/nerdctl/container.go index 892fbdf0fda..9db284f5919 100644 --- a/cmd/nerdctl/container.go +++ b/cmd/nerdctl/container.go @@ -27,7 +27,7 @@ func newContainerCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "container", Short: "Manage containers", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/helpers/cobra.go b/cmd/nerdctl/helpers/cobra.go new file mode 100644 index 00000000000..791c33deb0e --- /dev/null +++ b/cmd/nerdctl/helpers/cobra.go @@ -0,0 +1,43 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package helpers + +import ( + "errors" + "fmt" + + "github.com/spf13/cobra" +) + +// UnknownSubcommandAction is needed to let `nerdctl system non-existent-command` fail +// https://github.com/containerd/nerdctl/issues/487 +// +// Ideally this should be implemented in Cobra itself. +func UnknownSubcommandAction(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return cmd.Help() + } + // The output mimics https://github.com/spf13/cobra/blob/v1.2.1/command.go#L647-L662 + msg := fmt.Sprintf("unknown subcommand %q for %q", args[0], cmd.Name()) + if suggestions := cmd.SuggestionsFor(args[0]); len(suggestions) > 0 { + msg += "\n\nDid you mean this?\n" + for _, s := range suggestions { + msg += fmt.Sprintf("\t%v\n", s) + } + } + return errors.New(msg) +} diff --git a/cmd/nerdctl/image.go b/cmd/nerdctl/image.go index c14a81c9bf0..0a0f190483a 100644 --- a/cmd/nerdctl/image.go +++ b/cmd/nerdctl/image.go @@ -27,7 +27,7 @@ func newImageCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "image", Short: "Manage images", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/ipfs.go b/cmd/nerdctl/ipfs.go index 3c5c4b85368..eb08d3dd022 100644 --- a/cmd/nerdctl/ipfs.go +++ b/cmd/nerdctl/ipfs.go @@ -27,7 +27,7 @@ func newIPFSCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "ipfs", Short: "Distributing images on IPFS", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/ipfs_registry.go b/cmd/nerdctl/ipfs_registry.go index 7a090e13261..68b9b6e7458 100644 --- a/cmd/nerdctl/ipfs_registry.go +++ b/cmd/nerdctl/ipfs_registry.go @@ -28,7 +28,7 @@ func newIPFSRegistryCommand() *cobra.Command { Use: "registry", Short: "Manage read-only registry backed by IPFS", PreRunE: checkExperimental("ipfs"), - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 4dc26af179e..ff4eb35af47 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -234,7 +234,7 @@ Config file ($NERDCTL_TOML): %s } return nil } - rootCmd.RunE = unknownSubcommandAction + rootCmd.RunE = helpers.UnknownSubcommandAction rootCmd.AddCommand( newCreateCommand(), // #region Run & Exec @@ -346,25 +346,6 @@ func globalFlags(cmd *cobra.Command) (string, []string) { return args0, args } -// unknownSubcommandAction is needed to let `nerdctl system non-existent-command` fail -// https://github.com/containerd/nerdctl/issues/487 -// -// Ideally this should be implemented in Cobra itself. -func unknownSubcommandAction(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return cmd.Help() - } - // The output mimics https://github.com/spf13/cobra/blob/v1.2.1/command.go#L647-L662 - msg := fmt.Sprintf("unknown subcommand %q for %q", args[0], cmd.Name()) - if suggestions := cmd.SuggestionsFor(args[0]); len(suggestions) > 0 { - msg += "\n\nDid you mean this?\n" - for _, s := range suggestions { - msg += fmt.Sprintf("\t%v\n", s) - } - } - return errors.New(msg) -} - // AddStringFlag is similar to cmd.Flags().String but supports aliases and env var func AddStringFlag(cmd *cobra.Command, name string, aliases []string, value string, env, usage string) { if env != "" { diff --git a/cmd/nerdctl/namespace.go b/cmd/nerdctl/namespace.go index d96bc1e384c..59b2858aa43 100644 --- a/cmd/nerdctl/namespace.go +++ b/cmd/nerdctl/namespace.go @@ -40,7 +40,7 @@ func newNamespaceCommand() *cobra.Command { Aliases: []string{"ns"}, Short: "Manage containerd namespaces", Long: "Unrelated to Linux namespaces and Kubernetes namespaces", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/network.go b/cmd/nerdctl/network.go index 6fc3db11feb..b957b61a8eb 100644 --- a/cmd/nerdctl/network.go +++ b/cmd/nerdctl/network.go @@ -27,7 +27,7 @@ func newNetworkCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "network", Short: "Manage networks", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/system.go b/cmd/nerdctl/system.go index facd7b36ab5..1119fded332 100644 --- a/cmd/nerdctl/system.go +++ b/cmd/nerdctl/system.go @@ -27,7 +27,7 @@ func newSystemCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "system", Short: "Manage containerd", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } diff --git a/cmd/nerdctl/volume.go b/cmd/nerdctl/volume.go index ffdb3b9f471..3e6c2ce120e 100644 --- a/cmd/nerdctl/volume.go +++ b/cmd/nerdctl/volume.go @@ -27,7 +27,7 @@ func newVolumeCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "volume", Short: "Manage volumes", - RunE: unknownSubcommandAction, + RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, } From a07d90665d727bdd9080751ec3e72d705ad31ee4 Mon Sep 17 00:00:00 2001 From: apostasie Date: Thu, 29 Aug 2024 12:45:11 -0700 Subject: [PATCH 06/16] Move volume actions under subpackage Signed-off-by: apostasie --- cmd/nerdctl/main.go | 3 ++- cmd/nerdctl/{ => volume}/volume.go | 4 +-- cmd/nerdctl/{ => volume}/volume_create.go | 2 +- .../{ => volume}/volume_create_test.go | 2 +- cmd/nerdctl/{ => volume}/volume_inspect.go | 2 +- .../{ => volume}/volume_inspect_test.go | 2 +- cmd/nerdctl/{ => volume}/volume_list.go | 2 +- cmd/nerdctl/{ => volume}/volume_list_test.go | 2 +- .../{ => volume}/volume_namespace_test.go | 2 +- cmd/nerdctl/{ => volume}/volume_prune.go | 2 +- .../{ => volume}/volume_prune_linux_test.go | 2 +- cmd/nerdctl/{ => volume}/volume_remove.go | 2 +- .../{ => volume}/volume_remove_linux_test.go | 2 +- cmd/nerdctl/volume/volume_test.go | 27 +++++++++++++++++++ 14 files changed, 42 insertions(+), 14 deletions(-) rename cmd/nerdctl/{ => volume}/volume.go (95%) rename cmd/nerdctl/{ => volume}/volume_create.go (99%) rename cmd/nerdctl/{ => volume}/volume_create_test.go (99%) rename cmd/nerdctl/{ => volume}/volume_inspect.go (99%) rename cmd/nerdctl/{ => volume}/volume_inspect_test.go (99%) rename cmd/nerdctl/{ => volume}/volume_list.go (99%) rename cmd/nerdctl/{ => volume}/volume_list_test.go (99%) rename cmd/nerdctl/{ => volume}/volume_namespace_test.go (99%) rename cmd/nerdctl/{ => volume}/volume_prune.go (99%) rename cmd/nerdctl/{ => volume}/volume_prune_linux_test.go (99%) rename cmd/nerdctl/{ => volume}/volume_remove.go (99%) rename cmd/nerdctl/{ => volume}/volume_remove_linux_test.go (99%) create mode 100644 cmd/nerdctl/volume/volume_test.go diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index ff4eb35af47..99e2068ae15 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -34,6 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/volume" "github.com/containerd/nerdctl/v2/pkg/config" ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" "github.com/containerd/nerdctl/v2/pkg/errutil" @@ -292,7 +293,7 @@ Config file ($NERDCTL_TOML): %s newContainerCommand(), newImageCommand(), newNetworkCommand(), - newVolumeCommand(), + volume.NewVolumeCommand(), newSystemCommand(), newNamespaceCommand(), newBuilderCommand(), diff --git a/cmd/nerdctl/volume.go b/cmd/nerdctl/volume/volume.go similarity index 95% rename from cmd/nerdctl/volume.go rename to cmd/nerdctl/volume/volume.go index 3e6c2ce120e..352993096b9 100644 --- a/cmd/nerdctl/volume.go +++ b/cmd/nerdctl/volume/volume.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newVolumeCommand() *cobra.Command { +func NewVolumeCommand() *cobra.Command { volumeCommand := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "volume", diff --git a/cmd/nerdctl/volume_create.go b/cmd/nerdctl/volume/volume_create.go similarity index 99% rename from cmd/nerdctl/volume_create.go rename to cmd/nerdctl/volume/volume_create.go index 95ef82d8b52..6927c4a556e 100644 --- a/cmd/nerdctl/volume_create.go +++ b/cmd/nerdctl/volume/volume_create.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "fmt" diff --git a/cmd/nerdctl/volume_create_test.go b/cmd/nerdctl/volume/volume_create_test.go similarity index 99% rename from cmd/nerdctl/volume_create_test.go rename to cmd/nerdctl/volume/volume_create_test.go index 9780fc3d20e..a9126f704f7 100644 --- a/cmd/nerdctl/volume_create_test.go +++ b/cmd/nerdctl/volume/volume_create_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "testing" diff --git a/cmd/nerdctl/volume_inspect.go b/cmd/nerdctl/volume/volume_inspect.go similarity index 99% rename from cmd/nerdctl/volume_inspect.go rename to cmd/nerdctl/volume/volume_inspect.go index 88cf067496f..bda84ef77f3 100644 --- a/cmd/nerdctl/volume_inspect.go +++ b/cmd/nerdctl/volume/volume_inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/volume_inspect_test.go b/cmd/nerdctl/volume/volume_inspect_test.go similarity index 99% rename from cmd/nerdctl/volume_inspect_test.go rename to cmd/nerdctl/volume/volume_inspect_test.go index 9b7dfb31e15..0d4ce4e6d05 100644 --- a/cmd/nerdctl/volume_inspect_test.go +++ b/cmd/nerdctl/volume/volume_inspect_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "crypto/rand" diff --git a/cmd/nerdctl/volume_list.go b/cmd/nerdctl/volume/volume_list.go similarity index 99% rename from cmd/nerdctl/volume_list.go rename to cmd/nerdctl/volume/volume_list.go index 6c15ee4da4d..891a626a09c 100644 --- a/cmd/nerdctl/volume_list.go +++ b/cmd/nerdctl/volume/volume_list.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/volume_list_test.go b/cmd/nerdctl/volume/volume_list_test.go similarity index 99% rename from cmd/nerdctl/volume_list_test.go rename to cmd/nerdctl/volume/volume_list_test.go index cbab079ef5c..6195db2eb13 100644 --- a/cmd/nerdctl/volume_list_test.go +++ b/cmd/nerdctl/volume/volume_list_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "errors" diff --git a/cmd/nerdctl/volume_namespace_test.go b/cmd/nerdctl/volume/volume_namespace_test.go similarity index 99% rename from cmd/nerdctl/volume_namespace_test.go rename to cmd/nerdctl/volume/volume_namespace_test.go index 8c775d49a08..dcda29a3648 100644 --- a/cmd/nerdctl/volume_namespace_test.go +++ b/cmd/nerdctl/volume/volume_namespace_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "testing" diff --git a/cmd/nerdctl/volume_prune.go b/cmd/nerdctl/volume/volume_prune.go similarity index 99% rename from cmd/nerdctl/volume_prune.go rename to cmd/nerdctl/volume/volume_prune.go index 1833d67dd99..cbc74fb882f 100644 --- a/cmd/nerdctl/volume_prune.go +++ b/cmd/nerdctl/volume/volume_prune.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "fmt" diff --git a/cmd/nerdctl/volume_prune_linux_test.go b/cmd/nerdctl/volume/volume_prune_linux_test.go similarity index 99% rename from cmd/nerdctl/volume_prune_linux_test.go rename to cmd/nerdctl/volume/volume_prune_linux_test.go index 17f6ad539c7..19632a73992 100644 --- a/cmd/nerdctl/volume_prune_linux_test.go +++ b/cmd/nerdctl/volume/volume_prune_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "strings" diff --git a/cmd/nerdctl/volume_remove.go b/cmd/nerdctl/volume/volume_remove.go similarity index 99% rename from cmd/nerdctl/volume_remove.go rename to cmd/nerdctl/volume/volume_remove.go index 8b1b5907f39..6e8bad49b8f 100644 --- a/cmd/nerdctl/volume_remove.go +++ b/cmd/nerdctl/volume/volume_remove.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/volume_remove_linux_test.go b/cmd/nerdctl/volume/volume_remove_linux_test.go similarity index 99% rename from cmd/nerdctl/volume_remove_linux_test.go rename to cmd/nerdctl/volume/volume_remove_linux_test.go index b5757946720..04c67a131aa 100644 --- a/cmd/nerdctl/volume_remove_linux_test.go +++ b/cmd/nerdctl/volume/volume_remove_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package volume import ( "fmt" diff --git a/cmd/nerdctl/volume/volume_test.go b/cmd/nerdctl/volume/volume_test.go new file mode 100644 index 00000000000..980319ec60a --- /dev/null +++ b/cmd/nerdctl/volume/volume_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package volume + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} From d2741bafca3c855fb09d333b67b38afd9746a8cc Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 08:53:37 -0700 Subject: [PATCH 07/16] Rename completion.ShellCompleteX methods to completion.X Signed-off-by: apostasie --- cmd/nerdctl/apparmor_unload_linux.go | 2 +- cmd/nerdctl/builder_build.go | 2 +- cmd/nerdctl/completion/completion.go | 12 ++++++------ cmd/nerdctl/completion/completion_freebsd.go | 2 +- cmd/nerdctl/completion/completion_linux.go | 4 ++-- cmd/nerdctl/completion/completion_unix.go | 8 ++++---- cmd/nerdctl/completion/completion_windows.go | 10 +++++----- cmd/nerdctl/container_attach.go | 2 +- cmd/nerdctl/container_commit.go | 2 +- cmd/nerdctl/container_diff.go | 2 +- cmd/nerdctl/container_exec.go | 2 +- cmd/nerdctl/container_inspect.go | 2 +- cmd/nerdctl/container_kill.go | 2 +- cmd/nerdctl/container_logs.go | 2 +- cmd/nerdctl/container_pause.go | 2 +- cmd/nerdctl/container_port.go | 2 +- cmd/nerdctl/container_remove.go | 2 +- cmd/nerdctl/container_rename.go | 2 +- cmd/nerdctl/container_run.go | 6 +++--- cmd/nerdctl/container_run_linux.go | 2 +- cmd/nerdctl/container_start.go | 2 +- cmd/nerdctl/container_stats.go | 2 +- cmd/nerdctl/container_stop.go | 2 +- cmd/nerdctl/container_top.go | 2 +- cmd/nerdctl/container_unpause.go | 2 +- cmd/nerdctl/container_update.go | 2 +- cmd/nerdctl/container_wait.go | 2 +- cmd/nerdctl/image_convert.go | 4 ++-- cmd/nerdctl/image_cryptutil.go | 4 ++-- cmd/nerdctl/image_history.go | 2 +- cmd/nerdctl/image_inspect.go | 4 ++-- cmd/nerdctl/image_list.go | 2 +- cmd/nerdctl/image_load.go | 2 +- cmd/nerdctl/image_pull.go | 2 +- cmd/nerdctl/image_push.go | 4 ++-- cmd/nerdctl/image_remove.go | 2 +- cmd/nerdctl/image_save.go | 4 ++-- cmd/nerdctl/image_tag.go | 2 +- cmd/nerdctl/inspect.go | 4 ++-- cmd/nerdctl/main.go | 8 ++++---- cmd/nerdctl/network_create.go | 4 ++-- cmd/nerdctl/network_inspect.go | 2 +- cmd/nerdctl/network_remove.go | 2 +- cmd/nerdctl/volume/volume_inspect.go | 2 +- cmd/nerdctl/volume/volume_remove.go | 2 +- 45 files changed, 70 insertions(+), 70 deletions(-) diff --git a/cmd/nerdctl/apparmor_unload_linux.go b/cmd/nerdctl/apparmor_unload_linux.go index 1fc82bc52bd..c20aab68765 100644 --- a/cmd/nerdctl/apparmor_unload_linux.go +++ b/cmd/nerdctl/apparmor_unload_linux.go @@ -48,5 +48,5 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error { } func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteApparmorProfiles(cmd) + return completion.ApparmorProfiles(cmd) } diff --git a/cmd/nerdctl/builder_build.go b/cmd/nerdctl/builder_build.go index db2a20318bd..50810a8d0fe 100644 --- a/cmd/nerdctl/builder_build.go +++ b/cmd/nerdctl/builder_build.go @@ -73,7 +73,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")") - buildCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + buildCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) buildCommand.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)") // #endregion diff --git a/cmd/nerdctl/completion/completion.go b/cmd/nerdctl/completion/completion.go index 8a5a447a835..ea7352d8561 100644 --- a/cmd/nerdctl/completion/completion.go +++ b/cmd/nerdctl/completion/completion.go @@ -33,7 +33,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/netutil" ) -func ShellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func ImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -56,7 +56,7 @@ func ShellCompleteImageNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirec return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { +func ContainerNames(cmd *cobra.Command, filterFunc func(containerd.ProcessStatus) bool) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -104,8 +104,8 @@ func ShellCompleteContainerNames(cmd *cobra.Command, filterFunc func(containerd. return candidates, cobra.ShellCompDirectiveNoFileComp } -// ShellCompleteNetworkNames includes {"bridge","host","none"} -func ShellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { +// NetworkNames includes {"bridge","host","none"} +func NetworkNames(cmd *cobra.Command, exclude []string) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -137,7 +137,7 @@ func ShellCompleteNetworkNames(cmd *cobra.Command, exclude []string) ([]string, return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func VolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -153,7 +153,7 @@ func ShellCompleteVolumeNames(cmd *cobra.Command) ([]string, cobra.ShellCompDire return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompletePlatforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func Platforms(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { candidates := []string{ "amd64", "arm64", diff --git a/cmd/nerdctl/completion/completion_freebsd.go b/cmd/nerdctl/completion/completion_freebsd.go index 5ebfd57f1b6..465671cfc24 100644 --- a/cmd/nerdctl/completion/completion_freebsd.go +++ b/cmd/nerdctl/completion/completion_freebsd.go @@ -18,6 +18,6 @@ package completion import "github.com/spf13/cobra" -func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func CgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/completion/completion_linux.go b/cmd/nerdctl/completion/completion_linux.go index f94a2a018a3..38a992c19b5 100644 --- a/cmd/nerdctl/completion/completion_linux.go +++ b/cmd/nerdctl/completion/completion_linux.go @@ -24,7 +24,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) -func ShellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { +func ApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) { profiles, err := apparmorutil.Profiles() if err != nil { return nil, cobra.ShellCompDirectiveError @@ -36,7 +36,7 @@ func ShellCompleteApparmorProfiles(cmd *cobra.Command) ([]string, cobra.ShellCom return names, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func CgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { candidates := []string{"cgroupfs"} if ncdefaults.IsSystemdAvailable() { candidates = append(candidates, "systemd") diff --git a/cmd/nerdctl/completion/completion_unix.go b/cmd/nerdctl/completion/completion_unix.go index 1379c825f1c..af0b8698ce2 100644 --- a/cmd/nerdctl/completion/completion_unix.go +++ b/cmd/nerdctl/completion/completion_unix.go @@ -29,16 +29,16 @@ import ( "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) -func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func NetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { candidates := []string{"bridge", "macvlan", "ipvlan"} return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func IPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return []string{"default", "host-local", "dhcp"}, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func NamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError @@ -64,7 +64,7 @@ func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete s return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func SnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return nil, cobra.ShellCompDirectiveError diff --git a/cmd/nerdctl/completion/completion_windows.go b/cmd/nerdctl/completion/completion_windows.go index 1739017b95b..020e0594926 100644 --- a/cmd/nerdctl/completion/completion_windows.go +++ b/cmd/nerdctl/completion/completion_windows.go @@ -18,23 +18,23 @@ package completion import "github.com/spf13/cobra" -func ShellCompleteNamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func NamespaceNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteSnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func SnapshotterNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteCgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func CgroupManagerNames(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return nil, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteNetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func NetworkDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { candidates := []string{"nat"} return candidates, cobra.ShellCompDirectiveNoFileComp } -func ShellCompleteIPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { +func IPAMDrivers(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { return []string{"default"}, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_attach.go b/cmd/nerdctl/container_attach.go index 011184fbb78..0bcbc9830c8 100644 --- a/cmd/nerdctl/container_attach.go +++ b/cmd/nerdctl/container_attach.go @@ -96,5 +96,5 @@ func attachShellComplete(cmd *cobra.Command, args []string, toComplete string) ( statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_commit.go b/cmd/nerdctl/container_commit.go index e531150a8b2..aa646bcd580 100644 --- a/cmd/nerdctl/container_commit.go +++ b/cmd/nerdctl/container_commit.go @@ -94,7 +94,7 @@ func commitAction(cmd *cobra.Command, args []string) error { func commitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_diff.go b/cmd/nerdctl/container_diff.go index 051c84e1ec7..3e2025ea27e 100644 --- a/cmd/nerdctl/container_diff.go +++ b/cmd/nerdctl/container_diff.go @@ -227,5 +227,5 @@ func appendChanges(changes []fs.Change, new fs.Change) []fs.Change { func diffShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_exec.go b/cmd/nerdctl/container_exec.go index c1bcb9180d5..eb559b5515b 100644 --- a/cmd/nerdctl/container_exec.go +++ b/cmd/nerdctl/container_exec.go @@ -158,7 +158,7 @@ func execShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_inspect.go b/cmd/nerdctl/container_inspect.go index 230986a70d8..e0d5ab5e35f 100644 --- a/cmd/nerdctl/container_inspect.go +++ b/cmd/nerdctl/container_inspect.go @@ -105,5 +105,5 @@ func containerInspectAction(cmd *cobra.Command, args []string) error { func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_kill.go b/cmd/nerdctl/container_kill.go index 85e5c75cc82..dd50a24a8cd 100644 --- a/cmd/nerdctl/container_kill.go +++ b/cmd/nerdctl/container_kill.go @@ -72,5 +72,5 @@ func killShellComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobr statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Stopped && st != containerd.Created && st != containerd.Unknown } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_logs.go b/cmd/nerdctl/container_logs.go index 6569f30da93..7d633038f48 100644 --- a/cmd/nerdctl/container_logs.go +++ b/cmd/nerdctl/container_logs.go @@ -117,7 +117,7 @@ func logsAction(cmd *cobra.Command, args []string) error { func logsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names (TODO: only show containers with logs) - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } // Attempts to parse the argument given to `-n/--tail` as a uint. diff --git a/cmd/nerdctl/container_pause.go b/cmd/nerdctl/container_pause.go index 8cbea33b2b9..cf652d51703 100644 --- a/cmd/nerdctl/container_pause.go +++ b/cmd/nerdctl/container_pause.go @@ -72,5 +72,5 @@ func pauseShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_port.go b/cmd/nerdctl/container_port.go index 984464871f4..8c35bfc75e8 100644 --- a/cmd/nerdctl/container_port.go +++ b/cmd/nerdctl/container_port.go @@ -101,5 +101,5 @@ func portAction(cmd *cobra.Command, args []string) error { } func portShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_remove.go b/cmd/nerdctl/container_remove.go index 9adf2ee7319..bf2962bd87e 100644 --- a/cmd/nerdctl/container_remove.go +++ b/cmd/nerdctl/container_remove.go @@ -73,5 +73,5 @@ func rmAction(cmd *cobra.Command, args []string) error { func rmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container_rename.go index f9938d64d3b..fcd724e8852 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container_rename.go @@ -63,5 +63,5 @@ func renameAction(cmd *cobra.Command, args []string) error { return container.Rename(ctx, client, args[0], args[1], options) } func renameShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_run.go b/cmd/nerdctl/container_run.go index 514b0262268..c069dbb1c2d 100644 --- a/cmd/nerdctl/container_run.go +++ b/cmd/nerdctl/container_run.go @@ -106,18 +106,18 @@ func setCreateFlags(cmd *cobra.Command) { // #region platform flags cmd.Flags().String("platform", "", "Set platform (e.g. \"amd64\", \"arm64\")") // not a slice, and there is no --all-platforms - cmd.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) // #endregion // #region network flags // network (net) is defined as StringSlice, not StringArray, to allow specifying "--network=cni1,cni2" cmd.Flags().StringSlice("network", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|"container:"|)`) cmd.RegisterFlagCompletionFunc("network", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteNetworkNames(cmd, []string{}) + return completion.NetworkNames(cmd, []string{}) }) cmd.Flags().StringSlice("net", []string{netutil.DefaultNetworkName}, `Connect a container to a network ("bridge"|"host"|"none"|)`) cmd.RegisterFlagCompletionFunc("net", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteNetworkNames(cmd, []string{}) + return completion.NetworkNames(cmd, []string{}) }) // dns is defined as StringSlice, not StringArray, to allow specifying "--dns=1.1.1.1,8.8.8.8" (compatible with Podman) cmd.Flags().StringSlice("dns", nil, "Set custom DNS servers") diff --git a/cmd/nerdctl/container_run_linux.go b/cmd/nerdctl/container_run_linux.go index c4059796317..92dace222b0 100644 --- a/cmd/nerdctl/container_run_linux.go +++ b/cmd/nerdctl/container_run_linux.go @@ -38,7 +38,7 @@ func capShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s func runShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/container_start.go b/cmd/nerdctl/container_start.go index 446108f3b7f..9a2443d0aa2 100644 --- a/cmd/nerdctl/container_start.go +++ b/cmd/nerdctl/container_start.go @@ -88,5 +88,5 @@ func startShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Running && st != containerd.Unknown } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_stats.go b/cmd/nerdctl/container_stats.go index a28c118a0a0..985ac9cef5b 100644 --- a/cmd/nerdctl/container_stats.go +++ b/cmd/nerdctl/container_stats.go @@ -107,5 +107,5 @@ func statsShellComplete(cmd *cobra.Command, args []string, toComplete string) ([ statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_stop.go b/cmd/nerdctl/container_stop.go index 7789eccee76..d35458d09ca 100644 --- a/cmd/nerdctl/container_stop.go +++ b/cmd/nerdctl/container_stop.go @@ -86,5 +86,5 @@ func stopShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st != containerd.Stopped && st != containerd.Created && st != containerd.Unknown } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_top.go b/cmd/nerdctl/container_top.go index 2c1910bf1d2..bc22587287c 100644 --- a/cmd/nerdctl/container_top.go +++ b/cmd/nerdctl/container_top.go @@ -78,5 +78,5 @@ func topShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]s statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_unpause.go b/cmd/nerdctl/container_unpause.go index 043791bb12a..aaa72ce1475 100644 --- a/cmd/nerdctl/container_unpause.go +++ b/cmd/nerdctl/container_unpause.go @@ -72,5 +72,5 @@ func unpauseShellComplete(cmd *cobra.Command, args []string, toComplete string) statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Paused } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/container_update.go b/cmd/nerdctl/container_update.go index 4adedd36968..3ca9f9a030c 100644 --- a/cmd/nerdctl/container_update.go +++ b/cmd/nerdctl/container_update.go @@ -399,5 +399,5 @@ func copySpec(spec *runtimespec.Spec) (*runtimespec.Spec, error) { } func updateShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return completion.ShellCompleteContainerNames(cmd, nil) + return completion.ContainerNames(cmd, nil) } diff --git a/cmd/nerdctl/container_wait.go b/cmd/nerdctl/container_wait.go index 7756ce9d393..b9af63c37ed 100644 --- a/cmd/nerdctl/container_wait.go +++ b/cmd/nerdctl/container_wait.go @@ -72,5 +72,5 @@ func waitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([] statusFilterFn := func(st containerd.ProcessStatus) bool { return st == containerd.Running } - return completion.ShellCompleteContainerNames(cmd, statusFilterFn) + return completion.ContainerNames(cmd, statusFilterFn) } diff --git a/cmd/nerdctl/image_convert.go b/cmd/nerdctl/image_convert.go index 49b7c7c40ce..577099245f2 100644 --- a/cmd/nerdctl/image_convert.go +++ b/cmd/nerdctl/image_convert.go @@ -97,7 +97,7 @@ func newImageConvertCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" imageConvertCommand.Flags().StringSlice("platform", []string{}, "Convert content for a specific platform") - imageConvertCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + imageConvertCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) imageConvertCommand.Flags().Bool("all-platforms", false, "Convert content for all platforms") // #endregion @@ -299,5 +299,5 @@ func imageConvertAction(cmd *cobra.Command, args []string) error { func imageConvertShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_cryptutil.go b/cmd/nerdctl/image_cryptutil.go index db4a2cb172e..067a0f1afe6 100644 --- a/cmd/nerdctl/image_cryptutil.go +++ b/cmd/nerdctl/image_cryptutil.go @@ -38,7 +38,7 @@ func registerImgcryptFlags(cmd *cobra.Command, encrypt bool) { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" flags.StringSlice("platform", []string{}, "Convert content for a specific platform") - cmd.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + cmd.RegisterFlagCompletionFunc("platform", completion.Platforms) flags.Bool("all-platforms", false, "Convert content for all platforms") // #endregion @@ -125,5 +125,5 @@ func getImgcryptAction(encrypt bool) func(cmd *cobra.Command, args []string) err func imgcryptShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_history.go b/cmd/nerdctl/image_history.go index 78ef813a506..d0633e2ed01 100644 --- a/cmd/nerdctl/image_history.go +++ b/cmd/nerdctl/image_history.go @@ -272,5 +272,5 @@ func (x *historyPrinter) printHistory(printable historyPrintable) error { func historyShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_inspect.go b/cmd/nerdctl/image_inspect.go index d86be752100..c52e975df76 100644 --- a/cmd/nerdctl/image_inspect.go +++ b/cmd/nerdctl/image_inspect.go @@ -48,7 +48,7 @@ func newImageInspectCommand() *cobra.Command { // #region platform flags imageInspectCommand.Flags().String("platform", "", "Inspect a specific platform") // not a slice, and there is no --all-platforms - imageInspectCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + imageInspectCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) // #endregion return imageInspectCommand @@ -100,5 +100,5 @@ func imageInspectAction(cmd *cobra.Command, args []string) error { func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_list.go b/cmd/nerdctl/image_list.go index e8851e17ef4..c65e960347b 100644 --- a/cmd/nerdctl/image_list.go +++ b/cmd/nerdctl/image_list.go @@ -148,7 +148,7 @@ func imagesAction(cmd *cobra.Command, args []string) error { func imagesShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) == 0 { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/image_load.go b/cmd/nerdctl/image_load.go index 2c361335dbc..2cf8fdf3c05 100644 --- a/cmd/nerdctl/image_load.go +++ b/cmd/nerdctl/image_load.go @@ -42,7 +42,7 @@ func newLoadCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" loadCommand.Flags().StringSlice("platform", []string{}, "Import content for a specific platform") - loadCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + loadCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) loadCommand.Flags().Bool("all-platforms", false, "Import content for all platforms") // #endregion diff --git a/cmd/nerdctl/image_pull.go b/cmd/nerdctl/image_pull.go index 9da715044a9..b7e75938649 100644 --- a/cmd/nerdctl/image_pull.go +++ b/cmd/nerdctl/image_pull.go @@ -45,7 +45,7 @@ func newPullCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" pullCommand.Flags().StringSlice("platform", nil, "Pull content for a specific platform") - pullCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + pullCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) pullCommand.Flags().Bool("all-platforms", false, "Pull content for all platforms") // #endregion diff --git a/cmd/nerdctl/image_push.go b/cmd/nerdctl/image_push.go index 374a47d4c75..37fee9d3937 100644 --- a/cmd/nerdctl/image_push.go +++ b/cmd/nerdctl/image_push.go @@ -43,7 +43,7 @@ func newPushCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" pushCommand.Flags().StringSlice("platform", []string{}, "Push content for a specific platform") - pushCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + pushCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) pushCommand.Flags().Bool("all-platforms", false, "Push content for all platforms") // #endregion @@ -146,7 +146,7 @@ func pushAction(cmd *cobra.Command, args []string) error { func pushShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } func processImageSignOptions(cmd *cobra.Command) (opt types.ImageSignOptions, err error) { diff --git a/cmd/nerdctl/image_remove.go b/cmd/nerdctl/image_remove.go index 0173b2b34e0..dde5a6d035e 100644 --- a/cmd/nerdctl/image_remove.go +++ b/cmd/nerdctl/image_remove.go @@ -82,5 +82,5 @@ func rmiAction(cmd *cobra.Command, args []string) error { func rmiShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_save.go b/cmd/nerdctl/image_save.go index 3dc0eb539a8..287e93c1b39 100644 --- a/cmd/nerdctl/image_save.go +++ b/cmd/nerdctl/image_save.go @@ -46,7 +46,7 @@ func newSaveCommand() *cobra.Command { // #region platform flags // platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64" saveCommand.Flags().StringSlice("platform", []string{}, "Export content for a specific platform") - saveCommand.RegisterFlagCompletionFunc("platform", completion.ShellCompletePlatforms) + saveCommand.RegisterFlagCompletionFunc("platform", completion.Platforms) saveCommand.Flags().Bool("all-platforms", false, "Export content for all platforms") // #endregion @@ -111,5 +111,5 @@ func saveAction(cmd *cobra.Command, args []string) error { func saveShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image_tag.go index d0bc52003c5..7daef37cc9a 100644 --- a/cmd/nerdctl/image_tag.go +++ b/cmd/nerdctl/image_tag.go @@ -63,7 +63,7 @@ func tagAction(cmd *cobra.Command, args []string) error { func tagShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { if len(args) < 2 { // show image names - return completion.ShellCompleteImageNames(cmd) + return completion.ImageNames(cmd) } return nil, cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/inspect.go b/cmd/nerdctl/inspect.go index e74860a5371..14f92c8a379 100644 --- a/cmd/nerdctl/inspect.go +++ b/cmd/nerdctl/inspect.go @@ -167,8 +167,8 @@ func inspectAction(cmd *cobra.Command, args []string) error { func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show container names - containers, _ := completion.ShellCompleteContainerNames(cmd, nil) + containers, _ := completion.ContainerNames(cmd, nil) // show image names - images, _ := completion.ShellCompleteImageNames(cmd) + images, _ := completion.ImageNames(cmd) return append(containers, images...), cobra.ShellCompDirectiveNoFileComp } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 99e2068ae15..a8d4bba0aa2 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -159,15 +159,15 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet, AddPersistentStringFlag(rootCmd, "address", []string{"a", "H"}, nil, []string{"host"}, aliasToBeInherited, cfg.Address, "CONTAINERD_ADDRESS", `containerd address, optionally with "unix://" prefix`) // -n is aliases (conflicts with nerdctl logs -n) AddPersistentStringFlag(rootCmd, "namespace", []string{"n"}, nil, nil, aliasToBeInherited, cfg.Namespace, "CONTAINERD_NAMESPACE", `containerd namespace, such as "moby" for Docker, "k8s.io" for Kubernetes`) - rootCmd.RegisterFlagCompletionFunc("namespace", completion.ShellCompleteNamespaceNames) + rootCmd.RegisterFlagCompletionFunc("namespace", completion.NamespaceNames) AddPersistentStringFlag(rootCmd, "snapshotter", nil, nil, []string{"storage-driver"}, aliasToBeInherited, cfg.Snapshotter, "CONTAINERD_SNAPSHOTTER", "containerd snapshotter") - rootCmd.RegisterFlagCompletionFunc("snapshotter", completion.ShellCompleteSnapshotterNames) - rootCmd.RegisterFlagCompletionFunc("storage-driver", completion.ShellCompleteSnapshotterNames) + rootCmd.RegisterFlagCompletionFunc("snapshotter", completion.SnapshotterNames) + rootCmd.RegisterFlagCompletionFunc("storage-driver", completion.SnapshotterNames) AddPersistentStringFlag(rootCmd, "cni-path", nil, nil, nil, aliasToBeInherited, cfg.CNIPath, "CNI_PATH", "cni plugins binary directory") AddPersistentStringFlag(rootCmd, "cni-netconfpath", nil, nil, nil, aliasToBeInherited, cfg.CNINetConfPath, "NETCONFPATH", "cni config directory") rootCmd.PersistentFlags().String("data-root", cfg.DataRoot, "Root directory of persistent nerdctl state (managed by nerdctl, not by containerd)") rootCmd.PersistentFlags().String("cgroup-manager", cfg.CgroupManager, `Cgroup manager to use ("cgroupfs"|"systemd")`) - rootCmd.RegisterFlagCompletionFunc("cgroup-manager", completion.ShellCompleteCgroupManagerNames) + rootCmd.RegisterFlagCompletionFunc("cgroup-manager", completion.CgroupManagerNames) rootCmd.PersistentFlags().Bool("insecure-registry", cfg.InsecureRegistry, "skips verifying HTTPS certs, and allows falling back to plain HTTP") // hosts-dir is defined as StringSlice, not StringArray, to allow specifying "--hosts-dir=/etc/containerd/certs.d,/etc/docker/certs.d" rootCmd.PersistentFlags().StringSlice("hosts-dir", cfg.HostsDir, "A directory that contains /hosts.toml (containerd style) or /{ca.cert, cert.pem, key.pem} (docker style)") diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network_create.go index 84b39af357a..220c0da64d9 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network_create.go @@ -40,10 +40,10 @@ func newNetworkCreateCommand() *cobra.Command { SilenceErrors: true, } networkCreateCommand.Flags().StringP("driver", "d", DefaultNetworkDriver, "Driver to manage the Network") - networkCreateCommand.RegisterFlagCompletionFunc("driver", completion.ShellCompleteNetworkDrivers) + networkCreateCommand.RegisterFlagCompletionFunc("driver", completion.NetworkDrivers) networkCreateCommand.Flags().StringArrayP("opt", "o", nil, "Set driver specific options") networkCreateCommand.Flags().String("ipam-driver", "default", "IP Address helpers.Management Driver") - networkCreateCommand.RegisterFlagCompletionFunc("ipam-driver", completion.ShellCompleteIPAMDrivers) + networkCreateCommand.RegisterFlagCompletionFunc("ipam-driver", completion.IPAMDrivers) networkCreateCommand.Flags().StringArray("ipam-opt", nil, "Set IPAM driver specific options") networkCreateCommand.Flags().StringArray("subnet", nil, `Subnet in CIDR format that represents a network segment, e.g. "10.5.0.0/16"`) networkCreateCommand.Flags().String("gateway", "", `Gateway for the master subnet`) diff --git a/cmd/nerdctl/network_inspect.go b/cmd/nerdctl/network_inspect.go index 927a50d1a9a..f025922e445 100644 --- a/cmd/nerdctl/network_inspect.go +++ b/cmd/nerdctl/network_inspect.go @@ -71,5 +71,5 @@ func networkInspectAction(cmd *cobra.Command, args []string) error { func networkInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show network names, including "bridge" exclude := []string{"host", "none"} - return completion.ShellCompleteNetworkNames(cmd, exclude) + return completion.NetworkNames(cmd, exclude) } diff --git a/cmd/nerdctl/network_remove.go b/cmd/nerdctl/network_remove.go index e68b12b37cb..e40f53a4b4f 100644 --- a/cmd/nerdctl/network_remove.go +++ b/cmd/nerdctl/network_remove.go @@ -66,5 +66,5 @@ func networkRmAction(cmd *cobra.Command, args []string) error { func networkRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show network names, including "bridge" exclude := []string{netutil.DefaultNetworkName, "host", "none"} - return completion.ShellCompleteNetworkNames(cmd, exclude) + return completion.NetworkNames(cmd, exclude) } diff --git a/cmd/nerdctl/volume/volume_inspect.go b/cmd/nerdctl/volume/volume_inspect.go index bda84ef77f3..422df57ab3e 100644 --- a/cmd/nerdctl/volume/volume_inspect.go +++ b/cmd/nerdctl/volume/volume_inspect.go @@ -74,5 +74,5 @@ func volumeInspectAction(cmd *cobra.Command, args []string) error { func volumeInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show volume names - return completion.ShellCompleteVolumeNames(cmd) + return completion.VolumeNames(cmd) } diff --git a/cmd/nerdctl/volume/volume_remove.go b/cmd/nerdctl/volume/volume_remove.go index 6e8bad49b8f..d4350b93cee 100644 --- a/cmd/nerdctl/volume/volume_remove.go +++ b/cmd/nerdctl/volume/volume_remove.go @@ -75,5 +75,5 @@ func volumeRmAction(cmd *cobra.Command, args []string) error { func volumeRmShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { // show volume names - return completion.ShellCompleteVolumeNames(cmd) + return completion.VolumeNames(cmd) } From f7e033d9cdb04b5fad33678685b4c97b26399aa0 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:13:36 -0700 Subject: [PATCH 08/16] Move IsExactArgs and FindIPv6 to helpers Signed-off-by: apostasie --- cmd/nerdctl/container_commit.go | 2 +- cmd/nerdctl/container_cp_linux.go | 2 +- cmd/nerdctl/container_logs.go | 2 +- cmd/nerdctl/container_rename.go | 2 +- .../container_run_network_linux_test.go | 3 +- cmd/nerdctl/helpers/cobra.go | 18 +++++++++ cmd/nerdctl/helpers/testing.go | 37 +++++++++++++++++++ cmd/nerdctl/image_history.go | 2 +- cmd/nerdctl/image_pull.go | 2 +- cmd/nerdctl/image_push.go | 2 +- cmd/nerdctl/image_tag.go | 2 +- cmd/nerdctl/main.go | 18 --------- cmd/nerdctl/network_create.go | 2 +- cmd/nerdctl/network_create_linux_test.go | 19 +--------- 14 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 cmd/nerdctl/helpers/testing.go diff --git a/cmd/nerdctl/container_commit.go b/cmd/nerdctl/container_commit.go index aa646bcd580..c6ef5e1243f 100644 --- a/cmd/nerdctl/container_commit.go +++ b/cmd/nerdctl/container_commit.go @@ -30,7 +30,7 @@ func newCommitCommand() *cobra.Command { var commitCommand = &cobra.Command{ Use: "commit [flags] CONTAINER REPOSITORY[:TAG]", Short: "Create a new image from a container's changes", - Args: IsExactArgs(2), + Args: helpers.IsExactArgs(2), RunE: commitAction, ValidArgsFunction: commitShellComplete, SilenceUsage: true, diff --git a/cmd/nerdctl/container_cp_linux.go b/cmd/nerdctl/container_cp_linux.go index 184cbc66292..c63d46030b6 100644 --- a/cmd/nerdctl/container_cp_linux.go +++ b/cmd/nerdctl/container_cp_linux.go @@ -44,7 +44,7 @@ Using 'nerdctl cp' with untrusted or malicious containers is unsupported and may nerdctl cp [flags] SRC_PATH|- CONTAINER:DEST_PATH` var cpCommand = &cobra.Command{ Use: usage, - Args: IsExactArgs(2), + Args: helpers.IsExactArgs(2), Short: shortHelp, Long: longHelp, RunE: cpAction, diff --git a/cmd/nerdctl/container_logs.go b/cmd/nerdctl/container_logs.go index 7d633038f48..9a14d942e6d 100644 --- a/cmd/nerdctl/container_logs.go +++ b/cmd/nerdctl/container_logs.go @@ -40,7 +40,7 @@ The following containers are supported: ` var logsCommand = &cobra.Command{ Use: "logs [flags] CONTAINER", - Args: IsExactArgs(1), + Args: helpers.IsExactArgs(1), Short: shortUsage, Long: longUsage, RunE: logsAction, diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container_rename.go index fcd724e8852..d70ea215960 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container_rename.go @@ -29,7 +29,7 @@ import ( func newRenameCommand() *cobra.Command { var renameCommand = &cobra.Command{ Use: "rename [flags] CONTAINER NEW_NAME", - Args: IsExactArgs(2), + Args: helpers.IsExactArgs(2), Short: "rename a container", RunE: renameAction, ValidArgsFunction: renameShellComplete, diff --git a/cmd/nerdctl/container_run_network_linux_test.go b/cmd/nerdctl/container_run_network_linux_test.go index 2321df8927b..a9420fcfaca 100644 --- a/cmd/nerdctl/container_run_network_linux_test.go +++ b/cmd/nerdctl/container_run_network_linux_test.go @@ -31,6 +31,7 @@ import ( "github.com/containerd/errdefs" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" @@ -632,7 +633,7 @@ func TestRunContainerWithStaticIP6(t *testing.T) { return } cmd.AssertOutWithFunc(func(stdout string) error { - ip := findIPv6(stdout) + ip := helpers.FindIPv6(stdout) if !subnet.Contains(ip) { return fmt.Errorf("expected subnet %s include ip %s", subnet, ip) } diff --git a/cmd/nerdctl/helpers/cobra.go b/cmd/nerdctl/helpers/cobra.go index 791c33deb0e..f280517b686 100644 --- a/cmd/nerdctl/helpers/cobra.go +++ b/cmd/nerdctl/helpers/cobra.go @@ -41,3 +41,21 @@ func UnknownSubcommandAction(cmd *cobra.Command, args []string) error { } return errors.New(msg) } + +// IsExactArgs returns an error if there is not the exact number of args +func IsExactArgs(number int) cobra.PositionalArgs { + return func(cmd *cobra.Command, args []string) error { + if len(args) == number { + return nil + } + return fmt.Errorf( + "%q requires exactly %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s", + cmd.CommandPath(), + number, + "argument(s)", + cmd.CommandPath(), + cmd.UseLine(), + cmd.Short, + ) + } +} diff --git a/cmd/nerdctl/helpers/testing.go b/cmd/nerdctl/helpers/testing.go new file mode 100644 index 00000000000..b0e6fdf442e --- /dev/null +++ b/cmd/nerdctl/helpers/testing.go @@ -0,0 +1,37 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package helpers + +import ( + "net" + "strings" +) + +func FindIPv6(output string) net.IP { + var ipv6 string + lines := strings.Split(output, "\n") + for _, line := range lines { + if strings.Contains(line, "inet6") { + fields := strings.Fields(line) + if len(fields) > 1 { + ipv6 = strings.Split(fields[1], "/")[0] + break + } + } + } + return net.ParseIP(ipv6) +} diff --git a/cmd/nerdctl/image_history.go b/cmd/nerdctl/image_history.go index d0633e2ed01..d3d7a6a3c44 100644 --- a/cmd/nerdctl/image_history.go +++ b/cmd/nerdctl/image_history.go @@ -47,7 +47,7 @@ func newHistoryCommand() *cobra.Command { var historyCommand = &cobra.Command{ Use: "history [flags] IMAGE", Short: "Show the history of an image", - Args: IsExactArgs(1), + Args: helpers.IsExactArgs(1), RunE: historyAction, ValidArgsFunction: historyShellComplete, SilenceUsage: true, diff --git a/cmd/nerdctl/image_pull.go b/cmd/nerdctl/image_pull.go index b7e75938649..fdfb2420c6c 100644 --- a/cmd/nerdctl/image_pull.go +++ b/cmd/nerdctl/image_pull.go @@ -32,7 +32,7 @@ func newPullCommand() *cobra.Command { var pullCommand = &cobra.Command{ Use: "pull [flags] NAME[:TAG]", Short: "Pull an image from a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS.", - Args: IsExactArgs(1), + Args: helpers.IsExactArgs(1), RunE: pullAction, SilenceUsage: true, SilenceErrors: true, diff --git a/cmd/nerdctl/image_push.go b/cmd/nerdctl/image_push.go index 37fee9d3937..147e8dfa063 100644 --- a/cmd/nerdctl/image_push.go +++ b/cmd/nerdctl/image_push.go @@ -34,7 +34,7 @@ func newPushCommand() *cobra.Command { var pushCommand = &cobra.Command{ Use: "push [flags] NAME[:TAG]", Short: "Push an image or a repository to a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to push image to IPFS.", - Args: IsExactArgs(1), + Args: helpers.IsExactArgs(1), RunE: pushAction, ValidArgsFunction: pushShellComplete, SilenceUsage: true, diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image_tag.go index 7daef37cc9a..9989665d12d 100644 --- a/cmd/nerdctl/image_tag.go +++ b/cmd/nerdctl/image_tag.go @@ -30,7 +30,7 @@ func newTagCommand() *cobra.Command { var tagCommand = &cobra.Command{ Use: "tag [flags] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", - Args: IsExactArgs(2), + Args: helpers.IsExactArgs(2), RunE: tagAction, ValidArgsFunction: tagShellComplete, SilenceUsage: true, diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index a8d4bba0aa2..85b67d8b842 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -554,21 +554,3 @@ func checkExperimental(feature string) func(cmd *cobra.Command, args []string) e return nil } } - -// IsExactArgs returns an error if there is not the exact number of args -func IsExactArgs(number int) cobra.PositionalArgs { - return func(cmd *cobra.Command, args []string) error { - if len(args) == number { - return nil - } - return fmt.Errorf( - "%q requires exactly %d %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s", - cmd.CommandPath(), - number, - "argument(s)", - cmd.CommandPath(), - cmd.UseLine(), - cmd.Short, - ) - } -} diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network_create.go index 220c0da64d9..c38e555d757 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network_create.go @@ -34,7 +34,7 @@ func newNetworkCreateCommand() *cobra.Command { Use: "create [flags] NETWORK", Short: "Create a network", Long: `NOTE: To isolate CNI bridge, CNI plugin "firewall" (>= v1.1.0) is needed.`, - Args: IsExactArgs(1), + Args: helpers.IsExactArgs(1), RunE: networkCreateAction, SilenceUsage: true, SilenceErrors: true, diff --git a/cmd/nerdctl/network_create_linux_test.go b/cmd/nerdctl/network_create_linux_test.go index 0ff4cc9b4c8..1fb3b3f4fcd 100644 --- a/cmd/nerdctl/network_create_linux_test.go +++ b/cmd/nerdctl/network_create_linux_test.go @@ -19,11 +19,11 @@ package main import ( "fmt" "net" - "strings" "testing" "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -73,25 +73,10 @@ func TestNetworkCreateIPv6(t *testing.T) { }) base.Cmd("run", "--rm", "--net", testNetwork, testutil.CommonImage, "ip", "addr", "show", "dev", "eth0").AssertOutWithFunc(func(stdout string) error { - ip := findIPv6(stdout) + ip := helpers.FindIPv6(stdout) if subnet.Contains(ip) { return nil } return fmt.Errorf("expected subnet %s include ip %s", subnet, ip) }) } - -func findIPv6(output string) net.IP { - var ipv6 string - lines := strings.Split(output, "\n") - for _, line := range lines { - if strings.Contains(line, "inet6") { - fields := strings.Fields(line) - if len(fields) > 1 { - ipv6 = strings.Split(fields[1], "/")[0] - break - } - } - } - return net.ParseIP(ipv6) -} From d1bc54d92bc3df6f61d4840e4ad3e52d60577a0c Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:17:23 -0700 Subject: [PATCH 09/16] Move network to cmd/nerdctl/network Signed-off-by: apostasie --- cmd/nerdctl/main.go | 3 ++- cmd/nerdctl/{ => network}/network.go | 4 +-- cmd/nerdctl/{ => network}/network_create.go | 2 +- .../network_create_linux_test.go | 2 +- .../{ => network}/network_create_unix.go | 2 +- .../{ => network}/network_create_windows.go | 2 +- cmd/nerdctl/{ => network}/network_inspect.go | 2 +- .../{ => network}/network_inspect_test.go | 2 +- cmd/nerdctl/{ => network}/network_list.go | 2 +- .../{ => network}/network_list_linux_test.go | 2 +- cmd/nerdctl/{ => network}/network_prune.go | 6 ++--- .../{ => network}/network_prune_linux_test.go | 2 +- cmd/nerdctl/{ => network}/network_remove.go | 2 +- .../network_remove_linux_test.go | 2 +- cmd/nerdctl/network/network_test.go | 27 +++++++++++++++++++ cmd/nerdctl/system_prune.go | 3 ++- 16 files changed, 47 insertions(+), 18 deletions(-) rename cmd/nerdctl/{ => network}/network.go (95%) rename cmd/nerdctl/{ => network}/network_create.go (99%) rename cmd/nerdctl/{ => network}/network_create_linux_test.go (99%) rename cmd/nerdctl/{ => network}/network_create_unix.go (97%) rename cmd/nerdctl/{ => network}/network_create_windows.go (97%) rename cmd/nerdctl/{ => network}/network_inspect.go (99%) rename cmd/nerdctl/{ => network}/network_inspect_test.go (99%) rename cmd/nerdctl/{ => network}/network_list.go (99%) rename cmd/nerdctl/{ => network}/network_list_linux_test.go (99%) rename cmd/nerdctl/{ => network}/network_prune.go (94%) rename cmd/nerdctl/{ => network}/network_prune_linux_test.go (98%) rename cmd/nerdctl/{ => network}/network_remove.go (99%) rename cmd/nerdctl/{ => network}/network_remove_linux_test.go (99%) create mode 100644 cmd/nerdctl/network/network_test.go diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 85b67d8b842..0da60f83fdb 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -34,6 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" "github.com/containerd/nerdctl/v2/cmd/nerdctl/volume" "github.com/containerd/nerdctl/v2/pkg/config" ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" @@ -292,7 +293,7 @@ Config file ($NERDCTL_TOML): %s // #region helpers.Management newContainerCommand(), newImageCommand(), - newNetworkCommand(), + network.NewNetworkCommand(), volume.NewVolumeCommand(), newSystemCommand(), newNamespaceCommand(), diff --git a/cmd/nerdctl/network.go b/cmd/nerdctl/network/network.go similarity index 95% rename from cmd/nerdctl/network.go rename to cmd/nerdctl/network/network.go index b957b61a8eb..b953135cd0e 100644 --- a/cmd/nerdctl/network.go +++ b/cmd/nerdctl/network/network.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newNetworkCommand() *cobra.Command { +func NewNetworkCommand() *cobra.Command { networkCommand := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "network", diff --git a/cmd/nerdctl/network_create.go b/cmd/nerdctl/network/network_create.go similarity index 99% rename from cmd/nerdctl/network_create.go rename to cmd/nerdctl/network/network_create.go index c38e555d757..1b996d27f4d 100644 --- a/cmd/nerdctl/network_create.go +++ b/cmd/nerdctl/network/network_create.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "fmt" diff --git a/cmd/nerdctl/network_create_linux_test.go b/cmd/nerdctl/network/network_create_linux_test.go similarity index 99% rename from cmd/nerdctl/network_create_linux_test.go rename to cmd/nerdctl/network/network_create_linux_test.go index 1fb3b3f4fcd..adf61d67c11 100644 --- a/cmd/nerdctl/network_create_linux_test.go +++ b/cmd/nerdctl/network/network_create_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "fmt" diff --git a/cmd/nerdctl/network_create_unix.go b/cmd/nerdctl/network/network_create_unix.go similarity index 97% rename from cmd/nerdctl/network_create_unix.go rename to cmd/nerdctl/network/network_create_unix.go index a04b6fb790a..dbfb8a9781c 100644 --- a/cmd/nerdctl/network_create_unix.go +++ b/cmd/nerdctl/network/network_create_unix.go @@ -16,6 +16,6 @@ limitations under the License. */ -package main +package network const DefaultNetworkDriver = "bridge" diff --git a/cmd/nerdctl/network_create_windows.go b/cmd/nerdctl/network/network_create_windows.go similarity index 97% rename from cmd/nerdctl/network_create_windows.go rename to cmd/nerdctl/network/network_create_windows.go index 9c11f3c3e81..1be4a08f4e4 100644 --- a/cmd/nerdctl/network_create_windows.go +++ b/cmd/nerdctl/network/network_create_windows.go @@ -14,6 +14,6 @@ limitations under the License. */ -package main +package network const DefaultNetworkDriver = "nat" diff --git a/cmd/nerdctl/network_inspect.go b/cmd/nerdctl/network/network_inspect.go similarity index 99% rename from cmd/nerdctl/network_inspect.go rename to cmd/nerdctl/network/network_inspect.go index f025922e445..6a0c0a6bc53 100644 --- a/cmd/nerdctl/network_inspect.go +++ b/cmd/nerdctl/network/network_inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/network_inspect_test.go b/cmd/nerdctl/network/network_inspect_test.go similarity index 99% rename from cmd/nerdctl/network_inspect_test.go rename to cmd/nerdctl/network/network_inspect_test.go index aa47a33ce4f..1f91f09963e 100644 --- a/cmd/nerdctl/network_inspect_test.go +++ b/cmd/nerdctl/network/network_inspect_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "runtime" diff --git a/cmd/nerdctl/network_list.go b/cmd/nerdctl/network/network_list.go similarity index 99% rename from cmd/nerdctl/network_list.go rename to cmd/nerdctl/network/network_list.go index 8cc8ecde0c8..22a10a239cd 100644 --- a/cmd/nerdctl/network_list.go +++ b/cmd/nerdctl/network/network_list.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/network_list_linux_test.go b/cmd/nerdctl/network/network_list_linux_test.go similarity index 99% rename from cmd/nerdctl/network_list_linux_test.go rename to cmd/nerdctl/network/network_list_linux_test.go index adf5cd71efa..62927f04b3f 100644 --- a/cmd/nerdctl/network_list_linux_test.go +++ b/cmd/nerdctl/network/network_list_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "errors" diff --git a/cmd/nerdctl/network_prune.go b/cmd/nerdctl/network/network_prune.go similarity index 94% rename from cmd/nerdctl/network_prune.go rename to cmd/nerdctl/network/network_prune.go index 3c68263c0ef..f9114fe6bbe 100644 --- a/cmd/nerdctl/network_prune.go +++ b/cmd/nerdctl/network/network_prune.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "fmt" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/network" ) -var networkDriversToKeep = []string{"host", "none", DefaultNetworkDriver} +var NetworkDriversToKeep = []string{"host", "none", DefaultNetworkDriver} func newNetworkPruneCommand() *cobra.Command { networkPruneCommand := &cobra.Command{ @@ -67,7 +67,7 @@ func networkPruneAction(cmd *cobra.Command, _ []string) error { } options := types.NetworkPruneOptions{ GOptions: globalOptions, - NetworkDriversToKeep: networkDriversToKeep, + NetworkDriversToKeep: NetworkDriversToKeep, Stdout: cmd.OutOrStdout(), } diff --git a/cmd/nerdctl/network_prune_linux_test.go b/cmd/nerdctl/network/network_prune_linux_test.go similarity index 98% rename from cmd/nerdctl/network_prune_linux_test.go rename to cmd/nerdctl/network/network_prune_linux_test.go index ae1b92d41a0..cb137c4c68f 100644 --- a/cmd/nerdctl/network_prune_linux_test.go +++ b/cmd/nerdctl/network/network_prune_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "testing" diff --git a/cmd/nerdctl/network_remove.go b/cmd/nerdctl/network/network_remove.go similarity index 99% rename from cmd/nerdctl/network_remove.go rename to cmd/nerdctl/network/network_remove.go index e40f53a4b4f..907908979bc 100644 --- a/cmd/nerdctl/network_remove.go +++ b/cmd/nerdctl/network/network_remove.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/network_remove_linux_test.go b/cmd/nerdctl/network/network_remove_linux_test.go similarity index 99% rename from cmd/nerdctl/network_remove_linux_test.go rename to cmd/nerdctl/network/network_remove_linux_test.go index eed01050c36..7b8acb7b4b8 100644 --- a/cmd/nerdctl/network_remove_linux_test.go +++ b/cmd/nerdctl/network/network_remove_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package network import ( "testing" diff --git a/cmd/nerdctl/network/network_test.go b/cmd/nerdctl/network/network_test.go new file mode 100644 index 00000000000..245f26e3b09 --- /dev/null +++ b/cmd/nerdctl/network/network_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package network + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/system_prune.go b/cmd/nerdctl/system_prune.go index e8e384cf5fb..2f446572618 100644 --- a/cmd/nerdctl/system_prune.go +++ b/cmd/nerdctl/system_prune.go @@ -25,6 +25,7 @@ import ( "github.com/containerd/log" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/system" @@ -74,7 +75,7 @@ func processSystemPruneOptions(cmd *cobra.Command) (types.SystemPruneOptions, er All: all, Volumes: vFlag, BuildKitHost: buildkitHost, - NetworkDriversToKeep: networkDriversToKeep, + NetworkDriversToKeep: network.NetworkDriversToKeep, }, nil } From 9c75f29dcb6deab6326874760e840fdee314080e Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:22:20 -0700 Subject: [PATCH 10/16] Move namespace code to subpackage Signed-off-by: apostasie --- cmd/nerdctl/main.go | 3 ++- cmd/nerdctl/{ => namespace}/namespace.go | 4 +-- .../{ => namespace}/namespace_create.go | 2 +- .../{ => namespace}/namespace_inspect.go | 2 +- .../{ => namespace}/namespace_remove.go | 2 +- cmd/nerdctl/namespace/namespace_test.go | 27 +++++++++++++++++++ .../{ => namespace}/namespace_update.go | 2 +- 7 files changed, 35 insertions(+), 7 deletions(-) rename cmd/nerdctl/{ => namespace}/namespace.go (98%) rename cmd/nerdctl/{ => namespace}/namespace_create.go (99%) rename cmd/nerdctl/{ => namespace}/namespace_inspect.go (99%) rename cmd/nerdctl/{ => namespace}/namespace_remove.go (99%) create mode 100644 cmd/nerdctl/namespace/namespace_test.go rename cmd/nerdctl/{ => namespace}/namespace_update.go (99%) diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 0da60f83fdb..aa91738cc2a 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -34,6 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace" "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" "github.com/containerd/nerdctl/v2/cmd/nerdctl/volume" "github.com/containerd/nerdctl/v2/pkg/config" @@ -296,7 +297,7 @@ Config file ($NERDCTL_TOML): %s network.NewNetworkCommand(), volume.NewVolumeCommand(), newSystemCommand(), - newNamespaceCommand(), + namespace.NewNamespaceCommand(), newBuilderCommand(), // #endregion diff --git a/cmd/nerdctl/namespace.go b/cmd/nerdctl/namespace/namespace.go similarity index 98% rename from cmd/nerdctl/namespace.go rename to cmd/nerdctl/namespace/namespace.go index 59b2858aa43..f83cc956862 100644 --- a/cmd/nerdctl/namespace.go +++ b/cmd/nerdctl/namespace/namespace.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package namespace import ( "fmt" @@ -33,7 +33,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/mountutil/volumestore" ) -func newNamespaceCommand() *cobra.Command { +func NewNamespaceCommand() *cobra.Command { namespaceCommand := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "namespace", diff --git a/cmd/nerdctl/namespace_create.go b/cmd/nerdctl/namespace/namespace_create.go similarity index 99% rename from cmd/nerdctl/namespace_create.go rename to cmd/nerdctl/namespace/namespace_create.go index 61700c56d19..4c6e51312fb 100644 --- a/cmd/nerdctl/namespace_create.go +++ b/cmd/nerdctl/namespace/namespace_create.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package namespace import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/namespace_inspect.go b/cmd/nerdctl/namespace/namespace_inspect.go similarity index 99% rename from cmd/nerdctl/namespace_inspect.go rename to cmd/nerdctl/namespace/namespace_inspect.go index 580ed4689e9..cd3a5ff98e0 100644 --- a/cmd/nerdctl/namespace_inspect.go +++ b/cmd/nerdctl/namespace/namespace_inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package namespace import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/namespace_remove.go b/cmd/nerdctl/namespace/namespace_remove.go similarity index 99% rename from cmd/nerdctl/namespace_remove.go rename to cmd/nerdctl/namespace/namespace_remove.go index 6ba48fe7200..b665736b1d8 100644 --- a/cmd/nerdctl/namespace_remove.go +++ b/cmd/nerdctl/namespace/namespace_remove.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package namespace import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/namespace/namespace_test.go b/cmd/nerdctl/namespace/namespace_test.go new file mode 100644 index 00000000000..bad2c633371 --- /dev/null +++ b/cmd/nerdctl/namespace/namespace_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package namespace + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/namespace_update.go b/cmd/nerdctl/namespace/namespace_update.go similarity index 99% rename from cmd/nerdctl/namespace_update.go rename to cmd/nerdctl/namespace/namespace_update.go index 6debd47cae4..b390dc26792 100644 --- a/cmd/nerdctl/namespace_update.go +++ b/cmd/nerdctl/namespace/namespace_update.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package namespace import ( "github.com/spf13/cobra" From 3b8a098d5b921250b5e5b5861a4c7e96a8613879 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:37:45 -0700 Subject: [PATCH 11/16] Move login/logout to subpackage Signed-off-by: apostasie --- cmd/nerdctl/{ => login}/login.go | 4 +-- cmd/nerdctl/{ => login}/login_linux_test.go | 2 +- cmd/nerdctl/login/login_test.go | 27 +++++++++++++++++++++ cmd/nerdctl/{ => login}/logout.go | 4 +-- cmd/nerdctl/main.go | 5 ++-- 5 files changed, 35 insertions(+), 7 deletions(-) rename cmd/nerdctl/{ => login}/login.go (98%) rename cmd/nerdctl/{ => login}/login_linux_test.go (99%) create mode 100644 cmd/nerdctl/login/login_test.go rename cmd/nerdctl/{ => login}/logout.go (98%) diff --git a/cmd/nerdctl/login.go b/cmd/nerdctl/login/login.go similarity index 98% rename from cmd/nerdctl/login.go rename to cmd/nerdctl/login/login.go index 047b712d529..64dc5f4b265 100644 --- a/cmd/nerdctl/login.go +++ b/cmd/nerdctl/login/login.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package login import ( "errors" @@ -30,7 +30,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/login" ) -func newLoginCommand() *cobra.Command { +func NewLoginCommand() *cobra.Command { var loginCommand = &cobra.Command{ Use: "login [flags] [SERVER]", Args: cobra.MaximumNArgs(1), diff --git a/cmd/nerdctl/login_linux_test.go b/cmd/nerdctl/login/login_linux_test.go similarity index 99% rename from cmd/nerdctl/login_linux_test.go rename to cmd/nerdctl/login/login_linux_test.go index 395792c32fb..ef65e9868fd 100644 --- a/cmd/nerdctl/login_linux_test.go +++ b/cmd/nerdctl/login/login_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package login import ( "crypto/rand" diff --git a/cmd/nerdctl/login/login_test.go b/cmd/nerdctl/login/login_test.go new file mode 100644 index 00000000000..ca5ad784c9b --- /dev/null +++ b/cmd/nerdctl/login/login_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package login + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/logout.go b/cmd/nerdctl/login/logout.go similarity index 98% rename from cmd/nerdctl/logout.go rename to cmd/nerdctl/login/logout.go index 4c7d10c07b8..17019c6a485 100644 --- a/cmd/nerdctl/logout.go +++ b/cmd/nerdctl/login/logout.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package login import ( "fmt" @@ -25,7 +25,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil/dockerconfigresolver" ) -func newLogoutCommand() *cobra.Command { +func NewLogoutCommand() *cobra.Command { var logoutCommand = &cobra.Command{ Use: "logout [flags] [SERVER]", Args: cobra.MaximumNArgs(1), diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index aa91738cc2a..d6aa9377560 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -34,6 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/login" "github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace" "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" "github.com/containerd/nerdctl/v2/cmd/nerdctl/volume" @@ -305,10 +306,10 @@ Config file ($NERDCTL_TOML): %s newInternalCommand(), // login - newLoginCommand(), + login.NewLoginCommand(), // Logout - newLogoutCommand(), + login.NewLogoutCommand(), // Compose newComposeCommand(), From c8df501eb2040ba335b1771037c266a03a23b648 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:52:59 -0700 Subject: [PATCH 12/16] Move a bunch of helpers AddIntFlag AddDurationFlag AddStringFlag checkExperimental createBuildContext testComposeUp requiresStargz newJWEKeyPair rmiAll Signed-off-by: apostasie --- cmd/nerdctl/builder.go | 4 +- cmd/nerdctl/builder_build.go | 2 +- cmd/nerdctl/builder_build_linux_test.go | 5 +- cmd/nerdctl/builder_build_test.go | 42 +++---- cmd/nerdctl/builder_linux_test.go | 7 +- cmd/nerdctl/compose_up_linux_test.go | 56 +--------- cmd/nerdctl/container_run_mount_linux_test.go | 11 +- .../container_run_stargz_linux_test.go | 13 +-- cmd/nerdctl/container_run_test.go | 5 +- .../container_run_verify_linux_test.go | 3 +- cmd/nerdctl/helpers/cobra.go | 79 +++++++++++++ cmd/nerdctl/helpers/flagutil.go | 15 +++ cmd/nerdctl/helpers/testing.go | 104 ++++++++++++++++++ cmd/nerdctl/helpers/testing_linux.go | 79 +++++++++++++ cmd/nerdctl/image_encrypt_linux_test.go | 97 ++-------------- cmd/nerdctl/image_list_test.go | 5 +- cmd/nerdctl/image_prune_test.go | 9 +- cmd/nerdctl/image_pull_linux_test.go | 7 +- cmd/nerdctl/ipfs_build_linux_test.go | 3 +- cmd/nerdctl/ipfs_compose_linux_test.go | 5 +- cmd/nerdctl/ipfs_linux_test.go | 21 ++-- cmd/nerdctl/ipfs_registry.go | 2 +- cmd/nerdctl/ipfs_registry_linux_test.go | 3 +- cmd/nerdctl/ipfs_registry_serve.go | 9 +- cmd/nerdctl/main.go | 88 --------------- cmd/nerdctl/multi_platform_linux_test.go | 5 +- 26 files changed, 367 insertions(+), 312 deletions(-) create mode 100644 cmd/nerdctl/helpers/testing_linux.go diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder.go index 6bfb99e456a..af3eadce09d 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder.go @@ -58,7 +58,7 @@ func newBuilderPruneCommand() *cobra.Command { SilenceErrors: true, } - AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") + helpers.AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") buildPruneCommand.Flags().BoolP("all", "a", false, "Remove all unused build cache, not just dangling ones") buildPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation") @@ -136,7 +136,7 @@ func newBuilderDebugCommand() *cobra.Command { var buildDebugCommand = &cobra.Command{ Use: "debug", Short: shortHelp, - PreRunE: checkExperimental("`nerdctl builder debug`"), + PreRunE: helpers.CheckExperimental("`nerdctl builder debug`"), RunE: builderDebugAction, SilenceUsage: true, SilenceErrors: true, diff --git a/cmd/nerdctl/builder_build.go b/cmd/nerdctl/builder_build.go index 50810a8d0fe..6881e2ec169 100644 --- a/cmd/nerdctl/builder_build.go +++ b/cmd/nerdctl/builder_build.go @@ -44,7 +44,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container SilenceUsage: true, SilenceErrors: true, } - AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") + helpers.AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address") buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format") buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile") buildCommand.Flags().String("target", "", "Set the target build stage to build") diff --git a/cmd/nerdctl/builder_build_linux_test.go b/cmd/nerdctl/builder_build_linux_test.go index 3e3fc7334ee..63ca1b6c014 100644 --- a/cmd/nerdctl/builder_build_linux_test.go +++ b/cmd/nerdctl/builder_build_linux_test.go @@ -22,6 +22,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -51,7 +52,7 @@ func TestBuildContextWithOCILayout(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s LABEL layer=oci-layout-parent CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) tarPath := fmt.Sprintf("%s/%s.tar", buildCtx, ociLayout) @@ -66,7 +67,7 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonIm dockerfile = fmt.Sprintf(`FROM %s CMD ["echo", "test-nerdctl-build-context-oci-layout"]`, ociLayout) - buildCtx = createBuildContext(t, dockerfile) + buildCtx = helpers.CreateBuildContext(t, dockerfile) var buildArgs = []string{} if testutil.IsDocker() { diff --git a/cmd/nerdctl/builder_build_test.go b/cmd/nerdctl/builder_build_test.go index 774bf02d5b2..82dd9cfa438 100644 --- a/cmd/nerdctl/builder_build_test.go +++ b/cmd/nerdctl/builder_build_test.go @@ -27,6 +27,7 @@ import ( "github.com/containerd/platforms" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -41,7 +42,7 @@ func TestBuild(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("build", buildCtx, "-t", imageName).AssertOK() @@ -66,7 +67,7 @@ func TestBuildIsShareableForCompatiblePlatform(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx, "-t", imageName).AssertErrNotContains("tarball") @@ -99,7 +100,7 @@ RUN echo hello > /hello CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("build", buildCtx, "-t", imageName).AssertOK() @@ -109,7 +110,7 @@ RUN echo hello2 > /hello2 CMD ["cat", "/hello2"] `, imageName) - buildCtx2 := createBuildContext(t, dockerfile2) + buildCtx2 := helpers.CreateBuildContext(t, dockerfile2) base.Cmd("build", "-t", imageName2, buildCtx2).AssertOK() base.Cmd("build", buildCtx2, "-t", imageName2).AssertOK() @@ -142,7 +143,7 @@ RUN echo hello2 > /hello2 CMD ["cat", "/hello2"] `, imageName) - buildCtx2 := createBuildContext(t, dockerfile2) + buildCtx2 := helpers.CreateBuildContext(t, dockerfile2) base.Cmd("build", "-t", imageName2, buildCtx2).AssertOK() base.Cmd("build", buildCtx2, "-t", imageName2).AssertOK() @@ -208,7 +209,7 @@ func TestBuildLocal(t *testing.T) { COPY %s /`, testFileName) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) if err := os.WriteFile(filepath.Join(buildCtx, testFileName), []byte(testContent), 0644); err != nil { t.Fatal(err) @@ -234,13 +235,6 @@ COPY %s /`, assert.Equal(t, string(data), testContent) } -func createBuildContext(t *testing.T, dockerfile string) string { - tmpDir := t.TempDir() - err := os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644) - assert.NilError(t, err) - return tmpDir -} - func TestBuildWithBuildArg(t *testing.T) { testutil.RequiresBuild(t) testutil.RegisterBuildCacheCleanup(t) @@ -254,7 +248,7 @@ ENV TEST_STRING=$TEST_STRING CMD echo $TEST_STRING `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx, "-t", imageName).AssertOK() base.Cmd("run", "--rm", imageName).AssertOutExactly("1\n") @@ -297,7 +291,7 @@ func TestBuildWithIIDFile(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) fileName := filepath.Join(t.TempDir(), "id.txt") base.Cmd("build", "-t", imageName, buildCtx, "--iidfile", fileName).AssertOK() @@ -320,7 +314,7 @@ func TestBuildWithLabels(t *testing.T) { LABEL name=nerdctl-build-test-label `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx, "--label", "label=test").AssertOK() defer base.Cmd("rmi", imageName).Run() @@ -343,7 +337,7 @@ func TestBuildMultipleTags(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "%s"] `, testutil.CommonImage, output) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", img, buildCtx).AssertOK() base.Cmd("build", buildCtx, "-t", img, "-t", imgWithNoTag, "-t", imgWithCustomTag).AssertOK() @@ -396,7 +390,7 @@ CMD ["echo", "dockerfile"] err = os.WriteFile(filepath.Join(tmpDir, "Containerfile"), []byte(containerfile), 0644) assert.NilError(t, err) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("run", "--rm", imageName).AssertOutExactly("dockerfile\n") @@ -411,7 +405,7 @@ func TestBuildNoTag(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-build-notag-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx).AssertOK() base.Cmd("images").AssertOutContains("") @@ -426,7 +420,7 @@ func TestBuildContextDockerImageAlias(t *testing.T) { dockerfile := `FROM myorg/myapp CMD ["echo", "nerdctl-build-myorg/myapp"]` - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx, fmt.Sprintf("--build-context=myorg/myapp=docker-image://%s", testutil.CommonImage)).AssertOK() base.Cmd("images").AssertOutContains("") @@ -451,7 +445,7 @@ func TestBuildContextWithCopyFromDir(t *testing.T) { COPY --from=dir2 /%s /hello_from_dir2.txt RUN ["cat", "/hello_from_dir2.txt"]`, testutil.CommonImage, filename) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx, fmt.Sprintf("--build-context=dir2=%s", dir2)).AssertOK() base.Cmd("images").AssertOutContains("") @@ -472,7 +466,7 @@ RUN echo $SOURCE_DATE_EPOCH >/source-date-epoch CMD ["cat", "/source-date-epoch"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) const sourceDateEpochEnvStr = "1111111111" base.Env = append(base.Env, "SOURCE_DATE_EPOCH="+sourceDateEpochEnvStr) @@ -493,7 +487,7 @@ func TestBuildNetwork(t *testing.T) { RUN apk add --no-cache curl RUN curl -I http://google.com `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) validCases := []struct { name string @@ -549,7 +543,7 @@ func TestBuildAttestation(t *testing.T) { } dockerfile := "FROM " + testutil.NginxAlpineImage - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) // Test sbom outputSBOMDir := t.TempDir() diff --git a/cmd/nerdctl/builder_linux_test.go b/cmd/nerdctl/builder_linux_test.go index cc70bd640de..4aa0e212e08 100644 --- a/cmd/nerdctl/builder_linux_test.go +++ b/cmd/nerdctl/builder_linux_test.go @@ -26,6 +26,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -39,7 +40,7 @@ func TestBuilderPrune(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-test-builder-prune"]`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) testCases := []struct { name string @@ -71,7 +72,7 @@ func TestBuilderDebug(t *testing.T) { CMD ["echo", "nerdctl-builder-debug-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("builder", "debug", buildCtx).CmdOption(testutil.WithStdin(bytes.NewReader([]byte("c\n")))).AssertOK() } @@ -132,7 +133,7 @@ namespace = "%s"`, testutil.Namespace) err := os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644) assert.NilError(t, err) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) buildCmd := []string{"build", buildCtx} switch tc.pull { diff --git a/cmd/nerdctl/compose_up_linux_test.go b/cmd/nerdctl/compose_up_linux_test.go index cbcdbb7e43b..d17df207462 100644 --- a/cmd/nerdctl/compose_up_linux_test.go +++ b/cmd/nerdctl/compose_up_linux_test.go @@ -29,6 +29,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/composer/serviceparser" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" @@ -37,7 +38,7 @@ import ( func TestComposeUp(t *testing.T) { base := testutil.NewBase(t) - testComposeUp(t, base, fmt.Sprintf(` + helpers.ComposeUp(t, base, fmt.Sprintf(` version: '3.1' services: @@ -72,57 +73,6 @@ volumes: `, testutil.WordpressImage, testutil.MariaDBImage)) } -func testComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts ...string) { - comp := testutil.NewComposeDir(t, dockerComposeYAML) - defer comp.CleanUp() - - projectName := comp.ProjectName() - t.Logf("projectName=%q", projectName) - - base.ComposeCmd(append(append([]string{"-f", comp.YAMLFullPath()}, opts...), "up", "-d")...).AssertOK() - defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run() - base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertOK() - base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertOK() - - checkWordpress := func() error { - resp, err := nettestutil.HTTPGet("http://127.0.0.1:8080", 10, false) - if err != nil { - return err - } - respBody, err := io.ReadAll(resp.Body) - if err != nil { - return err - } - if !strings.Contains(string(respBody), testutil.WordpressIndexHTMLSnippet) { - t.Logf("respBody=%q", respBody) - return fmt.Errorf("respBody does not contain %q", testutil.WordpressIndexHTMLSnippet) - } - return nil - } - - var wordpressWorking bool - for i := 0; i < 30; i++ { - t.Logf("(retry %d)", i) - err := checkWordpress() - if err == nil { - wordpressWorking = true - break - } - // NOTE: "

Error establishing a database connection

" is expected for the first few iterations - t.Log(err) - time.Sleep(3 * time.Second) - } - - if !wordpressWorking { - t.Fatal("wordpress is not working") - } - t.Log("wordpress seems functional") - - base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() - base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertFail() - base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertFail() -} - func TestComposeUpBuild(t *testing.T) { testutil.RequiresBuild(t) testutil.RegisterBuildCacheCleanup(t) @@ -505,7 +455,7 @@ func TestComposeUpWithBypass4netns(t *testing.T) { testutil.RequireKernelVersion(t, ">= 5.9.0-0") testutil.RequireSystemService(t, "bypass4netnsd") base := testutil.NewBase(t) - testComposeUp(t, base, fmt.Sprintf(` + helpers.ComposeUp(t, base, fmt.Sprintf(` version: '3.1' services: diff --git a/cmd/nerdctl/container_run_mount_linux_test.go b/cmd/nerdctl/container_run_mount_linux_test.go index 04dc4440ddb..ab382c7cf5f 100644 --- a/cmd/nerdctl/container_run_mount_linux_test.go +++ b/cmd/nerdctl/container_run_mount_linux_test.go @@ -28,6 +28,7 @@ import ( "github.com/containerd/containerd/v2/core/mount" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -125,7 +126,7 @@ func TestRunAnonymousVolumeWithBuild(t *testing.T) { VOLUME /foo `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("run", "--rm", "-v", "/foo", testutil.AlpineImage, @@ -147,7 +148,7 @@ RUN mkdir -p /mnt && echo hi > /mnt/initial_file CMD ["cat", "/mnt/initial_file"] `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() @@ -175,7 +176,7 @@ VOLUME /mnt CMD ["cat", "/mnt/initial_file"] `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() //AnonymousVolume @@ -208,7 +209,7 @@ CMD ["readlink", "/mnt/passwd"] `, testutil.AlpineImage) const expected = "../../../../../../../../../../../../../../../../../../etc/passwd\n" - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() @@ -235,7 +236,7 @@ func TestRunCopyingUpInitialContentsShouldNotResetTheCopiedContents(t *testing.T RUN echo -n "rev0" > /mnt/file `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() diff --git a/cmd/nerdctl/container_run_stargz_linux_test.go b/cmd/nerdctl/container_run_stargz_linux_test.go index 469cb641b00..2b8a79ad975 100644 --- a/cmd/nerdctl/container_run_stargz_linux_test.go +++ b/cmd/nerdctl/container_run_stargz_linux_test.go @@ -20,6 +20,7 @@ import ( "runtime" "testing" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -30,17 +31,7 @@ func TestRunStargz(t *testing.T) { } base := testutil.NewBase(t) - requiresStargz(base) + helpers.RequiresStargz(base) // if stargz snapshotter is functional, "/.stargz-snapshotter" appears base.Cmd("--snapshotter=stargz", "run", "--rm", testutil.FedoraESGZImage, "ls", "/.stargz-snapshotter").AssertOK() } - -func requiresStargz(base *testutil.Base) { - info := base.Info() - for _, p := range info.Plugins.Storage { - if p == "stargz" { - return - } - } - base.T.Skip("test requires stargz") -} diff --git a/cmd/nerdctl/container_run_test.go b/cmd/nerdctl/container_run_test.go index 53103317451..d1ece634eed 100644 --- a/cmd/nerdctl/container_run_test.go +++ b/cmd/nerdctl/container_run_test.go @@ -34,6 +34,7 @@ import ( "gotest.tools/v3/icmd" "gotest.tools/v3/poll" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -50,7 +51,7 @@ ENTRYPOINT ["echo", "foo"] CMD ["echo", "bar"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("run", "--rm", imageName).AssertOutExactly("foo echo bar\n") @@ -440,7 +441,7 @@ FROM scratch COPY --from=builder /go/src/logger/logger / ` - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) tmpDir := t.TempDir() base.Cmd("build", buildCtx, "--output", fmt.Sprintf("type=local,src=/go/src/logger/logger,dest=%s", tmpDir)).AssertOK() defer base.Cmd("image", "rm", "-f", imageName).AssertOK() diff --git a/cmd/nerdctl/container_run_verify_linux_test.go b/cmd/nerdctl/container_run_verify_linux_test.go index 3522f12cfa6..06459f93297 100644 --- a/cmd/nerdctl/container_run_verify_linux_test.go +++ b/cmd/nerdctl/container_run_verify_linux_test.go @@ -20,6 +20,7 @@ import ( "fmt" "testing" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" ) @@ -47,7 +48,7 @@ func TestRunVerifyCosign(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() diff --git a/cmd/nerdctl/helpers/cobra.go b/cmd/nerdctl/helpers/cobra.go index f280517b686..b41c851bb77 100644 --- a/cmd/nerdctl/helpers/cobra.go +++ b/cmd/nerdctl/helpers/cobra.go @@ -19,8 +19,13 @@ package helpers import ( "errors" "fmt" + "os" + "strconv" + "time" "github.com/spf13/cobra" + + "github.com/containerd/log" ) // UnknownSubcommandAction is needed to let `nerdctl system non-existent-command` fail @@ -59,3 +64,77 @@ func IsExactArgs(number int) cobra.PositionalArgs { ) } } + +// AddStringFlag is similar to cmd.Flags().String but supports aliases and env var +func AddStringFlag(cmd *cobra.Command, name string, aliases []string, value string, env, usage string) { + if env != "" { + usage = fmt.Sprintf("%s [$%s]", usage, env) + } + if envV, ok := os.LookupEnv(env); ok { + value = envV + } + aliasesUsage := fmt.Sprintf("Alias of --%s", name) + p := new(string) + flags := cmd.Flags() + flags.StringVar(p, name, value, usage) + for _, a := range aliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + flags.StringVarP(p, a, a, value, aliasesUsage) + } else { + flags.StringVar(p, a, value, aliasesUsage) + } + } +} + +// AddIntFlag is similar to cmd.Flags().Int but supports aliases and env var +func AddIntFlag(cmd *cobra.Command, name string, aliases []string, value int, env, usage string) { + if env != "" { + usage = fmt.Sprintf("%s [$%s]", usage, env) + } + if envV, ok := os.LookupEnv(env); ok { + v, err := strconv.ParseInt(envV, 10, 64) + if err != nil { + log.L.WithError(err).Warnf("Invalid int value for `%s`", env) + } + value = int(v) + } + aliasesUsage := fmt.Sprintf("Alias of --%s", name) + p := new(int) + flags := cmd.Flags() + flags.IntVar(p, name, value, usage) + for _, a := range aliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + flags.IntVarP(p, a, a, value, aliasesUsage) + } else { + flags.IntVar(p, a, value, aliasesUsage) + } + } +} + +// AddDurationFlag is similar to cmd.Flags().Duration but supports aliases and env var +func AddDurationFlag(cmd *cobra.Command, name string, aliases []string, value time.Duration, env, usage string) { + if env != "" { + usage = fmt.Sprintf("%s [$%s]", usage, env) + } + if envV, ok := os.LookupEnv(env); ok { + var err error + value, err = time.ParseDuration(envV) + if err != nil { + log.L.WithError(err).Warnf("Invalid duration value for `%s`", env) + } + } + aliasesUsage := fmt.Sprintf("Alias of --%s", name) + p := new(time.Duration) + flags := cmd.Flags() + flags.DurationVar(p, name, value, usage) + for _, a := range aliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + flags.DurationVarP(p, a, a, value, aliasesUsage) + } else { + flags.DurationVar(p, a, value, aliasesUsage) + } + } +} diff --git a/cmd/nerdctl/helpers/flagutil.go b/cmd/nerdctl/helpers/flagutil.go index 9ca11d449fd..8e87839b2f9 100644 --- a/cmd/nerdctl/helpers/flagutil.go +++ b/cmd/nerdctl/helpers/flagutil.go @@ -17,6 +17,8 @@ package helpers import ( + "fmt" + "github.com/spf13/cobra" "github.com/containerd/nerdctl/v2/pkg/api/types" @@ -113,3 +115,16 @@ func ProcessRootCmdFlags(cmd *cobra.Command) (types.GlobalCommandOptions, error) HostGatewayIP: hostGatewayIP, }, nil } + +func CheckExperimental(feature string) func(cmd *cobra.Command, args []string) error { + return func(cmd *cobra.Command, args []string) error { + globalOptions, err := ProcessRootCmdFlags(cmd) + if err != nil { + return err + } + if !globalOptions.Experimental { + return fmt.Errorf("%s is experimental feature, you should enable experimental config", feature) + } + return nil + } +} diff --git a/cmd/nerdctl/helpers/testing.go b/cmd/nerdctl/helpers/testing.go index b0e6fdf442e..553f4d996d2 100644 --- a/cmd/nerdctl/helpers/testing.go +++ b/cmd/nerdctl/helpers/testing.go @@ -17,8 +17,21 @@ package helpers import ( + "context" "net" + "os" + "os/exec" + "path/filepath" "strings" + "testing" + + "gotest.tools/v3/assert" + + containerd "github.com/containerd/containerd/v2/client" + "github.com/containerd/containerd/v2/core/content" + + "github.com/containerd/nerdctl/v2/pkg/buildkitutil" + "github.com/containerd/nerdctl/v2/pkg/testutil" ) func FindIPv6(output string) net.IP { @@ -35,3 +48,94 @@ func FindIPv6(output string) net.IP { } return net.ParseIP(ipv6) } + +func RequiresStargz(base *testutil.Base) { + info := base.Info() + for _, p := range info.Plugins.Storage { + if p == "stargz" { + return + } + } + base.T.Skip("test requires stargz") +} + +type JweKeyPair struct { + Prv string + Pub string + Cleanup func() +} + +func NewJWEKeyPair(t testing.TB) *JweKeyPair { + testutil.RequireExecutable(t, "openssl") + td, err := os.MkdirTemp(t.TempDir(), "jwe-key-pair") + assert.NilError(t, err) + prv := filepath.Join(td, "mykey.pem") + pub := filepath.Join(td, "mypubkey.pem") + cmds := [][]string{ + // Exec openssl commands to ensure that nerdctl is compatible with the output of openssl commands. + // Do NOT refactor this function to use "crypto/rsa" stdlib. + {"openssl", "genrsa", "-out", prv}, + {"openssl", "rsa", "-in", prv, "-pubout", "-out", pub}, + } + for _, f := range cmds { + cmd := exec.Command(f[0], f[1:]...) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("failed to run %v: %v (%q)", cmd.Args, err, string(out)) + } + } + return &JweKeyPair{ + Prv: prv, + Pub: pub, + Cleanup: func() { + _ = os.RemoveAll(td) + }, + } +} + +func RmiAll(base *testutil.Base) { + base.T.Logf("Pruning images") + imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() + // remove empty output line at the end + imageIDs = imageIDs[:len(imageIDs)-1] + // use `Run` on purpose (same below) because `rmi all` may fail on individual + // image id that has an expected running container (e.g. a registry) + base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() + + base.T.Logf("Pruning build caches") + if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil { + base.Cmd("builder", "prune", "--force").AssertOK() + } + + // For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs + // https://github.com/containerd/nerdctl/pull/1833 + if base.Target == testutil.Nerdctl { + base.T.Logf("Pruning all content blobs") + addr := base.ContainerdAddress() + client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace)) + assert.NilError(base.T, err) + cs := client.ContentStore() + ctx := context.TODO() + wf := func(info content.Info) error { + base.T.Logf("Pruning blob %+v", info) + if err := cs.Delete(ctx, info.Digest); err != nil { + base.T.Log(err) + } + return nil + } + if err := cs.Walk(ctx, wf); err != nil { + base.T.Log(err) + } + + base.T.Logf("Pruning all images (again?)") + imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() + base.T.Logf("pruning following images: %+v", imageIDs) + base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() + } +} + +func CreateBuildContext(t *testing.T, dockerfile string) string { + tmpDir := t.TempDir() + err := os.WriteFile(filepath.Join(tmpDir, "Dockerfile"), []byte(dockerfile), 0644) + assert.NilError(t, err) + return tmpDir +} diff --git a/cmd/nerdctl/helpers/testing_linux.go b/cmd/nerdctl/helpers/testing_linux.go new file mode 100644 index 00000000000..d459e9272b5 --- /dev/null +++ b/cmd/nerdctl/helpers/testing_linux.go @@ -0,0 +1,79 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package helpers + +import ( + "fmt" + "io" + "strings" + "testing" + "time" + + "github.com/containerd/nerdctl/v2/pkg/testutil" + "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" +) + +func ComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts ...string) { + comp := testutil.NewComposeDir(t, dockerComposeYAML) + defer comp.CleanUp() + + projectName := comp.ProjectName() + t.Logf("projectName=%q", projectName) + + base.ComposeCmd(append(append([]string{"-f", comp.YAMLFullPath()}, opts...), "up", "-d")...).AssertOK() + defer base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").Run() + base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertOK() + base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertOK() + + checkWordpress := func() error { + resp, err := nettestutil.HTTPGet("http://127.0.0.1:8080", 10, false) + if err != nil { + return err + } + respBody, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + if !strings.Contains(string(respBody), testutil.WordpressIndexHTMLSnippet) { + t.Logf("respBody=%q", respBody) + return fmt.Errorf("respBody does not contain %q", testutil.WordpressIndexHTMLSnippet) + } + return nil + } + + var wordpressWorking bool + for i := 0; i < 30; i++ { + t.Logf("(retry %d)", i) + err := checkWordpress() + if err == nil { + wordpressWorking = true + break + } + // NOTE: "

Error establishing a database connection

" is expected for the first few iterations + t.Log(err) + time.Sleep(3 * time.Second) + } + + if !wordpressWorking { + t.Fatal("wordpress is not working") + } + t.Log("wordpress seems functional") + + base.ComposeCmd("-f", comp.YAMLFullPath(), "down", "-v").AssertOK() + base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertFail() + base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertFail() +} diff --git a/cmd/nerdctl/image_encrypt_linux_test.go b/cmd/nerdctl/image_encrypt_linux_test.go index e6809d29f5b..1cd71d49af9 100644 --- a/cmd/nerdctl/image_encrypt_linux_test.go +++ b/cmd/nerdctl/image_encrypt_linux_test.go @@ -17,102 +17,19 @@ package main import ( - "context" "fmt" - "os" - "os/exec" - "path/filepath" "testing" - "gotest.tools/v3/assert" - - containerd "github.com/containerd/containerd/v2/client" - "github.com/containerd/containerd/v2/core/content" - - "github.com/containerd/nerdctl/v2/pkg/buildkitutil" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" ) -type jweKeyPair struct { - prv string - pub string - cleanup func() -} - -func newJWEKeyPair(t testing.TB) *jweKeyPair { - testutil.RequireExecutable(t, "openssl") - td, err := os.MkdirTemp(t.TempDir(), "jwe-key-pair") - assert.NilError(t, err) - prv := filepath.Join(td, "mykey.pem") - pub := filepath.Join(td, "mypubkey.pem") - cmds := [][]string{ - // Exec openssl commands to ensure that nerdctl is compatible with the output of openssl commands. - // Do NOT refactor this function to use "crypto/rsa" stdlib. - {"openssl", "genrsa", "-out", prv}, - {"openssl", "rsa", "-in", prv, "-pubout", "-out", pub}, - } - for _, f := range cmds { - cmd := exec.Command(f[0], f[1:]...) - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("failed to run %v: %v (%q)", cmd.Args, err, string(out)) - } - } - return &jweKeyPair{ - prv: prv, - pub: pub, - cleanup: func() { - _ = os.RemoveAll(td) - }, - } -} - -func rmiAll(base *testutil.Base) { - base.T.Logf("Pruning images") - imageIDs := base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() - // remove empty output line at the end - imageIDs = imageIDs[:len(imageIDs)-1] - // use `Run` on purpose (same below) because `rmi all` may fail on individual - // image id that has an expected running container (e.g. a registry) - base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() - - base.T.Logf("Pruning build caches") - if _, err := buildkitutil.GetBuildkitHost(testutil.Namespace); err == nil { - base.Cmd("builder", "prune", "--force").AssertOK() - } - - // For BuildKit >= 0.11, pruning cache isn't enough to remove manifest blobs that are referred by build history blobs - // https://github.com/containerd/nerdctl/pull/1833 - if base.Target == testutil.Nerdctl { - base.T.Logf("Pruning all content blobs") - addr := base.ContainerdAddress() - client, err := containerd.New(addr, containerd.WithDefaultNamespace(testutil.Namespace)) - assert.NilError(base.T, err) - cs := client.ContentStore() - ctx := context.TODO() - wf := func(info content.Info) error { - base.T.Logf("Pruning blob %+v", info) - if err := cs.Delete(ctx, info.Digest); err != nil { - base.T.Log(err) - } - return nil - } - if err := cs.Walk(ctx, wf); err != nil { - base.T.Log(err) - } - - base.T.Logf("Pruning all images (again?)") - imageIDs = base.Cmd("images", "--no-trunc", "-a", "-q").OutLines() - base.T.Logf("pruning following images: %+v", imageIDs) - base.Cmd(append([]string{"rmi", "-f"}, imageIDs...)...).Run() - } -} - func TestImageEncryptJWE(t *testing.T) { testutil.RequiresBuild(t) testutil.DockerIncompatible(t) - keyPair := newJWEKeyPair(t) - defer keyPair.cleanup() + keyPair := helpers.NewJWEKeyPair(t) + defer keyPair.Cleanup() base := testutil.NewBase(t) tID := testutil.Identifier(t) reg := testregistry.NewWithNoAuth(base, 0, false) @@ -120,16 +37,16 @@ func TestImageEncryptJWE(t *testing.T) { base.Cmd("pull", testutil.CommonImage).AssertOK() encryptImageRef := fmt.Sprintf("127.0.0.1:%d/%s:encrypted", reg.Port, tID) defer base.Cmd("rmi", encryptImageRef).Run() - base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.pub, testutil.CommonImage, encryptImageRef).AssertOK() + base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, testutil.CommonImage, encryptImageRef).AssertOK() base.Cmd("image", "inspect", "--mode=native", "--format={{len .Index.Manifests}}", encryptImageRef).AssertOutExactly("1\n") base.Cmd("image", "inspect", "--mode=native", "--format={{json .Manifest.Layers}}", encryptImageRef).AssertOutContains("org.opencontainers.image.enc.keys.jwe") base.Cmd("push", encryptImageRef).AssertOK() // remove all local images (in the nerdctl-test namespace), to ensure that we do not have blobs of the original image. - rmiAll(base) + helpers.RmiAll(base) base.Cmd("pull", encryptImageRef).AssertFail() // defaults to --unpack=true, and fails due to missing prv key base.Cmd("pull", "--unpack=false", encryptImageRef).AssertOK() decryptImageRef := tID + ":decrypted" defer base.Cmd("rmi", decryptImageRef).Run() - base.Cmd("image", "decrypt", "--key="+keyPair.pub, encryptImageRef, decryptImageRef).AssertFail() // decryption needs prv key, not pub key - base.Cmd("image", "decrypt", "--key="+keyPair.prv, encryptImageRef, decryptImageRef).AssertOK() + base.Cmd("image", "decrypt", "--key="+keyPair.Pub, encryptImageRef, decryptImageRef).AssertFail() // decryption needs prv key, not pub key + base.Cmd("image", "decrypt", "--key="+keyPair.Prv, encryptImageRef, decryptImageRef).AssertOK() } diff --git a/cmd/nerdctl/image_list_test.go b/cmd/nerdctl/image_list_test.go index e91a9043f54..12edc26472b 100644 --- a/cmd/nerdctl/image_list_test.go +++ b/cmd/nerdctl/image_list_test.go @@ -24,6 +24,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/tabutil" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -90,7 +91,7 @@ CMD ["echo", "nerdctl-build-test-string"] \n LABEL foo=bar LABEL version=0.1`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", tempName, "-f", buildCtx+"/Dockerfile", buildCtx).AssertOK() defer base.Cmd("rmi", tempName).AssertOK() @@ -129,7 +130,7 @@ func TestImagesFilterDangling(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-build-notag-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-f", buildCtx+"/Dockerfile", buildCtx).AssertOK() diff --git a/cmd/nerdctl/image_prune_test.go b/cmd/nerdctl/image_prune_test.go index 17d7ec038a9..2c07a5a330d 100644 --- a/cmd/nerdctl/image_prune_test.go +++ b/cmd/nerdctl/image_prune_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -35,7 +36,7 @@ func TestImagePrune(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-test-image-prune"]`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", buildCtx).AssertOK() base.Cmd("build", "-t", imageName, buildCtx).AssertOK() @@ -56,7 +57,7 @@ func TestImagePruneAll(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-test-image-prune"]`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() // The following commands will clean up all images, so it should fail at this point. @@ -86,7 +87,7 @@ CMD ["echo", "nerdctl-test-image-prune-filter-label"] LABEL foo=bar LABEL version=0.1`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("images", "--all").AssertOutContains(imageName) @@ -117,7 +118,7 @@ func TestImagePruneFilterUntil(t *testing.T) { dockerfile := fmt.Sprintf(`FROM %s CMD ["echo", "nerdctl-test-image-prune-filter-until"]`, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, buildCtx).AssertOK() base.Cmd("images", "--all").AssertOutContains(imageName) diff --git a/cmd/nerdctl/image_pull_linux_test.go b/cmd/nerdctl/image_pull_linux_test.go index dbb0ff4d8e8..577e54291df 100644 --- a/cmd/nerdctl/image_pull_linux_test.go +++ b/cmd/nerdctl/image_pull_linux_test.go @@ -26,6 +26,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" ) @@ -81,7 +82,7 @@ func TestImageVerifyWithCosign(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() @@ -103,7 +104,7 @@ func TestImagePullPlainHttpWithDefaultPort(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() base.Cmd("--insecure-registry", "push", testImageRef).AssertOK() base.Cmd("--insecure-registry", "pull", testImageRef).AssertOK() @@ -131,7 +132,7 @@ func TestImageVerifyWithCosignShouldFailWhenKeyIsNotCorrect(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, testutil.CommonImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() diff --git a/cmd/nerdctl/ipfs_build_linux_test.go b/cmd/nerdctl/ipfs_build_linux_test.go index c3373715fa9..cdc47bc3b2a 100644 --- a/cmd/nerdctl/ipfs_build_linux_test.go +++ b/cmd/nerdctl/ipfs_build_linux_test.go @@ -25,6 +25,7 @@ import ( "gotest.tools/v3/assert" "gotest.tools/v3/icmd" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -43,7 +44,7 @@ func TestIPFSBuild(t *testing.T) { CMD ["echo", "nerdctl-build-test-string"] `, ipfsCIDBase) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) done := ipfsRegistryUp(t, base) defer done() diff --git a/cmd/nerdctl/ipfs_compose_linux_test.go b/cmd/nerdctl/ipfs_compose_linux_test.go index 3257fef88d3..c8d60900cc5 100644 --- a/cmd/nerdctl/ipfs_compose_linux_test.go +++ b/cmd/nerdctl/ipfs_compose_linux_test.go @@ -24,6 +24,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" @@ -67,14 +68,14 @@ func TestIPFSComposeUp(t *testing.T) { t.Run(tt.name, func(t *testing.T) { base := testutil.NewBase(t) if tt.requiresStargz { - requiresStargz(base) + helpers.RequiresStargz(base) } ipfsImgs := make([]string, 2) for i, img := range []string{testutil.WordpressImage, testutil.MariaDBImage} { ipfsImgs[i] = pushImageToIPFS(t, base, img, tt.pushOptions...) } base.Env = append(base.Env, "CONTAINERD_SNAPSHOTTER="+tt.snapshotter) - testComposeUp(t, base, fmt.Sprintf(` + helpers.ComposeUp(t, base, fmt.Sprintf(` version: '3.1' services: diff --git a/cmd/nerdctl/ipfs_linux_test.go b/cmd/nerdctl/ipfs_linux_test.go index 0cff7e9433e..b9f5471bc97 100644 --- a/cmd/nerdctl/ipfs_linux_test.go +++ b/cmd/nerdctl/ipfs_linux_test.go @@ -22,6 +22,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/infoutil" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" @@ -37,23 +38,23 @@ func TestIPFS(t *testing.T) { base.Cmd("run", "--rm", ipfsCID, "echo", "hello").AssertOK() // encryption - keyPair := newJWEKeyPair(t) - defer keyPair.cleanup() + keyPair := helpers.NewJWEKeyPair(t) + defer keyPair.Cleanup() tID := testutil.Identifier(t) encryptImageRef := tID + ":enc" layersNum := 1 - base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.pub, ipfsCID, encryptImageRef).AssertOK() + base.Cmd("image", "encrypt", "--recipient=jwe:"+keyPair.Pub, ipfsCID, encryptImageRef).AssertOK() base.Cmd("image", "inspect", "--mode=native", "--format={{len .Manifest.Layers}}", encryptImageRef).AssertOutExactly(fmt.Sprintf("%d\n", layersNum)) for i := 0; i < layersNum; i++ { base.Cmd("image", "inspect", "--mode=native", fmt.Sprintf("--format={{json (index .Manifest.Layers %d) }}", i), encryptImageRef).AssertOutContains("org.opencontainers.image.enc.keys.jwe") } ipfsCIDEnc := cidOf(t, base.Cmd("push", "ipfs://"+encryptImageRef).OutLines()) - rmiAll(base) + helpers.RmiAll(base) decryptImageRef := tID + ":dec" base.Cmd("pull", "--unpack=false", ipfsCIDEnc).AssertOK() - base.Cmd("image", "decrypt", "--key="+keyPair.pub, ipfsCIDEnc, decryptImageRef).AssertFail() // decryption needs prv key, not pub key - base.Cmd("image", "decrypt", "--key="+keyPair.prv, ipfsCIDEnc, decryptImageRef).AssertOK() + base.Cmd("image", "decrypt", "--key="+keyPair.Pub, ipfsCIDEnc, decryptImageRef).AssertFail() // decryption needs prv key, not pub key + base.Cmd("image", "decrypt", "--key="+keyPair.Prv, ipfsCIDEnc, decryptImageRef).AssertOK() base.Cmd("run", "--rm", decryptImageRef, "/bin/sh", "-c", "echo hello").AssertOK() } @@ -91,7 +92,7 @@ func TestIPFSCommit(t *testing.T) { base.Cmd("kill", newContainer).AssertOK() base.Cmd("rm", newContainer).AssertOK() ipfsCID2 := cidOf(t, base.Cmd("push", "ipfs://"+newImg).OutLines()) - rmiAll(base) + helpers.RmiAll(base) base.Cmd("pull", ipfsCID2).AssertOK() base.Cmd("run", "--rm", ipfsCID2, "/bin/sh", "-c", "cat /hello").AssertOK() } @@ -99,7 +100,7 @@ func TestIPFSCommit(t *testing.T) { func TestIPFSWithLazyPulling(t *testing.T) { testutil.DockerIncompatible(t) base := testutil.NewBase(t) - requiresStargz(base) + helpers.RequiresStargz(base) ipfsCID := pushImageToIPFS(t, base, testutil.AlpineImage, "--estargz") base.Env = append(base.Env, "CONTAINERD_SNAPSHOTTER=stargz") @@ -114,7 +115,7 @@ func TestIPFSWithLazyPullingCommit(t *testing.T) { } testutil.DockerIncompatible(t) base := testutil.NewBase(t) - requiresStargz(base) + helpers.RequiresStargz(base) ipfsCID := pushImageToIPFS(t, base, testutil.AlpineImage, "--estargz") base.Env = append(base.Env, "CONTAINERD_SNAPSHOTTER=stargz") @@ -127,7 +128,7 @@ func TestIPFSWithLazyPullingCommit(t *testing.T) { base.Cmd("kill", newContainer).AssertOK() base.Cmd("rm", newContainer).AssertOK() ipfsCID2 := cidOf(t, base.Cmd("push", "--estargz", "ipfs://"+newImg).OutLines()) - rmiAll(base) + helpers.RmiAll(base) base.Cmd("pull", ipfsCID2).AssertOK() base.Cmd("run", "--rm", ipfsCID2, "/bin/sh", "-c", "ls /.stargz-snapshotter && cat /hello").AssertOK() diff --git a/cmd/nerdctl/ipfs_registry.go b/cmd/nerdctl/ipfs_registry.go index 68b9b6e7458..c61468df40e 100644 --- a/cmd/nerdctl/ipfs_registry.go +++ b/cmd/nerdctl/ipfs_registry.go @@ -27,7 +27,7 @@ func newIPFSRegistryCommand() *cobra.Command { Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "registry", Short: "Manage read-only registry backed by IPFS", - PreRunE: checkExperimental("ipfs"), + PreRunE: helpers.CheckExperimental("ipfs"), RunE: helpers.UnknownSubcommandAction, SilenceUsage: true, SilenceErrors: true, diff --git a/cmd/nerdctl/ipfs_registry_linux_test.go b/cmd/nerdctl/ipfs_registry_linux_test.go index c47ad9f606b..1d2ce14ebd7 100644 --- a/cmd/nerdctl/ipfs_registry_linux_test.go +++ b/cmd/nerdctl/ipfs_registry_linux_test.go @@ -20,6 +20,7 @@ import ( "strings" "testing" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -42,7 +43,7 @@ func TestIPFSRegistryWithLazyPulling(t *testing.T) { testutil.DockerIncompatible(t) base := testutil.NewBase(t) - requiresStargz(base) + helpers.RequiresStargz(base) base.Env = append(base.Env, "CONTAINERD_SNAPSHOTTER=stargz") ipfsCID := pushImageToIPFS(t, base, testutil.AlpineImage, "--estargz") ipfsRegistryAddr := "localhost:5555" diff --git a/cmd/nerdctl/ipfs_registry_serve.go b/cmd/nerdctl/ipfs_registry_serve.go index c02b32937a1..1ccbcd67720 100644 --- a/cmd/nerdctl/ipfs_registry_serve.go +++ b/cmd/nerdctl/ipfs_registry_serve.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/cmd/ipfs" ) @@ -38,10 +39,10 @@ func newIPFSRegistryServeCommand() *cobra.Command { SilenceErrors: true, } - AddStringFlag(ipfsRegistryServeCommand, "listen-registry", nil, defaultIPFSRegistry, "IPFS_REGISTRY_SERVE_LISTEN_REGISTRY", "address to listen") - AddStringFlag(ipfsRegistryServeCommand, "ipfs-address", nil, "", "IPFS_REGISTRY_SERVE_IPFS_ADDRESS", "multiaddr of IPFS API (default is pulled from $IPFS_PATH/api file. If $IPFS_PATH env var is not present, it defaults to ~/.ipfs)") - AddIntFlag(ipfsRegistryServeCommand, "read-retry-num", nil, defaultIPFSReadRetryNum, "IPFS_REGISTRY_SERVE_READ_RETRY_NUM", "times to retry query on IPFS. Zero or lower means no retry.") - AddDurationFlag(ipfsRegistryServeCommand, "read-timeout", nil, defaultIPFSReadTimeoutDuration, "IPFS_REGISTRY_SERVE_READ_TIMEOUT", "timeout duration of a read request to IPFS. Zero means no timeout.") + helpers.AddStringFlag(ipfsRegistryServeCommand, "listen-registry", nil, defaultIPFSRegistry, "IPFS_REGISTRY_SERVE_LISTEN_REGISTRY", "address to listen") + helpers.AddStringFlag(ipfsRegistryServeCommand, "ipfs-address", nil, "", "IPFS_REGISTRY_SERVE_IPFS_ADDRESS", "multiaddr of IPFS API (default is pulled from $IPFS_PATH/api file. If $IPFS_PATH env var is not present, it defaults to ~/.ipfs)") + helpers.AddIntFlag(ipfsRegistryServeCommand, "read-retry-num", nil, defaultIPFSReadRetryNum, "IPFS_REGISTRY_SERVE_READ_RETRY_NUM", "times to retry query on IPFS. Zero or lower means no retry.") + helpers.AddDurationFlag(ipfsRegistryServeCommand, "read-timeout", nil, defaultIPFSReadTimeoutDuration, "IPFS_REGISTRY_SERVE_READ_TIMEOUT", "timeout duration of a read request to IPFS. Zero means no timeout.") return ipfsRegistryServeCommand } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index d6aa9377560..5a7d6b194a7 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -23,7 +23,6 @@ import ( "runtime" "strconv" "strings" - "time" "github.com/fatih/color" "github.com/pelletier/go-toml/v2" @@ -350,80 +349,6 @@ func globalFlags(cmd *cobra.Command) (string, []string) { return args0, args } -// AddStringFlag is similar to cmd.Flags().String but supports aliases and env var -func AddStringFlag(cmd *cobra.Command, name string, aliases []string, value string, env, usage string) { - if env != "" { - usage = fmt.Sprintf("%s [$%s]", usage, env) - } - if envV, ok := os.LookupEnv(env); ok { - value = envV - } - aliasesUsage := fmt.Sprintf("Alias of --%s", name) - p := new(string) - flags := cmd.Flags() - flags.StringVar(p, name, value, usage) - for _, a := range aliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - flags.StringVarP(p, a, a, value, aliasesUsage) - } else { - flags.StringVar(p, a, value, aliasesUsage) - } - } -} - -// AddIntFlag is similar to cmd.Flags().Int but supports aliases and env var -func AddIntFlag(cmd *cobra.Command, name string, aliases []string, value int, env, usage string) { - if env != "" { - usage = fmt.Sprintf("%s [$%s]", usage, env) - } - if envV, ok := os.LookupEnv(env); ok { - v, err := strconv.ParseInt(envV, 10, 64) - if err != nil { - log.L.WithError(err).Warnf("Invalid int value for `%s`", env) - } - value = int(v) - } - aliasesUsage := fmt.Sprintf("Alias of --%s", name) - p := new(int) - flags := cmd.Flags() - flags.IntVar(p, name, value, usage) - for _, a := range aliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - flags.IntVarP(p, a, a, value, aliasesUsage) - } else { - flags.IntVar(p, a, value, aliasesUsage) - } - } -} - -// AddDurationFlag is similar to cmd.Flags().Duration but supports aliases and env var -func AddDurationFlag(cmd *cobra.Command, name string, aliases []string, value time.Duration, env, usage string) { - if env != "" { - usage = fmt.Sprintf("%s [$%s]", usage, env) - } - if envV, ok := os.LookupEnv(env); ok { - var err error - value, err = time.ParseDuration(envV) - if err != nil { - log.L.WithError(err).Warnf("Invalid duration value for `%s`", env) - } - } - aliasesUsage := fmt.Sprintf("Alias of --%s", name) - p := new(time.Duration) - flags := cmd.Flags() - flags.DurationVar(p, name, value, usage) - for _, a := range aliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - flags.DurationVarP(p, a, a, value, aliasesUsage) - } else { - flags.DurationVar(p, a, value, aliasesUsage) - } - } -} - // AddPersistentStringFlag is similar to AddStringFlag but persistent. // See https://github.com/spf13/cobra/blob/main/user_guide.md#persistent-flags to learn what is "persistent". func AddPersistentStringFlag(cmd *cobra.Command, name string, aliases, localAliases, persistentAliases []string, aliasToBeInherited *pflag.FlagSet, value string, env, usage string) { @@ -544,16 +469,3 @@ func AddPersistentStringArrayFlag(cmd *cobra.Command, name string, aliases, nonP } } } - -func checkExperimental(feature string) func(cmd *cobra.Command, args []string) error { - return func(cmd *cobra.Command, args []string) error { - globalOptions, err := helpers.ProcessRootCmdFlags(cmd) - if err != nil { - return err - } - if !globalOptions.Experimental { - return fmt.Errorf("%s is experimental feature, you should enable experimental config", feature) - } - return nil - } -} diff --git a/cmd/nerdctl/multi_platform_linux_test.go b/cmd/nerdctl/multi_platform_linux_test.go index 944bcf48e28..411df72b308 100644 --- a/cmd/nerdctl/multi_platform_linux_test.go +++ b/cmd/nerdctl/multi_platform_linux_test.go @@ -24,6 +24,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" @@ -68,7 +69,7 @@ func TestMultiPlatformBuildPush(t *testing.T) { RUN echo dummy `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, "--platform=amd64,arm64,linux/arm/v7", buildCtx).AssertOK() testMultiPlatformRun(base, imageName) @@ -95,7 +96,7 @@ func TestMultiPlatformBuildPushNoRun(t *testing.T) { CMD echo dummy `, testutil.AlpineImage) - buildCtx := createBuildContext(t, dockerfile) + buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", imageName, "--platform=amd64,arm64,linux/arm/v7", buildCtx).AssertOK() testMultiPlatformRun(base, imageName) From 3ea64eb9c4972c1daefb8776afda4c0cf51fbc59 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 09:56:37 -0700 Subject: [PATCH 13/16] Move ipfs to subpackage Signed-off-by: apostasie --- cmd/nerdctl/{ => ipfs}/ipfs.go | 4 +-- .../{ => ipfs}/ipfs_build_linux_test.go | 2 +- .../{ => ipfs}/ipfs_compose_linux_test.go | 2 +- cmd/nerdctl/{ => ipfs}/ipfs_linux_test.go | 2 +- cmd/nerdctl/{ => ipfs}/ipfs_registry.go | 2 +- .../{ => ipfs}/ipfs_registry_linux_test.go | 2 +- cmd/nerdctl/{ => ipfs}/ipfs_registry_serve.go | 2 +- cmd/nerdctl/ipfs/ipfs_test.go | 27 +++++++++++++++++++ cmd/nerdctl/main.go | 3 ++- 9 files changed, 37 insertions(+), 9 deletions(-) rename cmd/nerdctl/{ => ipfs}/ipfs.go (95%) rename cmd/nerdctl/{ => ipfs}/ipfs_build_linux_test.go (99%) rename cmd/nerdctl/{ => ipfs}/ipfs_compose_linux_test.go (99%) rename cmd/nerdctl/{ => ipfs}/ipfs_linux_test.go (99%) rename cmd/nerdctl/{ => ipfs}/ipfs_registry.go (98%) rename cmd/nerdctl/{ => ipfs}/ipfs_registry_linux_test.go (99%) rename cmd/nerdctl/{ => ipfs}/ipfs_registry_serve.go (99%) create mode 100644 cmd/nerdctl/ipfs/ipfs_test.go diff --git a/cmd/nerdctl/ipfs.go b/cmd/nerdctl/ipfs/ipfs.go similarity index 95% rename from cmd/nerdctl/ipfs.go rename to cmd/nerdctl/ipfs/ipfs.go index eb08d3dd022..e6e3a2194c8 100644 --- a/cmd/nerdctl/ipfs.go +++ b/cmd/nerdctl/ipfs/ipfs.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newIPFSCommand() *cobra.Command { +func NewIPFSCommand() *cobra.Command { cmd := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "ipfs", diff --git a/cmd/nerdctl/ipfs_build_linux_test.go b/cmd/nerdctl/ipfs/ipfs_build_linux_test.go similarity index 99% rename from cmd/nerdctl/ipfs_build_linux_test.go rename to cmd/nerdctl/ipfs/ipfs_build_linux_test.go index cdc47bc3b2a..7ca00803bdb 100644 --- a/cmd/nerdctl/ipfs_build_linux_test.go +++ b/cmd/nerdctl/ipfs/ipfs_build_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "fmt" diff --git a/cmd/nerdctl/ipfs_compose_linux_test.go b/cmd/nerdctl/ipfs/ipfs_compose_linux_test.go similarity index 99% rename from cmd/nerdctl/ipfs_compose_linux_test.go rename to cmd/nerdctl/ipfs/ipfs_compose_linux_test.go index c8d60900cc5..1ac2b682482 100644 --- a/cmd/nerdctl/ipfs_compose_linux_test.go +++ b/cmd/nerdctl/ipfs/ipfs_compose_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "fmt" diff --git a/cmd/nerdctl/ipfs_linux_test.go b/cmd/nerdctl/ipfs/ipfs_linux_test.go similarity index 99% rename from cmd/nerdctl/ipfs_linux_test.go rename to cmd/nerdctl/ipfs/ipfs_linux_test.go index b9f5471bc97..a50e426ae0f 100644 --- a/cmd/nerdctl/ipfs_linux_test.go +++ b/cmd/nerdctl/ipfs/ipfs_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "fmt" diff --git a/cmd/nerdctl/ipfs_registry.go b/cmd/nerdctl/ipfs/ipfs_registry.go similarity index 98% rename from cmd/nerdctl/ipfs_registry.go rename to cmd/nerdctl/ipfs/ipfs_registry.go index c61468df40e..9beb019f8f3 100644 --- a/cmd/nerdctl/ipfs_registry.go +++ b/cmd/nerdctl/ipfs/ipfs_registry.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/ipfs_registry_linux_test.go b/cmd/nerdctl/ipfs/ipfs_registry_linux_test.go similarity index 99% rename from cmd/nerdctl/ipfs_registry_linux_test.go rename to cmd/nerdctl/ipfs/ipfs_registry_linux_test.go index 1d2ce14ebd7..d367d0d9f86 100644 --- a/cmd/nerdctl/ipfs_registry_linux_test.go +++ b/cmd/nerdctl/ipfs/ipfs_registry_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "strings" diff --git a/cmd/nerdctl/ipfs_registry_serve.go b/cmd/nerdctl/ipfs/ipfs_registry_serve.go similarity index 99% rename from cmd/nerdctl/ipfs_registry_serve.go rename to cmd/nerdctl/ipfs/ipfs_registry_serve.go index 1ccbcd67720..739d3d3ece6 100644 --- a/cmd/nerdctl/ipfs_registry_serve.go +++ b/cmd/nerdctl/ipfs/ipfs_registry_serve.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package ipfs import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/ipfs/ipfs_test.go b/cmd/nerdctl/ipfs/ipfs_test.go new file mode 100644 index 00000000000..1b6749bee5f --- /dev/null +++ b/cmd/nerdctl/ipfs/ipfs_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package ipfs + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 5a7d6b194a7..373cbc80e20 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -33,6 +33,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/ipfs" "github.com/containerd/nerdctl/v2/cmd/nerdctl/login" "github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace" "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" @@ -314,7 +315,7 @@ Config file ($NERDCTL_TOML): %s newComposeCommand(), // IPFS - newIPFSCommand(), + ipfs.NewIPFSCommand(), ) addApparmorCommand(rootCmd) addCpCommand(rootCmd) From 4b8061d979d358dc1f1eae6187b0acc88965c340 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 11:19:49 -0700 Subject: [PATCH 14/16] Move cmd/image & cmd/builder to subpackages Signed-off-by: apostasie --- cmd/nerdctl/{ => builder}/builder.go | 8 +-- cmd/nerdctl/{ => builder}/builder_build.go | 8 +-- .../{ => builder}/builder_build_linux_test.go | 4 +- .../{ => builder}/builder_build_test.go | 2 +- .../{ => builder}/builder_linux_test.go | 2 +- cmd/nerdctl/builder/builder_test.go | 27 ++++++++ cmd/nerdctl/compose_run_linux_test.go | 9 +-- cmd/nerdctl/container_run_linux_test.go | 3 +- cmd/nerdctl/container_run_soci_linux_test.go | 13 +--- .../container_run_verify_linux_test.go | 8 +-- cmd/nerdctl/helpers/testing.go | 40 ++++++++++++ cmd/nerdctl/helpers/testing_linux.go | 58 +++++++++++++++++ cmd/nerdctl/{ => image}/image.go | 23 +++---- cmd/nerdctl/{ => image}/image_convert.go | 2 +- .../{ => image}/image_convert_linux_test.go | 2 +- cmd/nerdctl/{ => image}/image_convert_test.go | 2 +- cmd/nerdctl/{ => image}/image_cryptutil.go | 2 +- cmd/nerdctl/{ => image}/image_decrypt.go | 2 +- cmd/nerdctl/{ => image}/image_encrypt.go | 2 +- .../{ => image}/image_encrypt_linux_test.go | 2 +- cmd/nerdctl/{ => image}/image_history.go | 4 +- cmd/nerdctl/{ => image}/image_history_test.go | 2 +- cmd/nerdctl/{ => image}/image_inspect.go | 6 +- cmd/nerdctl/{ => image}/image_inspect_test.go | 2 +- cmd/nerdctl/{ => image}/image_list.go | 4 +- cmd/nerdctl/{ => image}/image_list_test.go | 2 +- cmd/nerdctl/{ => image}/image_load.go | 4 +- .../{ => image}/image_load_linux_test.go | 2 +- cmd/nerdctl/{ => image}/image_prune.go | 2 +- cmd/nerdctl/{ => image}/image_prune_test.go | 2 +- cmd/nerdctl/{ => image}/image_pull.go | 4 +- .../{ => image}/image_pull_linux_test.go | 57 ++++------------- cmd/nerdctl/{ => image}/image_push.go | 4 +- .../{ => image}/image_push_linux_test.go | 5 +- cmd/nerdctl/{ => image}/image_remove.go | 4 +- .../{ => image}/image_remove_linux_test.go | 2 +- cmd/nerdctl/{ => image}/image_save.go | 4 +- .../{ => image}/image_save_linux_test.go | 62 +------------------ cmd/nerdctl/{ => image}/image_save_test.go | 2 +- cmd/nerdctl/{ => image}/image_tag.go | 4 +- cmd/nerdctl/image/image_test.go | 27 ++++++++ cmd/nerdctl/inspect.go | 3 +- cmd/nerdctl/main.go | 24 +++---- cmd/nerdctl/system_prune.go | 3 +- 44 files changed, 258 insertions(+), 196 deletions(-) rename cmd/nerdctl/{ => builder}/builder.go (97%) rename cmd/nerdctl/{ => builder}/builder_build.go (98%) rename cmd/nerdctl/{ => builder}/builder_build_linux_test.go (97%) rename cmd/nerdctl/{ => builder}/builder_build_test.go (99%) rename cmd/nerdctl/{ => builder}/builder_linux_test.go (99%) create mode 100644 cmd/nerdctl/builder/builder_test.go rename cmd/nerdctl/{ => image}/image.go (82%) rename cmd/nerdctl/{ => image}/image_convert.go (99%) rename cmd/nerdctl/{ => image}/image_convert_linux_test.go (99%) rename cmd/nerdctl/{ => image}/image_convert_test.go (99%) rename cmd/nerdctl/{ => image}/image_cryptutil.go (99%) rename cmd/nerdctl/{ => image}/image_decrypt.go (99%) rename cmd/nerdctl/{ => image}/image_encrypt.go (99%) rename cmd/nerdctl/{ => image}/image_encrypt_linux_test.go (99%) rename cmd/nerdctl/{ => image}/image_history.go (99%) rename cmd/nerdctl/{ => image}/image_history_test.go (99%) rename cmd/nerdctl/{ => image}/image_inspect.go (96%) rename cmd/nerdctl/{ => image}/image_inspect_test.go (99%) rename cmd/nerdctl/{ => image}/image_list.go (98%) rename cmd/nerdctl/{ => image}/image_list_test.go (99%) rename cmd/nerdctl/{ => image}/image_load.go (98%) rename cmd/nerdctl/{ => image}/image_load_linux_test.go (99%) rename cmd/nerdctl/{ => image}/image_prune.go (99%) rename cmd/nerdctl/{ => image}/image_prune_test.go (99%) rename cmd/nerdctl/{ => image}/image_pull.go (99%) rename cmd/nerdctl/{ => image}/image_pull_linux_test.go (82%) rename cmd/nerdctl/{ => image}/image_push.go (99%) rename cmd/nerdctl/{ => image}/image_push_linux_test.go (98%) rename cmd/nerdctl/{ => image}/image_remove.go (98%) rename cmd/nerdctl/{ => image}/image_remove_linux_test.go (99%) rename cmd/nerdctl/{ => image}/image_save.go (98%) rename cmd/nerdctl/{ => image}/image_save_linux_test.go (51%) rename cmd/nerdctl/{ => image}/image_save_test.go (99%) rename cmd/nerdctl/{ => image}/image_tag.go (97%) create mode 100644 cmd/nerdctl/image/image_test.go diff --git a/cmd/nerdctl/builder.go b/cmd/nerdctl/builder/builder.go similarity index 97% rename from cmd/nerdctl/builder.go rename to cmd/nerdctl/builder/builder.go index af3eadce09d..3e8e120f23c 100644 --- a/cmd/nerdctl/builder.go +++ b/cmd/nerdctl/builder/builder.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package builder import ( "fmt" @@ -30,7 +30,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/builder" ) -func newBuilderCommand() *cobra.Command { +func NewBuilderCommand() *cobra.Command { var builderCommand = &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "builder", @@ -40,7 +40,7 @@ func newBuilderCommand() *cobra.Command { SilenceErrors: true, } builderCommand.AddCommand( - newBuildCommand(), + NewBuildCommand(), newBuilderPruneCommand(), newBuilderDebugCommand(), ) @@ -107,7 +107,7 @@ func processBuilderPruneOptions(cmd *cobra.Command) (types.BuilderPruneOptions, return types.BuilderPruneOptions{}, err } - buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace) + buildkitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace) if err != nil { return types.BuilderPruneOptions{}, err } diff --git a/cmd/nerdctl/builder_build.go b/cmd/nerdctl/builder/builder_build.go similarity index 98% rename from cmd/nerdctl/builder_build.go rename to cmd/nerdctl/builder/builder_build.go index 6881e2ec169..096a86df668 100644 --- a/cmd/nerdctl/builder_build.go +++ b/cmd/nerdctl/builder/builder_build.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package builder import ( "errors" @@ -34,7 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/strutil" ) -func newBuildCommand() *cobra.Command { +func NewBuildCommand() *cobra.Command { var buildCommand = &cobra.Command{ Use: "build [flags] PATH", Short: "Build an image from a Dockerfile. Needs buildkitd to be running.", @@ -88,7 +88,7 @@ func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBu if err != nil { return types.BuilderBuildOptions{}, err } - buildKitHost, err := getBuildkitHost(cmd, globalOptions.Namespace) + buildKitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace) if err != nil { return types.BuilderBuildOptions{}, err } @@ -235,7 +235,7 @@ func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBu }, nil } -func getBuildkitHost(cmd *cobra.Command, namespace string) (string, error) { +func GetBuildkitHost(cmd *cobra.Command, namespace string) (string, error) { if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" { // If address is explicitly specified, use it. buildkitHost, err := cmd.Flags().GetString("buildkit-host") diff --git a/cmd/nerdctl/builder_build_linux_test.go b/cmd/nerdctl/builder/builder_build_linux_test.go similarity index 97% rename from cmd/nerdctl/builder_build_linux_test.go rename to cmd/nerdctl/builder/builder_build_linux_test.go index 63ca1b6c014..0f80066b0a2 100644 --- a/cmd/nerdctl/builder_build_linux_test.go +++ b/cmd/nerdctl/builder/builder_build_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package builder import ( "fmt" @@ -62,7 +62,7 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonIm // Unpack OCI archive into OCI layout directory. ociLayoutDir := t.TempDir() - err := extractTarFile(ociLayoutDir, tarPath) + err := helpers.ExtractTarFile(ociLayoutDir, tarPath) assert.NilError(t, err) dockerfile = fmt.Sprintf(`FROM %s diff --git a/cmd/nerdctl/builder_build_test.go b/cmd/nerdctl/builder/builder_build_test.go similarity index 99% rename from cmd/nerdctl/builder_build_test.go rename to cmd/nerdctl/builder/builder_build_test.go index 82dd9cfa438..92a0928ecae 100644 --- a/cmd/nerdctl/builder_build_test.go +++ b/cmd/nerdctl/builder/builder_build_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package builder import ( "fmt" diff --git a/cmd/nerdctl/builder_linux_test.go b/cmd/nerdctl/builder/builder_linux_test.go similarity index 99% rename from cmd/nerdctl/builder_linux_test.go rename to cmd/nerdctl/builder/builder_linux_test.go index 4aa0e212e08..862320142f9 100644 --- a/cmd/nerdctl/builder_linux_test.go +++ b/cmd/nerdctl/builder/builder_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package builder import ( "bytes" diff --git a/cmd/nerdctl/builder/builder_test.go b/cmd/nerdctl/builder/builder_test.go new file mode 100644 index 00000000000..fee2491cb9b --- /dev/null +++ b/cmd/nerdctl/builder/builder_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package builder + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/compose_run_linux_test.go b/cmd/nerdctl/compose_run_linux_test.go index cd8d353bc9a..e3e3e5ea559 100644 --- a/cmd/nerdctl/compose_run_linux_test.go +++ b/cmd/nerdctl/compose_run_linux_test.go @@ -27,6 +27,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" @@ -432,10 +433,10 @@ func TestComposePushAndPullWithCosignVerify(t *testing.T) { base := testutil.NewBase(t) base.Env = append(base.Env, "COSIGN_PASSWORD=1") - keyPair := newCosignKeyPair(t, "cosign-key-pair", "1") + keyPair := helpers.NewCosignKeyPair(t, "cosign-key-pair", "1") reg := testregistry.NewWithNoAuth(base, 0, false) t.Cleanup(func() { - keyPair.cleanup() + keyPair.Cleanup() reg.Cleanup(nil) }) @@ -475,8 +476,8 @@ services: x-nerdctl-sign: none entrypoint: - stty -`, imageSvc0, keyPair.publicKey, keyPair.privateKey, - imageSvc1, keyPair.privateKey, imageSvc2) +`, imageSvc0, keyPair.PublicKey, keyPair.PrivateKey, + imageSvc1, keyPair.PrivateKey, imageSvc2) dockerfile := fmt.Sprintf(`FROM %s`, testutil.AlpineImage) diff --git a/cmd/nerdctl/container_run_linux_test.go b/cmd/nerdctl/container_run_linux_test.go index 93e9de95d8f..04b7b851a1d 100644 --- a/cmd/nerdctl/container_run_linux_test.go +++ b/cmd/nerdctl/container_run_linux_test.go @@ -35,6 +35,7 @@ import ( "gotest.tools/v3/assert" "gotest.tools/v3/icmd" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/strutil" "github.com/containerd/nerdctl/v2/pkg/testutil" @@ -70,7 +71,7 @@ func prepareCustomRootfs(base *testutil.Base, imageName string) string { base.Cmd("save", "-o", archiveTarPath, imageName).AssertOK() rootfs, err := os.MkdirTemp(base.T.TempDir(), "rootfs") assert.NilError(base.T, err) - err = extractDockerArchive(archiveTarPath, rootfs) + err = helpers.ExtractDockerArchive(archiveTarPath, rootfs) assert.NilError(base.T, err) return rootfs } diff --git a/cmd/nerdctl/container_run_soci_linux_test.go b/cmd/nerdctl/container_run_soci_linux_test.go index 635f2710cf9..a8085164168 100644 --- a/cmd/nerdctl/container_run_soci_linux_test.go +++ b/cmd/nerdctl/container_run_soci_linux_test.go @@ -21,6 +21,7 @@ import ( "strings" "testing" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -41,7 +42,7 @@ func TestRunSoci(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { base := testutil.NewBase(t) - requiresSoci(base) + helpers.RequiresSoci(base) //counting initial snapshot mounts initialMounts, err := exec.Command("mount").Output() @@ -73,13 +74,3 @@ func TestRunSoci(t *testing.T) { }) } } - -func requiresSoci(base *testutil.Base) { - info := base.Info() - for _, p := range info.Plugins.Storage { - if p == "soci" { - return - } - } - base.T.Skip("test requires soci") -} diff --git a/cmd/nerdctl/container_run_verify_linux_test.go b/cmd/nerdctl/container_run_verify_linux_test.go index 06459f93297..bb3556b7da6 100644 --- a/cmd/nerdctl/container_run_verify_linux_test.go +++ b/cmd/nerdctl/container_run_verify_linux_test.go @@ -35,10 +35,10 @@ func TestRunVerifyCosign(t *testing.T) { base := testutil.NewBase(t) base.Env = append(base.Env, "COSIGN_PASSWORD=1") - keyPair := newCosignKeyPair(t, "cosign-key-pair", "1") + keyPair := helpers.NewCosignKeyPair(t, "cosign-key-pair", "1") reg := testregistry.NewWithNoAuth(base, 0, false) t.Cleanup(func() { - keyPair.cleanup() + keyPair.Cleanup() reg.Cleanup(nil) }) @@ -51,7 +51,7 @@ CMD ["echo", "nerdctl-build-test-string"] buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() - base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() - base.Cmd("run", "--rm", "--verify=cosign", "--cosign-key="+keyPair.publicKey, testImageRef).AssertOK() + base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.PrivateKey).AssertOK() + base.Cmd("run", "--rm", "--verify=cosign", "--cosign-key="+keyPair.PublicKey, testImageRef).AssertOK() base.Cmd("run", "--rm", "--verify=cosign", "--cosign-key=dummy", testImageRef).AssertFail() } diff --git a/cmd/nerdctl/helpers/testing.go b/cmd/nerdctl/helpers/testing.go index 553f4d996d2..d3197a531a5 100644 --- a/cmd/nerdctl/helpers/testing.go +++ b/cmd/nerdctl/helpers/testing.go @@ -18,6 +18,7 @@ package helpers import ( "context" + "fmt" "net" "os" "os/exec" @@ -139,3 +140,42 @@ func CreateBuildContext(t *testing.T, dockerfile string) string { assert.NilError(t, err) return tmpDir } + +func RequiresSoci(base *testutil.Base) { + info := base.Info() + for _, p := range info.Plugins.Storage { + if p == "soci" { + return + } + } + base.T.Skip("test requires soci") +} + +type CosignKeyPair struct { + PublicKey string + PrivateKey string + Cleanup func() +} + +func NewCosignKeyPair(t testing.TB, path string, password string) *CosignKeyPair { + td, err := os.MkdirTemp(t.TempDir(), path) + assert.NilError(t, err) + + cmd := exec.Command("cosign", "generate-key-pair") + cmd.Dir = td + cmd.Env = append(cmd.Env, fmt.Sprintf("COSIGN_PASSWORD=%s", password)) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("failed to run %v: %v (%q)", cmd.Args, err, string(out)) + } + + publicKey := filepath.Join(td, "cosign.pub") + privateKey := filepath.Join(td, "cosign.key") + + return &CosignKeyPair{ + PublicKey: publicKey, + PrivateKey: privateKey, + Cleanup: func() { + _ = os.RemoveAll(td) + }, + } +} diff --git a/cmd/nerdctl/helpers/testing_linux.go b/cmd/nerdctl/helpers/testing_linux.go index d459e9272b5..4aed02384bf 100644 --- a/cmd/nerdctl/helpers/testing_linux.go +++ b/cmd/nerdctl/helpers/testing_linux.go @@ -17,8 +17,13 @@ package helpers import ( + "encoding/json" + "errors" "fmt" "io" + "os" + "os/exec" + "path/filepath" "strings" "testing" "time" @@ -77,3 +82,56 @@ func ComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts base.Cmd("volume", "inspect", fmt.Sprintf("%s_db", projectName)).AssertFail() base.Cmd("network", "inspect", fmt.Sprintf("%s_default", projectName)).AssertFail() } + +func ExtractDockerArchive(archiveTarPath, rootfsPath string) error { + if err := os.MkdirAll(rootfsPath, 0755); err != nil { + return err + } + workDir, err := os.MkdirTemp("", "extract-docker-archive") + if err != nil { + return err + } + defer os.RemoveAll(workDir) + if err := ExtractTarFile(workDir, archiveTarPath); err != nil { + return err + } + manifestJSONPath := filepath.Join(workDir, "manifest.json") + manifestJSONBytes, err := os.ReadFile(manifestJSONPath) + if err != nil { + return err + } + var mani DockerArchiveManifestJSON + if err := json.Unmarshal(manifestJSONBytes, &mani); err != nil { + return err + } + if len(mani) > 1 { + return fmt.Errorf("multi-image archive cannot be extracted: contains %d images", len(mani)) + } + if len(mani) < 1 { + return errors.New("invalid archive") + } + ent := mani[0] + for _, l := range ent.Layers { + layerTarPath := filepath.Join(workDir, l) + if err := ExtractTarFile(rootfsPath, layerTarPath); err != nil { + return err + } + } + return nil +} + +type DockerArchiveManifestJSON []DockerArchiveManifestJSONEntry + +type DockerArchiveManifestJSONEntry struct { + Config string + RepoTags []string + Layers []string +} + +func ExtractTarFile(dirPath, tarFilePath string) error { + cmd := exec.Command("tar", "Cxf", dirPath, tarFilePath) + if out, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) + } + return nil +} diff --git a/cmd/nerdctl/image.go b/cmd/nerdctl/image/image.go similarity index 82% rename from cmd/nerdctl/image.go rename to cmd/nerdctl/image/image.go index 0a0f190483a..3c4bc48eeb3 100644 --- a/cmd/nerdctl/image.go +++ b/cmd/nerdctl/image/image.go @@ -14,15 +14,16 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/builder" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newImageCommand() *cobra.Command { +func NewImageCommand() *cobra.Command { cmd := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "image", @@ -32,15 +33,15 @@ func newImageCommand() *cobra.Command { SilenceErrors: true, } cmd.AddCommand( - newBuildCommand(), + builder.NewBuildCommand(), // commitCommand is in "container", not in "image" imageLsCommand(), - newHistoryCommand(), - newPullCommand(), - newPushCommand(), - newLoadCommand(), - newSaveCommand(), - newTagCommand(), + NewHistoryCommand(), + NewPullCommand(), + NewPushCommand(), + NewLoadCommand(), + NewSaveCommand(), + NewTagCommand(), imageRmCommand(), newImageConvertCommand(), newImageInspectCommand(), @@ -52,14 +53,14 @@ func newImageCommand() *cobra.Command { } func imageLsCommand() *cobra.Command { - x := newImagesCommand() + x := NewImagesCommand() x.Use = "ls" x.Aliases = []string{"list"} return x } func imageRmCommand() *cobra.Command { - x := newRmiCommand() + x := NewRmiCommand() x.Use = "rm" x.Aliases = []string{"remove"} return x diff --git a/cmd/nerdctl/image_convert.go b/cmd/nerdctl/image/image_convert.go similarity index 99% rename from cmd/nerdctl/image_convert.go rename to cmd/nerdctl/image/image_convert.go index 577099245f2..5b181580da6 100644 --- a/cmd/nerdctl/image_convert.go +++ b/cmd/nerdctl/image/image_convert.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "compress/gzip" diff --git a/cmd/nerdctl/image_convert_linux_test.go b/cmd/nerdctl/image/image_convert_linux_test.go similarity index 99% rename from cmd/nerdctl/image_convert_linux_test.go rename to cmd/nerdctl/image/image_convert_linux_test.go index 8022035d0c7..ae90cca5af9 100644 --- a/cmd/nerdctl/image_convert_linux_test.go +++ b/cmd/nerdctl/image/image_convert_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_convert_test.go b/cmd/nerdctl/image/image_convert_test.go similarity index 99% rename from cmd/nerdctl/image_convert_test.go rename to cmd/nerdctl/image/image_convert_test.go index 8df0b5d6e76..ca5780597d3 100644 --- a/cmd/nerdctl/image_convert_test.go +++ b/cmd/nerdctl/image/image_convert_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "runtime" diff --git a/cmd/nerdctl/image_cryptutil.go b/cmd/nerdctl/image/image_cryptutil.go similarity index 99% rename from cmd/nerdctl/image_cryptutil.go rename to cmd/nerdctl/image/image_cryptutil.go index 067a0f1afe6..0268bddb001 100644 --- a/cmd/nerdctl/image_cryptutil.go +++ b/cmd/nerdctl/image/image_cryptutil.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/image_decrypt.go b/cmd/nerdctl/image/image_decrypt.go similarity index 99% rename from cmd/nerdctl/image_decrypt.go rename to cmd/nerdctl/image/image_decrypt.go index fa6faf46629..6830d137d73 100644 --- a/cmd/nerdctl/image_decrypt.go +++ b/cmd/nerdctl/image/image_decrypt.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/image_encrypt.go b/cmd/nerdctl/image/image_encrypt.go similarity index 99% rename from cmd/nerdctl/image_encrypt.go rename to cmd/nerdctl/image/image_encrypt.go index 39ee30659f3..7c2ef199cbc 100644 --- a/cmd/nerdctl/image_encrypt.go +++ b/cmd/nerdctl/image/image_encrypt.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/image_encrypt_linux_test.go b/cmd/nerdctl/image/image_encrypt_linux_test.go similarity index 99% rename from cmd/nerdctl/image_encrypt_linux_test.go rename to cmd/nerdctl/image/image_encrypt_linux_test.go index 1cd71d49af9..61fb6c30981 100644 --- a/cmd/nerdctl/image_encrypt_linux_test.go +++ b/cmd/nerdctl/image/image_encrypt_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_history.go b/cmd/nerdctl/image/image_history.go similarity index 99% rename from cmd/nerdctl/image_history.go rename to cmd/nerdctl/image/image_history.go index d3d7a6a3c44..d4f4aa9bcc8 100644 --- a/cmd/nerdctl/image_history.go +++ b/cmd/nerdctl/image/image_history.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "bytes" @@ -43,7 +43,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil" ) -func newHistoryCommand() *cobra.Command { +func NewHistoryCommand() *cobra.Command { var historyCommand = &cobra.Command{ Use: "history [flags] IMAGE", Short: "Show the history of an image", diff --git a/cmd/nerdctl/image_history_test.go b/cmd/nerdctl/image/image_history_test.go similarity index 99% rename from cmd/nerdctl/image_history_test.go rename to cmd/nerdctl/image/image_history_test.go index d68930cb384..21bef4f5692 100644 --- a/cmd/nerdctl/image_history_test.go +++ b/cmd/nerdctl/image/image_history_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "encoding/json" diff --git a/cmd/nerdctl/image_inspect.go b/cmd/nerdctl/image/image_inspect.go similarity index 96% rename from cmd/nerdctl/image_inspect.go rename to cmd/nerdctl/image/image_inspect.go index c52e975df76..c24d8cf64e3 100644 --- a/cmd/nerdctl/image_inspect.go +++ b/cmd/nerdctl/image/image_inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -54,7 +54,7 @@ func newImageInspectCommand() *cobra.Command { return imageInspectCommand } -func processImageInspectOptions(cmd *cobra.Command, platform *string) (types.ImageInspectOptions, error) { +func ProcessImageInspectOptions(cmd *cobra.Command, platform *string) (types.ImageInspectOptions, error) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return types.ImageInspectOptions{}, err @@ -84,7 +84,7 @@ func processImageInspectOptions(cmd *cobra.Command, platform *string) (types.Ima } func imageInspectAction(cmd *cobra.Command, args []string) error { - options, err := processImageInspectOptions(cmd, nil) + options, err := ProcessImageInspectOptions(cmd, nil) if err != nil { return err } diff --git a/cmd/nerdctl/image_inspect_test.go b/cmd/nerdctl/image/image_inspect_test.go similarity index 99% rename from cmd/nerdctl/image_inspect_test.go rename to cmd/nerdctl/image/image_inspect_test.go index 4b54c6bbb13..14fcb90caf8 100644 --- a/cmd/nerdctl/image_inspect_test.go +++ b/cmd/nerdctl/image/image_inspect_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "encoding/json" diff --git a/cmd/nerdctl/image_list.go b/cmd/nerdctl/image/image_list.go similarity index 98% rename from cmd/nerdctl/image_list.go rename to cmd/nerdctl/image/image_list.go index c65e960347b..d01fbbd13e2 100644 --- a/cmd/nerdctl/image_list.go +++ b/cmd/nerdctl/image/image_list.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" @@ -29,7 +29,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/referenceutil" ) -func newImagesCommand() *cobra.Command { +func NewImagesCommand() *cobra.Command { shortHelp := "List images" longHelp := shortHelp + ` diff --git a/cmd/nerdctl/image_list_test.go b/cmd/nerdctl/image/image_list_test.go similarity index 99% rename from cmd/nerdctl/image_list_test.go rename to cmd/nerdctl/image/image_list_test.go index 12edc26472b..72e902fa62b 100644 --- a/cmd/nerdctl/image_list_test.go +++ b/cmd/nerdctl/image/image_list_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_load.go b/cmd/nerdctl/image/image_load.go similarity index 98% rename from cmd/nerdctl/image_load.go rename to cmd/nerdctl/image/image_load.go index 2cf8fdf3c05..b56a905a0ab 100644 --- a/cmd/nerdctl/image_load.go +++ b/cmd/nerdctl/image/image_load.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/image" ) -func newLoadCommand() *cobra.Command { +func NewLoadCommand() *cobra.Command { var loadCommand = &cobra.Command{ Use: "load", Args: cobra.NoArgs, diff --git a/cmd/nerdctl/image_load_linux_test.go b/cmd/nerdctl/image/image_load_linux_test.go similarity index 99% rename from cmd/nerdctl/image_load_linux_test.go rename to cmd/nerdctl/image/image_load_linux_test.go index f8e3c9684f8..d92a2167f80 100644 --- a/cmd/nerdctl/image_load_linux_test.go +++ b/cmd/nerdctl/image/image_load_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_prune.go b/cmd/nerdctl/image/image_prune.go similarity index 99% rename from cmd/nerdctl/image_prune.go rename to cmd/nerdctl/image/image_prune.go index 0480a6a544d..9eefa288958 100644 --- a/cmd/nerdctl/image_prune.go +++ b/cmd/nerdctl/image/image_prune.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_prune_test.go b/cmd/nerdctl/image/image_prune_test.go similarity index 99% rename from cmd/nerdctl/image_prune_test.go rename to cmd/nerdctl/image/image_prune_test.go index 2c07a5a330d..94ef0c625f2 100644 --- a/cmd/nerdctl/image_prune_test.go +++ b/cmd/nerdctl/image/image_prune_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" diff --git a/cmd/nerdctl/image_pull.go b/cmd/nerdctl/image/image_pull.go similarity index 99% rename from cmd/nerdctl/image_pull.go rename to cmd/nerdctl/image/image_pull.go index fdfb2420c6c..9b1f961d8e1 100644 --- a/cmd/nerdctl/image_pull.go +++ b/cmd/nerdctl/image/image_pull.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/strutil" ) -func newPullCommand() *cobra.Command { +func NewPullCommand() *cobra.Command { var pullCommand = &cobra.Command{ Use: "pull [flags] NAME[:TAG]", Short: "Pull an image from a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS.", diff --git a/cmd/nerdctl/image_pull_linux_test.go b/cmd/nerdctl/image/image_pull_linux_test.go similarity index 82% rename from cmd/nerdctl/image_pull_linux_test.go rename to cmd/nerdctl/image/image_pull_linux_test.go index 577e54291df..d3e956238cf 100644 --- a/cmd/nerdctl/image_pull_linux_test.go +++ b/cmd/nerdctl/image/image_pull_linux_test.go @@ -14,52 +14,19 @@ limitations under the License. */ -package main +package image import ( "fmt" - "os" "os/exec" - "path/filepath" "strings" "testing" - "gotest.tools/v3/assert" - "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" ) -type cosignKeyPair struct { - publicKey string - privateKey string - cleanup func() -} - -func newCosignKeyPair(t testing.TB, path string, password string) *cosignKeyPair { - td, err := os.MkdirTemp(t.TempDir(), path) - assert.NilError(t, err) - - cmd := exec.Command("cosign", "generate-key-pair") - cmd.Dir = td - cmd.Env = append(cmd.Env, fmt.Sprintf("COSIGN_PASSWORD=%s", password)) - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("failed to run %v: %v (%q)", cmd.Args, err, string(out)) - } - - publicKey := filepath.Join(td, "cosign.pub") - privateKey := filepath.Join(td, "cosign.key") - - return &cosignKeyPair{ - publicKey: publicKey, - privateKey: privateKey, - cleanup: func() { - _ = os.RemoveAll(td) - }, - } -} - func TestImageVerifyWithCosign(t *testing.T) { testutil.RequireExecutable(t, "cosign") testutil.DockerIncompatible(t) @@ -67,8 +34,8 @@ func TestImageVerifyWithCosign(t *testing.T) { testutil.RegisterBuildCacheCleanup(t) base := testutil.NewBase(t) base.Env = append(base.Env, "COSIGN_PASSWORD=1") - keyPair := newCosignKeyPair(t, "cosign-key-pair", "1") - defer keyPair.cleanup() + keyPair := helpers.NewCosignKeyPair(t, "cosign-key-pair", "1") + defer keyPair.Cleanup() tID := testutil.Identifier(t) reg := testregistry.NewWithNoAuth(base, 0, false) defer reg.Cleanup(nil) @@ -85,8 +52,8 @@ CMD ["echo", "nerdctl-build-test-string"] buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() - base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() - base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+keyPair.publicKey).AssertOK() + base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.PrivateKey).AssertOK() + base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+keyPair.PublicKey).AssertOK() } func TestImagePullPlainHttpWithDefaultPort(t *testing.T) { @@ -117,8 +84,8 @@ func TestImageVerifyWithCosignShouldFailWhenKeyIsNotCorrect(t *testing.T) { testutil.RegisterBuildCacheCleanup(t) base := testutil.NewBase(t) base.Env = append(base.Env, "COSIGN_PASSWORD=1") - keyPair := newCosignKeyPair(t, "cosign-key-pair", "1") - defer keyPair.cleanup() + keyPair := helpers.NewCosignKeyPair(t, "cosign-key-pair", "1") + defer keyPair.Cleanup() tID := testutil.Identifier(t) reg := testregistry.NewWithNoAuth(base, 0, false) defer reg.Cleanup(nil) @@ -135,12 +102,12 @@ CMD ["echo", "nerdctl-build-test-string"] buildCtx := helpers.CreateBuildContext(t, dockerfile) base.Cmd("build", "-t", testImageRef, buildCtx).AssertOK() - base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.privateKey).AssertOK() - base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+keyPair.publicKey).AssertOK() + base.Cmd("push", testImageRef, "--sign=cosign", "--cosign-key="+keyPair.PrivateKey).AssertOK() + base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+keyPair.PublicKey).AssertOK() base.Env = append(base.Env, "COSIGN_PASSWORD=2") - newKeyPair := newCosignKeyPair(t, "cosign-key-pair-test", "2") - base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+newKeyPair.publicKey).AssertFail() + newKeyPair := helpers.NewCosignKeyPair(t, "cosign-key-pair-test", "2") + base.Cmd("pull", testImageRef, "--verify=cosign", "--cosign-key="+newKeyPair.PublicKey).AssertFail() } func TestPullSoci(t *testing.T) { @@ -168,7 +135,7 @@ func TestPullSoci(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { base := testutil.NewBase(t) - requiresSoci(base) + helpers.RequiresSoci(base) //counting initial snapshot mounts initialMounts, err := exec.Command("mount").Output() diff --git a/cmd/nerdctl/image_push.go b/cmd/nerdctl/image/image_push.go similarity index 99% rename from cmd/nerdctl/image_push.go rename to cmd/nerdctl/image/image_push.go index 147e8dfa063..eebadbf7586 100644 --- a/cmd/nerdctl/image_push.go +++ b/cmd/nerdctl/image/image_push.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -30,7 +30,7 @@ const ( allowNonDistFlag = "allow-nondistributable-artifacts" ) -func newPushCommand() *cobra.Command { +func NewPushCommand() *cobra.Command { var pushCommand = &cobra.Command{ Use: "push [flags] NAME[:TAG]", Short: "Push an image or a repository to a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to push image to IPFS.", diff --git a/cmd/nerdctl/image_push_linux_test.go b/cmd/nerdctl/image/image_push_linux_test.go similarity index 98% rename from cmd/nerdctl/image_push_linux_test.go rename to cmd/nerdctl/image/image_push_linux_test.go index 17ab1a89227..17f757703cf 100644 --- a/cmd/nerdctl/image_push_linux_test.go +++ b/cmd/nerdctl/image/image_push_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" @@ -24,6 +24,7 @@ import ( "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/testregistry" ) @@ -179,7 +180,7 @@ func TestPushNonDistributableArtifacts(t *testing.T) { func TestPushSoci(t *testing.T) { testutil.DockerIncompatible(t) base := testutil.NewBase(t) - requiresSoci(base) + helpers.RequiresSoci(base) reg := testregistry.NewWithNoAuth(base, 0, false) defer reg.Cleanup(nil) diff --git a/cmd/nerdctl/image_remove.go b/cmd/nerdctl/image/image_remove.go similarity index 98% rename from cmd/nerdctl/image_remove.go rename to cmd/nerdctl/image/image_remove.go index dde5a6d035e..cb9a86c39d5 100644 --- a/cmd/nerdctl/image_remove.go +++ b/cmd/nerdctl/image/image_remove.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/image" ) -func newRmiCommand() *cobra.Command { +func NewRmiCommand() *cobra.Command { var rmiCommand = &cobra.Command{ Use: "rmi [flags] IMAGE [IMAGE, ...]", Short: "Remove one or more images", diff --git a/cmd/nerdctl/image_remove_linux_test.go b/cmd/nerdctl/image/image_remove_linux_test.go similarity index 99% rename from cmd/nerdctl/image_remove_linux_test.go rename to cmd/nerdctl/image/image_remove_linux_test.go index 55fbc0a898b..5752aa04aa4 100644 --- a/cmd/nerdctl/image_remove_linux_test.go +++ b/cmd/nerdctl/image/image_remove_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "testing" diff --git a/cmd/nerdctl/image_save.go b/cmd/nerdctl/image/image_save.go similarity index 98% rename from cmd/nerdctl/image_save.go rename to cmd/nerdctl/image/image_save.go index 287e93c1b39..8c2ce38d309 100644 --- a/cmd/nerdctl/image_save.go +++ b/cmd/nerdctl/image/image_save.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "fmt" @@ -30,7 +30,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/image" ) -func newSaveCommand() *cobra.Command { +func NewSaveCommand() *cobra.Command { var saveCommand = &cobra.Command{ Use: "save", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/image_save_linux_test.go b/cmd/nerdctl/image/image_save_linux_test.go similarity index 51% rename from cmd/nerdctl/image_save_linux_test.go rename to cmd/nerdctl/image/image_save_linux_test.go index 0536d180983..0c7c722e97e 100644 --- a/cmd/nerdctl/image_save_linux_test.go +++ b/cmd/nerdctl/image/image_save_linux_test.go @@ -14,20 +14,17 @@ limitations under the License. */ -package main +package image import ( - "encoding/json" - "errors" - "fmt" "os" - "os/exec" "path/filepath" "strings" "testing" "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -41,7 +38,7 @@ func TestSave(t *testing.T) { archiveTarPath := filepath.Join(t.TempDir(), "a.tar") base.Cmd("save", "-o", archiveTarPath, testutil.AlpineImage).AssertOK() rootfsPath := filepath.Join(t.TempDir(), "rootfs") - err := extractDockerArchive(archiveTarPath, rootfsPath) + err := helpers.ExtractDockerArchive(archiveTarPath, rootfsPath) assert.NilError(t, err) etcOSReleasePath := filepath.Join(rootfsPath, "/etc/os-release") etcOSReleaseBytes, err := os.ReadFile(etcOSReleasePath) @@ -51,56 +48,3 @@ func TestSave(t *testing.T) { t.Log(etcOSRelease) assert.Assert(t, strings.Contains(etcOSRelease, "Alpine")) } - -func extractDockerArchive(archiveTarPath, rootfsPath string) error { - if err := os.MkdirAll(rootfsPath, 0755); err != nil { - return err - } - workDir, err := os.MkdirTemp("", "extract-docker-archive") - if err != nil { - return err - } - defer os.RemoveAll(workDir) - if err := extractTarFile(workDir, archiveTarPath); err != nil { - return err - } - manifestJSONPath := filepath.Join(workDir, "manifest.json") - manifestJSONBytes, err := os.ReadFile(manifestJSONPath) - if err != nil { - return err - } - var mani DockerArchiveManifestJSON - if err := json.Unmarshal(manifestJSONBytes, &mani); err != nil { - return err - } - if len(mani) > 1 { - return fmt.Errorf("multi-image archive cannot be extracted: contains %d images", len(mani)) - } - if len(mani) < 1 { - return errors.New("invalid archive") - } - ent := mani[0] - for _, l := range ent.Layers { - layerTarPath := filepath.Join(workDir, l) - if err := extractTarFile(rootfsPath, layerTarPath); err != nil { - return err - } - } - return nil -} - -type DockerArchiveManifestJSON []DockerArchiveManifestJSONEntry - -type DockerArchiveManifestJSONEntry struct { - Config string - RepoTags []string - Layers []string -} - -func extractTarFile(dirPath, tarFilePath string) error { - cmd := exec.Command("tar", "Cxf", dirPath, tarFilePath) - if out, err := cmd.CombinedOutput(); err != nil { - return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) - } - return nil -} diff --git a/cmd/nerdctl/image_save_test.go b/cmd/nerdctl/image/image_save_test.go similarity index 99% rename from cmd/nerdctl/image_save_test.go rename to cmd/nerdctl/image/image_save_test.go index 4b37a497b93..c8078967477 100644 --- a/cmd/nerdctl/image_save_test.go +++ b/cmd/nerdctl/image/image_save_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "path/filepath" diff --git a/cmd/nerdctl/image_tag.go b/cmd/nerdctl/image/image_tag.go similarity index 97% rename from cmd/nerdctl/image_tag.go rename to cmd/nerdctl/image/image_tag.go index 9989665d12d..4e3d5b965fc 100644 --- a/cmd/nerdctl/image_tag.go +++ b/cmd/nerdctl/image/image_tag.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package image import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/image" ) -func newTagCommand() *cobra.Command { +func NewTagCommand() *cobra.Command { var tagCommand = &cobra.Command{ Use: "tag [flags] SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]", Short: "Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE", diff --git a/cmd/nerdctl/image/image_test.go b/cmd/nerdctl/image/image_test.go new file mode 100644 index 00000000000..4cbba7d968f --- /dev/null +++ b/cmd/nerdctl/image/image_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package image + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/inspect.go b/cmd/nerdctl/inspect.go index 14f92c8a379..c9cf64c6949 100644 --- a/cmd/nerdctl/inspect.go +++ b/cmd/nerdctl/inspect.go @@ -24,6 +24,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + imageCmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/image" "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/clientutil" "github.com/containerd/nerdctl/v2/pkg/cmd/container" @@ -115,7 +116,7 @@ func inspectAction(cmd *cobra.Command, args []string) error { var containerInspectOptions types.ContainerInspectOptions if inspectImage { platform := "" - imageInspectOptions, err = processImageInspectOptions(cmd, &platform) + imageInspectOptions, err = imageCmd.ProcessImageInspectOptions(cmd, &platform) if err != nil { return err } diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 373cbc80e20..16c8b8816a0 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -31,8 +31,10 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/builder" "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/image" "github.com/containerd/nerdctl/v2/cmd/nerdctl/ipfs" "github.com/containerd/nerdctl/v2/cmd/nerdctl/login" "github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace" @@ -266,17 +268,17 @@ Config file ($NERDCTL_TOML): %s // #endregion // Build - newBuildCommand(), + builder.NewBuildCommand(), // #region Image management - newImagesCommand(), - newPullCommand(), - newPushCommand(), - newLoadCommand(), - newSaveCommand(), - newTagCommand(), - newRmiCommand(), - newHistoryCommand(), + image.NewImagesCommand(), + image.NewPullCommand(), + image.NewPushCommand(), + image.NewLoadCommand(), + image.NewSaveCommand(), + image.NewTagCommand(), + image.NewRmiCommand(), + image.NewHistoryCommand(), // #endregion // #region System @@ -294,12 +296,12 @@ Config file ($NERDCTL_TOML): %s // #region helpers.Management newContainerCommand(), - newImageCommand(), + image.NewImageCommand(), network.NewNetworkCommand(), volume.NewVolumeCommand(), newSystemCommand(), namespace.NewNamespaceCommand(), - newBuilderCommand(), + builder.NewBuilderCommand(), // #endregion // Internal diff --git a/cmd/nerdctl/system_prune.go b/cmd/nerdctl/system_prune.go index 2f446572618..cfe1cb41661 100644 --- a/cmd/nerdctl/system_prune.go +++ b/cmd/nerdctl/system_prune.go @@ -24,6 +24,7 @@ import ( "github.com/containerd/log" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/builder" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" "github.com/containerd/nerdctl/v2/pkg/api/types" @@ -62,7 +63,7 @@ func processSystemPruneOptions(cmd *cobra.Command) (types.SystemPruneOptions, er return types.SystemPruneOptions{}, err } - buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace) + buildkitHost, err := builder.GetBuildkitHost(cmd, globalOptions.Namespace) if err != nil { log.L.WithError(err).Warn("BuildKit is not running. Build caches will not be pruned.") buildkitHost = "" From c115c49c4cd9c811699e65cf124fa81c63b58154 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 11:24:00 -0700 Subject: [PATCH 15/16] cmd/AppArmor moved to subpackage Signed-off-by: apostasie --- .../{ => apparmor}/apparmor_inspect_linux.go | 2 +- cmd/nerdctl/{ => apparmor}/apparmor_linux.go | 4 +-- cmd/nerdctl/apparmor/apparmor_linux_test.go | 27 +++++++++++++++++++ .../{ => apparmor}/apparmor_list_linux.go | 2 +- .../{ => apparmor}/apparmor_load_linux.go | 2 +- .../{ => apparmor}/apparmor_unload_linux.go | 2 +- cmd/nerdctl/main_linux.go | 3 ++- 7 files changed, 35 insertions(+), 7 deletions(-) rename cmd/nerdctl/{ => apparmor}/apparmor_inspect_linux.go (98%) rename cmd/nerdctl/{ => apparmor}/apparmor_linux.go (94%) create mode 100644 cmd/nerdctl/apparmor/apparmor_linux_test.go rename cmd/nerdctl/{ => apparmor}/apparmor_list_linux.go (99%) rename cmd/nerdctl/{ => apparmor}/apparmor_load_linux.go (98%) rename cmd/nerdctl/{ => apparmor}/apparmor_unload_linux.go (98%) diff --git a/cmd/nerdctl/apparmor_inspect_linux.go b/cmd/nerdctl/apparmor/apparmor_inspect_linux.go similarity index 98% rename from cmd/nerdctl/apparmor_inspect_linux.go rename to cmd/nerdctl/apparmor/apparmor_inspect_linux.go index 681826bf82e..acfec41a959 100644 --- a/cmd/nerdctl/apparmor_inspect_linux.go +++ b/cmd/nerdctl/apparmor/apparmor_inspect_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package apparmor import ( "fmt" diff --git a/cmd/nerdctl/apparmor_linux.go b/cmd/nerdctl/apparmor/apparmor_linux.go similarity index 94% rename from cmd/nerdctl/apparmor_linux.go rename to cmd/nerdctl/apparmor/apparmor_linux.go index c94a80258ff..d1fa8ff977a 100644 --- a/cmd/nerdctl/apparmor_linux.go +++ b/cmd/nerdctl/apparmor/apparmor_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package apparmor import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newApparmorCommand() *cobra.Command { +func NewApparmorCommand() *cobra.Command { cmd := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "apparmor", diff --git a/cmd/nerdctl/apparmor/apparmor_linux_test.go b/cmd/nerdctl/apparmor/apparmor_linux_test.go new file mode 100644 index 00000000000..2cf255871ba --- /dev/null +++ b/cmd/nerdctl/apparmor/apparmor_linux_test.go @@ -0,0 +1,27 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package apparmor + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/apparmor_list_linux.go b/cmd/nerdctl/apparmor/apparmor_list_linux.go similarity index 99% rename from cmd/nerdctl/apparmor_list_linux.go rename to cmd/nerdctl/apparmor/apparmor_list_linux.go index e650e3ea21a..f1483e99db3 100644 --- a/cmd/nerdctl/apparmor_list_linux.go +++ b/cmd/nerdctl/apparmor/apparmor_list_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package apparmor import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/apparmor_load_linux.go b/cmd/nerdctl/apparmor/apparmor_load_linux.go similarity index 98% rename from cmd/nerdctl/apparmor_load_linux.go rename to cmd/nerdctl/apparmor/apparmor_load_linux.go index 0c04ae47783..ccd51f406d4 100644 --- a/cmd/nerdctl/apparmor_load_linux.go +++ b/cmd/nerdctl/apparmor/apparmor_load_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package apparmor import ( "fmt" diff --git a/cmd/nerdctl/apparmor_unload_linux.go b/cmd/nerdctl/apparmor/apparmor_unload_linux.go similarity index 98% rename from cmd/nerdctl/apparmor_unload_linux.go rename to cmd/nerdctl/apparmor/apparmor_unload_linux.go index c20aab68765..2ba93809544 100644 --- a/cmd/nerdctl/apparmor_unload_linux.go +++ b/cmd/nerdctl/apparmor/apparmor_unload_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package apparmor import ( "fmt" diff --git a/cmd/nerdctl/main_linux.go b/cmd/nerdctl/main_linux.go index 2e0c0a853e2..d1dfbeaf615 100644 --- a/cmd/nerdctl/main_linux.go +++ b/cmd/nerdctl/main_linux.go @@ -19,6 +19,7 @@ package main import ( "github.com/spf13/cobra" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/apparmor" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/strutil" ) @@ -63,7 +64,7 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { } func addApparmorCommand(rootCmd *cobra.Command) { - rootCmd.AddCommand(newApparmorCommand()) + rootCmd.AddCommand(apparmor.NewApparmorCommand()) } func addCpCommand(rootCmd *cobra.Command) { From d5a00f46d75af7b8bf622dacd0b5b3dfb49344c3 Mon Sep 17 00:00:00 2001 From: apostasie Date: Fri, 30 Aug 2024 11:26:33 -0700 Subject: [PATCH 16/16] Move cmd/internal to subpackage Signed-off-by: apostasie --- cmd/nerdctl/{ => internal}/internal.go | 4 ++-- cmd/nerdctl/{ => internal}/internal_oci_hook.go | 2 +- cmd/nerdctl/main.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) rename cmd/nerdctl/{ => internal}/internal.go (93%) rename cmd/nerdctl/{ => internal}/internal_oci_hook.go (98%) diff --git a/cmd/nerdctl/internal.go b/cmd/nerdctl/internal/internal.go similarity index 93% rename from cmd/nerdctl/internal.go rename to cmd/nerdctl/internal/internal.go index 9f62d1ea2ae..c159452823a 100644 --- a/cmd/nerdctl/internal.go +++ b/cmd/nerdctl/internal/internal.go @@ -14,13 +14,13 @@ limitations under the License. */ -package main +package internal import ( "github.com/spf13/cobra" ) -func newInternalCommand() *cobra.Command { +func NewInternalCommand() *cobra.Command { var internalCommand = &cobra.Command{ Use: "internal", Short: "DO NOT EXECUTE MANUALLY", diff --git a/cmd/nerdctl/internal_oci_hook.go b/cmd/nerdctl/internal/internal_oci_hook.go similarity index 98% rename from cmd/nerdctl/internal_oci_hook.go rename to cmd/nerdctl/internal/internal_oci_hook.go index 4ebad5d2383..404c865804b 100644 --- a/cmd/nerdctl/internal_oci_hook.go +++ b/cmd/nerdctl/internal/internal_oci_hook.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package internal import ( "errors" diff --git a/cmd/nerdctl/main.go b/cmd/nerdctl/main.go index 16c8b8816a0..f02d8867b0e 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -35,6 +35,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/cmd/nerdctl/image" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/internal" "github.com/containerd/nerdctl/v2/cmd/nerdctl/ipfs" "github.com/containerd/nerdctl/v2/cmd/nerdctl/login" "github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace" @@ -305,7 +306,7 @@ Config file ($NERDCTL_TOML): %s // #endregion // Internal - newInternalCommand(), + internal.NewInternalCommand(), // login login.NewLoginCommand(),