diff --git a/cmd/nerdctl/compose.go b/cmd/nerdctl/compose/compose.go similarity index 93% rename from cmd/nerdctl/compose.go rename to cmd/nerdctl/compose/compose.go index 5c3a30368c0..e85d31755f4 100644 --- a/cmd/nerdctl/compose.go +++ b/cmd/nerdctl/compose/compose.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" @@ -23,7 +23,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/composer" ) -func newComposeCommand() *cobra.Command { +func NewComposeCommand() *cobra.Command { var composeCommand = &cobra.Command{ Use: "compose [flags] COMMAND", Short: "Compose", @@ -33,7 +33,7 @@ func newComposeCommand() *cobra.Command { TraverseChildren: true, // required for global short hands like -f } // `-f` is a nonPersistentAlias, as it conflicts with `nerdctl compose logs --follow` - AddPersistentStringArrayFlag(composeCommand, "file", nil, []string{"f"}, nil, "", "Specify an alternate compose file") + helpers.AddPersistentStringArrayFlag(composeCommand, "file", nil, []string{"f"}, nil, "", "Specify an alternate compose file") composeCommand.PersistentFlags().String("project-directory", "", "Specify an alternate working directory") composeCommand.PersistentFlags().StringP("project-name", "p", "", "Specify an alternate project name") composeCommand.PersistentFlags().String("env-file", "", "Specify an alternate environment file") @@ -70,7 +70,7 @@ func newComposeCommand() *cobra.Command { } func getComposeOptions(cmd *cobra.Command, debugFull, experimental bool) (composer.Options, error) { - nerdctlCmd, nerdctlArgs := globalFlags(cmd) + nerdctlCmd, nerdctlArgs := helpers.GlobalFlags(cmd) projectDirectory, err := cmd.Flags().GetString("project-directory") if err != nil { return composer.Options{}, err diff --git a/cmd/nerdctl/compose_build.go b/cmd/nerdctl/compose/compose_build.go similarity index 99% rename from cmd/nerdctl/compose_build.go rename to cmd/nerdctl/compose/compose_build.go index 2c8ad6c34a6..fbd72ff0f39 100644 --- a/cmd/nerdctl/compose_build.go +++ b/cmd/nerdctl/compose/compose_build.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_build_linux_test.go b/cmd/nerdctl/compose/compose_build_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_build_linux_test.go rename to cmd/nerdctl/compose/compose_build_linux_test.go index 69046355e6a..80cc04d4c35 100644 --- a/cmd/nerdctl/compose_build_linux_test.go +++ b/cmd/nerdctl/compose/compose_build_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_config.go b/cmd/nerdctl/compose/compose_config.go similarity index 99% rename from cmd/nerdctl/compose_config.go rename to cmd/nerdctl/compose/compose_config.go index 4467d3f20df..25a3305779f 100644 --- a/cmd/nerdctl/compose_config.go +++ b/cmd/nerdctl/compose/compose_config.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_config_test.go b/cmd/nerdctl/compose/compose_config_test.go similarity index 99% rename from cmd/nerdctl/compose_config_test.go rename to cmd/nerdctl/compose/compose_config_test.go index 415a4c1e7b4..18dd728da5a 100644 --- a/cmd/nerdctl/compose_config_test.go +++ b/cmd/nerdctl/compose/compose_config_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_cp.go b/cmd/nerdctl/compose/compose_cp.go similarity index 99% rename from cmd/nerdctl/compose_cp.go rename to cmd/nerdctl/compose/compose_cp.go index 90e7cb8309e..a35078bc405 100644 --- a/cmd/nerdctl/compose_cp.go +++ b/cmd/nerdctl/compose/compose_cp.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_cp_linux_test.go b/cmd/nerdctl/compose/compose_cp_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_cp_linux_test.go rename to cmd/nerdctl/compose/compose_cp_linux_test.go index 1ea44db4c23..605210d8946 100644 --- a/cmd/nerdctl/compose_cp_linux_test.go +++ b/cmd/nerdctl/compose/compose_cp_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_create.go b/cmd/nerdctl/compose/compose_create.go similarity index 99% rename from cmd/nerdctl/compose_create.go rename to cmd/nerdctl/compose/compose_create.go index 14a8021460a..211cc4758f9 100644 --- a/cmd/nerdctl/compose_create.go +++ b/cmd/nerdctl/compose/compose_create.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_create_linux_test.go b/cmd/nerdctl/compose/compose_create_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_create_linux_test.go rename to cmd/nerdctl/compose/compose_create_linux_test.go index 9ad9ea59429..43f2dc40067 100644 --- a/cmd/nerdctl/compose_create_linux_test.go +++ b/cmd/nerdctl/compose/compose_create_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_down.go b/cmd/nerdctl/compose/compose_down.go similarity index 99% rename from cmd/nerdctl/compose_down.go rename to cmd/nerdctl/compose/compose_down.go index 9e29281282f..30552d18947 100644 --- a/cmd/nerdctl/compose_down.go +++ b/cmd/nerdctl/compose/compose_down.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_down_linux_test.go b/cmd/nerdctl/compose/compose_down_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_down_linux_test.go rename to cmd/nerdctl/compose/compose_down_linux_test.go index 7b8a393b523..b995631d6b6 100644 --- a/cmd/nerdctl/compose_down_linux_test.go +++ b/cmd/nerdctl/compose/compose_down_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_exec.go b/cmd/nerdctl/compose/compose_exec.go similarity index 99% rename from cmd/nerdctl/compose_exec.go rename to cmd/nerdctl/compose/compose_exec.go index 36958da83ff..79d5da5bdcb 100644 --- a/cmd/nerdctl/compose_exec.go +++ b/cmd/nerdctl/compose/compose_exec.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_exec_linux_test.go b/cmd/nerdctl/compose/compose_exec_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_exec_linux_test.go rename to cmd/nerdctl/compose/compose_exec_linux_test.go index 7a1ea165a54..24e0b3a51de 100644 --- a/cmd/nerdctl/compose_exec_linux_test.go +++ b/cmd/nerdctl/compose/compose_exec_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_images.go b/cmd/nerdctl/compose/compose_images.go similarity index 99% rename from cmd/nerdctl/compose_images.go rename to cmd/nerdctl/compose/compose_images.go index fcb2a373ea0..30212559e1d 100644 --- a/cmd/nerdctl/compose_images.go +++ b/cmd/nerdctl/compose/compose_images.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "context" diff --git a/cmd/nerdctl/compose_images_linux_test.go b/cmd/nerdctl/compose/compose_images_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_images_linux_test.go rename to cmd/nerdctl/compose/compose_images_linux_test.go index dd4ae118b72..f9f7f475186 100644 --- a/cmd/nerdctl/compose_images_linux_test.go +++ b/cmd/nerdctl/compose/compose_images_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "encoding/json" diff --git a/cmd/nerdctl/compose_kill.go b/cmd/nerdctl/compose/compose_kill.go similarity index 99% rename from cmd/nerdctl/compose_kill.go rename to cmd/nerdctl/compose/compose_kill.go index 1166e24a371..b1adcf4c753 100644 --- a/cmd/nerdctl/compose_kill.go +++ b/cmd/nerdctl/compose/compose_kill.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_kill_linux_test.go b/cmd/nerdctl/compose/compose_kill_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_kill_linux_test.go rename to cmd/nerdctl/compose/compose_kill_linux_test.go index 59e95519636..6571950a62e 100644 --- a/cmd/nerdctl/compose_kill_linux_test.go +++ b/cmd/nerdctl/compose/compose_kill_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_logs.go b/cmd/nerdctl/compose/compose_logs.go similarity index 99% rename from cmd/nerdctl/compose_logs.go rename to cmd/nerdctl/compose/compose_logs.go index c038d712866..5a6fd723cc3 100644 --- a/cmd/nerdctl/compose_logs.go +++ b/cmd/nerdctl/compose/compose_logs.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_pause.go b/cmd/nerdctl/compose/compose_pause.go similarity index 99% rename from cmd/nerdctl/compose_pause.go rename to cmd/nerdctl/compose/compose_pause.go index 6250214a269..9fd02bf3fa7 100644 --- a/cmd/nerdctl/compose_pause.go +++ b/cmd/nerdctl/compose/compose_pause.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_pause_linux_test.go b/cmd/nerdctl/compose/compose_pause_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_pause_linux_test.go rename to cmd/nerdctl/compose/compose_pause_linux_test.go index 34c259734c6..381e8686d6b 100644 --- a/cmd/nerdctl/compose_pause_linux_test.go +++ b/cmd/nerdctl/compose/compose_pause_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_port.go b/cmd/nerdctl/compose/compose_port.go similarity index 99% rename from cmd/nerdctl/compose_port.go rename to cmd/nerdctl/compose/compose_port.go index e654f45f9f1..2cbf14b0287 100644 --- a/cmd/nerdctl/compose_port.go +++ b/cmd/nerdctl/compose/compose_port.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_port_linux_test.go b/cmd/nerdctl/compose/compose_port_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_port_linux_test.go rename to cmd/nerdctl/compose/compose_port_linux_test.go index 39eff5c00a9..15946557ad2 100644 --- a/cmd/nerdctl/compose_port_linux_test.go +++ b/cmd/nerdctl/compose/compose_port_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_ps.go b/cmd/nerdctl/compose/compose_ps.go similarity index 99% rename from cmd/nerdctl/compose_ps.go rename to cmd/nerdctl/compose/compose_ps.go index 684428ad8c8..421731f78b7 100644 --- a/cmd/nerdctl/compose_ps.go +++ b/cmd/nerdctl/compose/compose_ps.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "context" diff --git a/cmd/nerdctl/compose_ps_linux_test.go b/cmd/nerdctl/compose/compose_ps_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_ps_linux_test.go rename to cmd/nerdctl/compose/compose_ps_linux_test.go index 5bf8e7a0358..df6f1d3cfe5 100644 --- a/cmd/nerdctl/compose_ps_linux_test.go +++ b/cmd/nerdctl/compose/compose_ps_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "encoding/json" diff --git a/cmd/nerdctl/compose_pull.go b/cmd/nerdctl/compose/compose_pull.go similarity index 99% rename from cmd/nerdctl/compose_pull.go rename to cmd/nerdctl/compose/compose_pull.go index dc90dc17843..9763377d4a5 100644 --- a/cmd/nerdctl/compose_pull.go +++ b/cmd/nerdctl/compose/compose_pull.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_pull_linux_test.go b/cmd/nerdctl/compose/compose_pull_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_pull_linux_test.go rename to cmd/nerdctl/compose/compose_pull_linux_test.go index 0040a4f84d9..64e267baa24 100644 --- a/cmd/nerdctl/compose_pull_linux_test.go +++ b/cmd/nerdctl/compose/compose_pull_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_push.go b/cmd/nerdctl/compose/compose_push.go similarity index 99% rename from cmd/nerdctl/compose_push.go rename to cmd/nerdctl/compose/compose_push.go index 6aca0c64e5a..50063d556e9 100644 --- a/cmd/nerdctl/compose_push.go +++ b/cmd/nerdctl/compose/compose_push.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_restart.go b/cmd/nerdctl/compose/compose_restart.go similarity index 99% rename from cmd/nerdctl/compose_restart.go rename to cmd/nerdctl/compose/compose_restart.go index 170a3fc4b62..0ee3a936cf1 100644 --- a/cmd/nerdctl/compose_restart.go +++ b/cmd/nerdctl/compose/compose_restart.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_restart_linux_test.go b/cmd/nerdctl/compose/compose_restart_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_restart_linux_test.go rename to cmd/nerdctl/compose/compose_restart_linux_test.go index 4b16f136215..6d5fe1fdedc 100644 --- a/cmd/nerdctl/compose_restart_linux_test.go +++ b/cmd/nerdctl/compose/compose_restart_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_rm.go b/cmd/nerdctl/compose/compose_rm.go similarity index 99% rename from cmd/nerdctl/compose_rm.go rename to cmd/nerdctl/compose/compose_rm.go index b0c32b47c1f..d82e345db76 100644 --- a/cmd/nerdctl/compose_rm.go +++ b/cmd/nerdctl/compose/compose_rm.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_rm_linux_test.go b/cmd/nerdctl/compose/compose_rm_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_rm_linux_test.go rename to cmd/nerdctl/compose/compose_rm_linux_test.go index 22ce6f5a487..948ea9e119d 100644 --- a/cmd/nerdctl/compose_rm_linux_test.go +++ b/cmd/nerdctl/compose/compose_rm_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_run.go b/cmd/nerdctl/compose/compose_run.go similarity index 99% rename from cmd/nerdctl/compose_run.go rename to cmd/nerdctl/compose/compose_run.go index 9f1ffa76c0b..afe02895ae0 100644 --- a/cmd/nerdctl/compose_run.go +++ b/cmd/nerdctl/compose/compose_run.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_run_linux_test.go b/cmd/nerdctl/compose/compose_run_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_run_linux_test.go rename to cmd/nerdctl/compose/compose_run_linux_test.go index e3e3e5ea559..65b36e7ffb6 100644 --- a/cmd/nerdctl/compose_run_linux_test.go +++ b/cmd/nerdctl/compose/compose_run_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_start.go b/cmd/nerdctl/compose/compose_start.go similarity index 99% rename from cmd/nerdctl/compose_start.go rename to cmd/nerdctl/compose/compose_start.go index 08ca46b33da..ce0cde46acc 100644 --- a/cmd/nerdctl/compose_start.go +++ b/cmd/nerdctl/compose/compose_start.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "context" diff --git a/cmd/nerdctl/compose_start_linux_test.go b/cmd/nerdctl/compose/compose_start_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_start_linux_test.go rename to cmd/nerdctl/compose/compose_start_linux_test.go index 79ce8c990a8..11c1581cd92 100644 --- a/cmd/nerdctl/compose_start_linux_test.go +++ b/cmd/nerdctl/compose/compose_start_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_stop.go b/cmd/nerdctl/compose/compose_stop.go similarity index 99% rename from cmd/nerdctl/compose_stop.go rename to cmd/nerdctl/compose/compose_stop.go index 7c769276700..2f3382a9822 100644 --- a/cmd/nerdctl/compose_stop.go +++ b/cmd/nerdctl/compose/compose_stop.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/compose_stop_linux_test.go b/cmd/nerdctl/compose/compose_stop_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_stop_linux_test.go rename to cmd/nerdctl/compose/compose_stop_linux_test.go index dcb1250e8a5..e10b16ff7b2 100644 --- a/cmd/nerdctl/compose_stop_linux_test.go +++ b/cmd/nerdctl/compose/compose_stop_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose/compose_test.go b/cmd/nerdctl/compose/compose_test.go new file mode 100644 index 00000000000..efcfd184e5b --- /dev/null +++ b/cmd/nerdctl/compose/compose_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 compose + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/compose_top.go b/cmd/nerdctl/compose/compose_top.go similarity index 99% rename from cmd/nerdctl/compose_top.go rename to cmd/nerdctl/compose/compose_top.go index f6466f2329b..0bb8dadccea 100644 --- a/cmd/nerdctl/compose_top.go +++ b/cmd/nerdctl/compose/compose_top.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_top_linux_test.go b/cmd/nerdctl/compose/compose_top_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_top_linux_test.go rename to cmd/nerdctl/compose/compose_top_linux_test.go index 09c01062f03..a0474c51b0b 100644 --- a/cmd/nerdctl/compose_top_linux_test.go +++ b/cmd/nerdctl/compose/compose_top_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_up.go b/cmd/nerdctl/compose/compose_up.go similarity index 99% rename from cmd/nerdctl/compose_up.go rename to cmd/nerdctl/compose/compose_up.go index b0c8fddab5e..f2829ae1e02 100644 --- a/cmd/nerdctl/compose_up.go +++ b/cmd/nerdctl/compose/compose_up.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "errors" diff --git a/cmd/nerdctl/compose_up_linux_test.go b/cmd/nerdctl/compose/compose_up_linux_test.go similarity index 99% rename from cmd/nerdctl/compose_up_linux_test.go rename to cmd/nerdctl/compose/compose_up_linux_test.go index d17df207462..6dbd6b7cf34 100644 --- a/cmd/nerdctl/compose_up_linux_test.go +++ b/cmd/nerdctl/compose/compose_up_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_up_test.go b/cmd/nerdctl/compose/compose_up_test.go similarity index 99% rename from cmd/nerdctl/compose_up_test.go rename to cmd/nerdctl/compose/compose_up_test.go index 1941fef8c1d..63bba829fcf 100644 --- a/cmd/nerdctl/compose_up_test.go +++ b/cmd/nerdctl/compose/compose_up_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_version.go b/cmd/nerdctl/compose/compose_version.go similarity index 99% rename from cmd/nerdctl/compose_version.go rename to cmd/nerdctl/compose/compose_version.go index e847bc1343e..d4a74a4bca0 100644 --- a/cmd/nerdctl/compose_version.go +++ b/cmd/nerdctl/compose/compose_version.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "fmt" diff --git a/cmd/nerdctl/compose_version_test.go b/cmd/nerdctl/compose/compose_version_test.go similarity index 98% rename from cmd/nerdctl/compose_version_test.go rename to cmd/nerdctl/compose/compose_version_test.go index 222d41edd4d..af3028b3d65 100644 --- a/cmd/nerdctl/compose_version_test.go +++ b/cmd/nerdctl/compose/compose_version_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package compose import ( "testing" diff --git a/cmd/nerdctl/container.go b/cmd/nerdctl/container/container.go similarity index 70% rename from cmd/nerdctl/container.go rename to cmd/nerdctl/container/container.go index 9db284f5919..21c7f9b63b1 100644 --- a/cmd/nerdctl/container.go +++ b/cmd/nerdctl/container/container.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newContainerCommand() *cobra.Command { +func NewContainerCommand() *cobra.Command { containerCommand := &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "container", @@ -32,35 +32,35 @@ func newContainerCommand() *cobra.Command { SilenceErrors: true, } containerCommand.AddCommand( - newCreateCommand(), - newRunCommand(), - newUpdateCommand(), - newExecCommand(), + NewCreateCommand(), + NewRunCommand(), + NewUpdateCommand(), + NewExecCommand(), containerLsCommand(), newContainerInspectCommand(), - newLogsCommand(), - newPortCommand(), - newRmCommand(), - newStopCommand(), - newStartCommand(), - newRestartCommand(), - newKillCommand(), - newPauseCommand(), - newDiffCommand(), - newWaitCommand(), - newUnpauseCommand(), - newCommitCommand(), - newRenameCommand(), + NewLogsCommand(), + NewPortCommand(), + NewRmCommand(), + NewStopCommand(), + NewStartCommand(), + NewRestartCommand(), + NewKillCommand(), + NewPauseCommand(), + NewDiffCommand(), + NewWaitCommand(), + NewUnpauseCommand(), + NewCommitCommand(), + NewRenameCommand(), newContainerPruneCommand(), - newStatsCommand(), - newAttachCommand(), + NewStatsCommand(), + NewAttachCommand(), ) - addCpCommand(containerCommand) + AddCpCommand(containerCommand) return containerCommand } func containerLsCommand() *cobra.Command { - x := newPsCommand() + x := NewPsCommand() x.Use = "ls" x.Aliases = []string{"list"} return x diff --git a/cmd/nerdctl/container_attach.go b/cmd/nerdctl/container/container_attach.go similarity index 98% rename from cmd/nerdctl/container_attach.go rename to cmd/nerdctl/container/container_attach.go index 0bcbc9830c8..e92aa44b26d 100644 --- a/cmd/nerdctl/container_attach.go +++ b/cmd/nerdctl/container/container_attach.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -29,7 +29,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/consoleutil" ) -func newAttachCommand() *cobra.Command { +func NewAttachCommand() *cobra.Command { const shortHelp = "Attach stdin, stdout, and stderr to a running container." const longHelp = `Attach stdin, stdout, and stderr to a running container. For example: diff --git a/cmd/nerdctl/container_attach_linux_test.go b/cmd/nerdctl/container/container_attach_linux_test.go similarity index 99% rename from cmd/nerdctl/container_attach_linux_test.go rename to cmd/nerdctl/container/container_attach_linux_test.go index e0b04b87d5d..71a74eae59e 100644 --- a/cmd/nerdctl/container_attach_linux_test.go +++ b/cmd/nerdctl/container/container_attach_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bytes" diff --git a/cmd/nerdctl/container_commit.go b/cmd/nerdctl/container/container_commit.go similarity index 98% rename from cmd/nerdctl/container_commit.go rename to cmd/nerdctl/container/container_commit.go index c6ef5e1243f..e7276499ab3 100644 --- a/cmd/nerdctl/container_commit.go +++ b/cmd/nerdctl/container/container_commit.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newCommitCommand() *cobra.Command { +func NewCommitCommand() *cobra.Command { var commitCommand = &cobra.Command{ Use: "commit [flags] CONTAINER REPOSITORY[:TAG]", Short: "Create a new image from a container's changes", diff --git a/cmd/nerdctl/container_commit_linux_test.go b/cmd/nerdctl/container/container_commit_linux_test.go similarity index 99% rename from cmd/nerdctl/container_commit_linux_test.go rename to cmd/nerdctl/container/container_commit_linux_test.go index a2cc557c4d7..24c791d4f60 100644 --- a/cmd/nerdctl/container_commit_linux_test.go +++ b/cmd/nerdctl/container/container_commit_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "strings" diff --git a/cmd/nerdctl/container_commit_test.go b/cmd/nerdctl/container/container_commit_test.go similarity index 98% rename from cmd/nerdctl/container_commit_test.go rename to cmd/nerdctl/container/container_commit_test.go index b569ebfbcc2..f9f553d9ca1 100644 --- a/cmd/nerdctl/container_commit_test.go +++ b/cmd/nerdctl/container/container_commit_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_cp.go b/cmd/nerdctl/container/container_cp.go similarity index 98% rename from cmd/nerdctl/container_cp.go rename to cmd/nerdctl/container/container_cp.go index a80135b16bd..716fc8fc5c9 100644 --- a/cmd/nerdctl/container_cp.go +++ b/cmd/nerdctl/container/container_cp.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" diff --git a/cmd/nerdctl/main_unix.go b/cmd/nerdctl/container/container_cp_freebsd.go similarity index 85% rename from cmd/nerdctl/main_unix.go rename to cmd/nerdctl/container/container_cp_freebsd.go index e29c3c6075c..4e7d2cfd518 100644 --- a/cmd/nerdctl/main_unix.go +++ b/cmd/nerdctl/container/container_cp_freebsd.go @@ -1,5 +1,3 @@ -//go:build unix - /* Copyright The containerd Authors. @@ -16,4 +14,10 @@ limitations under the License. */ -package main +package container + +import "github.com/spf13/cobra" + +func AddCpCommand(rootCmd *cobra.Command) { + // NOP +} diff --git a/cmd/nerdctl/container_cp_linux.go b/cmd/nerdctl/container/container_cp_linux.go similarity index 97% rename from cmd/nerdctl/container_cp_linux.go rename to cmd/nerdctl/container/container_cp_linux.go index c63d46030b6..53abf9f898b 100644 --- a/cmd/nerdctl/container_cp_linux.go +++ b/cmd/nerdctl/container/container_cp_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" @@ -127,3 +127,7 @@ func processCpOptions(cmd *cobra.Command, args []string) (types.ContainerCpOptio FollowSymLink: flagL, }, nil } + +func AddCpCommand(rootCmd *cobra.Command) { + rootCmd.AddCommand(newCpCommand()) +} diff --git a/cmd/nerdctl/container_cp_linux_test.go b/cmd/nerdctl/container/container_cp_linux_test.go similarity index 99% rename from cmd/nerdctl/container_cp_linux_test.go rename to cmd/nerdctl/container/container_cp_linux_test.go index 33f59ba45d3..a94f46561b2 100644 --- a/cmd/nerdctl/container_cp_linux_test.go +++ b/cmd/nerdctl/container/container_cp_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container/container_cp_windows.go b/cmd/nerdctl/container/container_cp_windows.go new file mode 100644 index 00000000000..4e7d2cfd518 --- /dev/null +++ b/cmd/nerdctl/container/container_cp_windows.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 container + +import "github.com/spf13/cobra" + +func AddCpCommand(rootCmd *cobra.Command) { + // NOP +} diff --git a/cmd/nerdctl/container_create.go b/cmd/nerdctl/container/container_create.go similarity index 99% rename from cmd/nerdctl/container_create.go rename to cmd/nerdctl/container/container_create.go index 304ae65db87..bb1c9e88256 100644 --- a/cmd/nerdctl/container_create.go +++ b/cmd/nerdctl/container/container_create.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" @@ -29,7 +29,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/containerutil" ) -func newCreateCommand() *cobra.Command { +func NewCreateCommand() *cobra.Command { shortHelp := "Create a new container. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS." longHelp := shortHelp switch runtime.GOOS { @@ -67,7 +67,7 @@ func processContainerCreateOptions(cmd *cobra.Command) (types.ContainerCreateOpt return opt, err } - opt.NerdctlCmd, opt.NerdctlArgs = globalFlags(cmd) + opt.NerdctlCmd, opt.NerdctlArgs = helpers.GlobalFlags(cmd) // #region for basic flags // The command `container start` doesn't support the flag `--interactive`. Set the default value of `opt.Interactive` false. diff --git a/cmd/nerdctl/container_create_linux_test.go b/cmd/nerdctl/container/container_create_linux_test.go similarity index 99% rename from cmd/nerdctl/container_create_linux_test.go rename to cmd/nerdctl/container/container_create_linux_test.go index 601326462b2..e3e2122bf80 100644 --- a/cmd/nerdctl/container_create_linux_test.go +++ b/cmd/nerdctl/container/container_create_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_create_windows_test.go b/cmd/nerdctl/container/container_create_windows_test.go similarity index 98% rename from cmd/nerdctl/container_create_windows_test.go rename to cmd/nerdctl/container/container_create_windows_test.go index f52cda49819..d80f5bb6d1d 100644 --- a/cmd/nerdctl/container_create_windows_test.go +++ b/cmd/nerdctl/container/container_create_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_diff.go b/cmd/nerdctl/container/container_diff.go similarity index 99% rename from cmd/nerdctl/container_diff.go rename to cmd/nerdctl/container/container_diff.go index 3e2025ea27e..279c1aa2c79 100644 --- a/cmd/nerdctl/container_diff.go +++ b/cmd/nerdctl/container/container_diff.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "context" @@ -43,7 +43,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/labels" ) -func newDiffCommand() *cobra.Command { +func NewDiffCommand() *cobra.Command { var diffCommand = &cobra.Command{ Use: "diff [CONTAINER]", Short: "Inspect changes to files or directories on a container's filesystem", diff --git a/cmd/nerdctl/container_diff_linux_test.go b/cmd/nerdctl/container/container_diff_linux_test.go similarity index 98% rename from cmd/nerdctl/container_diff_linux_test.go rename to cmd/nerdctl/container/container_diff_linux_test.go index 69041d82e99..7de8c302ed5 100644 --- a/cmd/nerdctl/container_diff_linux_test.go +++ b/cmd/nerdctl/container/container_diff_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_exec.go b/cmd/nerdctl/container/container_exec.go similarity index 98% rename from cmd/nerdctl/container_exec.go rename to cmd/nerdctl/container/container_exec.go index eb559b5515b..4dd596ce54c 100644 --- a/cmd/nerdctl/container_exec.go +++ b/cmd/nerdctl/container/container_exec.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" @@ -32,7 +32,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newExecCommand() *cobra.Command { +func NewExecCommand() *cobra.Command { var execCommand = &cobra.Command{ Use: "exec [flags] CONTAINER COMMAND [ARG...]", Args: cobra.MinimumNArgs(2), diff --git a/cmd/nerdctl/container_exec_linux_test.go b/cmd/nerdctl/container/container_exec_linux_test.go similarity index 99% rename from cmd/nerdctl/container_exec_linux_test.go rename to cmd/nerdctl/container/container_exec_linux_test.go index 38c9a793d49..6e7d5429ede 100644 --- a/cmd/nerdctl/container_exec_linux_test.go +++ b/cmd/nerdctl/container/container_exec_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_exec_test.go b/cmd/nerdctl/container/container_exec_test.go similarity index 99% rename from cmd/nerdctl/container_exec_test.go rename to cmd/nerdctl/container/container_exec_test.go index c952aadb8d2..de58789ab77 100644 --- a/cmd/nerdctl/container_exec_test.go +++ b/cmd/nerdctl/container/container_exec_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" diff --git a/cmd/nerdctl/container_inspect.go b/cmd/nerdctl/container/container_inspect.go similarity index 96% rename from cmd/nerdctl/container_inspect.go rename to cmd/nerdctl/container/container_inspect.go index e0d5ab5e35f..6dae2cbf613 100644 --- a/cmd/nerdctl/container_inspect.go +++ b/cmd/nerdctl/container/container_inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" @@ -57,7 +57,7 @@ var validModeType = map[string]bool{ "dockercompat": true, } -func processContainerInspectOptions(cmd *cobra.Command) (opt types.ContainerInspectOptions, err error) { +func ProcessContainerInspectOptions(cmd *cobra.Command) (opt types.ContainerInspectOptions, err error) { globalOptions, err := helpers.ProcessRootCmdFlags(cmd) if err != nil { return @@ -90,7 +90,7 @@ func processContainerInspectOptions(cmd *cobra.Command) (opt types.ContainerInsp } func containerInspectAction(cmd *cobra.Command, args []string) error { - opt, err := processContainerInspectOptions(cmd) + opt, err := ProcessContainerInspectOptions(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go similarity index 99% rename from cmd/nerdctl/container_inspect_linux_test.go rename to cmd/nerdctl/container/container_inspect_linux_test.go index a1aca0d1a3c..2fadc2b5048 100644 --- a/cmd/nerdctl/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_inspect_windows_test.go b/cmd/nerdctl/container/container_inspect_windows_test.go similarity index 99% rename from cmd/nerdctl/container_inspect_windows_test.go rename to cmd/nerdctl/container/container_inspect_windows_test.go index 16c8ec4de36..8feb7fcd52d 100644 --- a/cmd/nerdctl/container_inspect_windows_test.go +++ b/cmd/nerdctl/container/container_inspect_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_kill.go b/cmd/nerdctl/container/container_kill.go similarity index 97% rename from cmd/nerdctl/container_kill.go rename to cmd/nerdctl/container/container_kill.go index dd50a24a8cd..154cec0e23f 100644 --- a/cmd/nerdctl/container_kill.go +++ b/cmd/nerdctl/container/container_kill.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newKillCommand() *cobra.Command { +func NewKillCommand() *cobra.Command { var killCommand = &cobra.Command{ Use: "kill [flags] CONTAINER [CONTAINER, ...]", Short: "Kill one or more running containers", diff --git a/cmd/nerdctl/container_kill_linux_test.go b/cmd/nerdctl/container/container_kill_linux_test.go similarity index 99% rename from cmd/nerdctl/container_kill_linux_test.go rename to cmd/nerdctl/container/container_kill_linux_test.go index 076d8820498..0e7d3fc3f62 100644 --- a/cmd/nerdctl/container_kill_linux_test.go +++ b/cmd/nerdctl/container/container_kill_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_list.go b/cmd/nerdctl/container/container_list.go similarity index 99% rename from cmd/nerdctl/container_list.go rename to cmd/nerdctl/container/container_list.go index d92de87a5de..c9f0d3bd2bb 100644 --- a/cmd/nerdctl/container_list.go +++ b/cmd/nerdctl/container/container_list.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bytes" @@ -33,7 +33,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/formatter" ) -func newPsCommand() *cobra.Command { +func NewPsCommand() *cobra.Command { var psCommand = &cobra.Command{ Use: "ps", Args: cobra.NoArgs, diff --git a/cmd/nerdctl/container_list_linux_test.go b/cmd/nerdctl/container/container_list_linux_test.go similarity index 99% rename from cmd/nerdctl/container_list_linux_test.go rename to cmd/nerdctl/container/container_list_linux_test.go index a4475e5c0c1..79df967332a 100644 --- a/cmd/nerdctl/container_list_linux_test.go +++ b/cmd/nerdctl/container/container_list_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" diff --git a/cmd/nerdctl/container_list_test.go b/cmd/nerdctl/container/container_list_test.go similarity index 98% rename from cmd/nerdctl/container_list_test.go rename to cmd/nerdctl/container/container_list_test.go index 8b37a999ec0..a5bcd65fec7 100644 --- a/cmd/nerdctl/container_list_test.go +++ b/cmd/nerdctl/container/container_list_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_list_windows_test.go b/cmd/nerdctl/container/container_list_windows_test.go similarity index 99% rename from cmd/nerdctl/container_list_windows_test.go rename to cmd/nerdctl/container/container_list_windows_test.go index 8ad0c7ea5ef..b9559a791b8 100644 --- a/cmd/nerdctl/container_list_windows_test.go +++ b/cmd/nerdctl/container/container_list_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_logs.go b/cmd/nerdctl/container/container_logs.go similarity index 98% rename from cmd/nerdctl/container_logs.go rename to cmd/nerdctl/container/container_logs.go index 9a14d942e6d..910cbeffa03 100644 --- a/cmd/nerdctl/container_logs.go +++ b/cmd/nerdctl/container/container_logs.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" @@ -29,7 +29,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newLogsCommand() *cobra.Command { +func NewLogsCommand() *cobra.Command { const shortUsage = "Fetch the logs of a container. Expected to be used with 'nerdctl run -d'." const longUsage = `Fetch the logs of a container. diff --git a/cmd/nerdctl/container_logs_test.go b/cmd/nerdctl/container/container_logs_test.go similarity index 99% rename from cmd/nerdctl/container_logs_test.go rename to cmd/nerdctl/container/container_logs_test.go index 7f6db3831ab..ec9fbd2ce70 100644 --- a/cmd/nerdctl/container_logs_test.go +++ b/cmd/nerdctl/container/container_logs_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_pause.go b/cmd/nerdctl/container/container_pause.go similarity index 97% rename from cmd/nerdctl/container_pause.go rename to cmd/nerdctl/container/container_pause.go index cf652d51703..338652f8d96 100644 --- a/cmd/nerdctl/container_pause.go +++ b/cmd/nerdctl/container/container_pause.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newPauseCommand() *cobra.Command { +func NewPauseCommand() *cobra.Command { var pauseCommand = &cobra.Command{ Use: "pause [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_port.go b/cmd/nerdctl/container/container_port.go similarity index 98% rename from cmd/nerdctl/container_port.go rename to cmd/nerdctl/container/container_port.go index 8c35bfc75e8..1b618be020b 100644 --- a/cmd/nerdctl/container_port.go +++ b/cmd/nerdctl/container/container_port.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "context" @@ -31,7 +31,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker" ) -func newPortCommand() *cobra.Command { +func NewPortCommand() *cobra.Command { var portCommand = &cobra.Command{ Use: "port [flags] CONTAINER [PRIVATE_PORT[/PROTO]]", Args: cobra.RangeArgs(1, 2), diff --git a/cmd/nerdctl/container_prune.go b/cmd/nerdctl/container/container_prune.go similarity index 99% rename from cmd/nerdctl/container_prune.go rename to cmd/nerdctl/container/container_prune.go index 960542157d5..79ce0d8bb56 100644 --- a/cmd/nerdctl/container_prune.go +++ b/cmd/nerdctl/container/container_prune.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/container_prune_linux_test.go b/cmd/nerdctl/container/container_prune_linux_test.go similarity index 98% rename from cmd/nerdctl/container_prune_linux_test.go rename to cmd/nerdctl/container/container_prune_linux_test.go index 47560af3acb..8912e8fb8dd 100644 --- a/cmd/nerdctl/container_prune_linux_test.go +++ b/cmd/nerdctl/container/container_prune_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_remove.go b/cmd/nerdctl/container/container_remove.go similarity index 97% rename from cmd/nerdctl/container_remove.go rename to cmd/nerdctl/container/container_remove.go index bf2962bd87e..fd50457a179 100644 --- a/cmd/nerdctl/container_remove.go +++ b/cmd/nerdctl/container/container_remove.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newRmCommand() *cobra.Command { +func NewRmCommand() *cobra.Command { var rmCommand = &cobra.Command{ Use: "rm [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_remove_linux_test.go b/cmd/nerdctl/container/container_remove_linux_test.go similarity index 98% rename from cmd/nerdctl/container_remove_linux_test.go rename to cmd/nerdctl/container/container_remove_linux_test.go index 71a060f2d41..6a304b80ebf 100644 --- a/cmd/nerdctl/container_remove_linux_test.go +++ b/cmd/nerdctl/container/container_remove_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_remove_windows_test.go b/cmd/nerdctl/container/container_remove_windows_test.go similarity index 99% rename from cmd/nerdctl/container_remove_windows_test.go rename to cmd/nerdctl/container/container_remove_windows_test.go index 7f113729000..f3b1a73f640 100644 --- a/cmd/nerdctl/container_remove_windows_test.go +++ b/cmd/nerdctl/container/container_remove_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_rename.go b/cmd/nerdctl/container/container_rename.go similarity index 97% rename from cmd/nerdctl/container_rename.go rename to cmd/nerdctl/container/container_rename.go index d70ea215960..46b48aae1ed 100644 --- a/cmd/nerdctl/container_rename.go +++ b/cmd/nerdctl/container/container_rename.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -26,7 +26,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newRenameCommand() *cobra.Command { +func NewRenameCommand() *cobra.Command { var renameCommand = &cobra.Command{ Use: "rename [flags] CONTAINER NEW_NAME", Args: helpers.IsExactArgs(2), diff --git a/cmd/nerdctl/container_rename_linux_test.go b/cmd/nerdctl/container/container_rename_linux_test.go similarity index 99% rename from cmd/nerdctl/container_rename_linux_test.go rename to cmd/nerdctl/container/container_rename_linux_test.go index ae48b82f84f..9018cc429dd 100644 --- a/cmd/nerdctl/container_rename_linux_test.go +++ b/cmd/nerdctl/container/container_rename_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_rename_windows_test.go b/cmd/nerdctl/container/container_rename_windows_test.go similarity index 99% rename from cmd/nerdctl/container_rename_windows_test.go rename to cmd/nerdctl/container/container_rename_windows_test.go index 698f6bf004f..6ebd2796049 100644 --- a/cmd/nerdctl/container_rename_windows_test.go +++ b/cmd/nerdctl/container/container_rename_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_restart.go b/cmd/nerdctl/container/container_restart.go similarity index 97% rename from cmd/nerdctl/container_restart.go rename to cmd/nerdctl/container/container_restart.go index a29ac4dac1f..b1875a77853 100644 --- a/cmd/nerdctl/container_restart.go +++ b/cmd/nerdctl/container/container_restart.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "time" @@ -27,7 +27,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newRestartCommand() *cobra.Command { +func NewRestartCommand() *cobra.Command { var restartCommand = &cobra.Command{ Use: "restart [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_restart_linux_test.go b/cmd/nerdctl/container/container_restart_linux_test.go similarity index 99% rename from cmd/nerdctl/container_restart_linux_test.go rename to cmd/nerdctl/container/container_restart_linux_test.go index dddaef2f40b..cd9f7754473 100644 --- a/cmd/nerdctl/container_restart_linux_test.go +++ b/cmd/nerdctl/container/container_restart_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run.go b/cmd/nerdctl/container/container_run.go similarity index 99% rename from cmd/nerdctl/container_run.go rename to cmd/nerdctl/container/container_run.go index c069dbb1c2d..143cd16d99f 100644 --- a/cmd/nerdctl/container_run.go +++ b/cmd/nerdctl/container/container_run.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" @@ -47,7 +47,7 @@ const ( tiniInitBinary = "tini" ) -func newRunCommand() *cobra.Command { +func NewRunCommand() *cobra.Command { shortHelp := "Run a command in a new container. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS." longHelp := shortHelp switch runtime.GOOS { diff --git a/cmd/nerdctl/container_run_cgroup_linux_test.go b/cmd/nerdctl/container/container_run_cgroup_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_cgroup_linux_test.go rename to cmd/nerdctl/container/container_run_cgroup_linux_test.go index 43c491dce55..a856bb5f732 100644 --- a/cmd/nerdctl/container_run_cgroup_linux_test.go +++ b/cmd/nerdctl/container/container_run_cgroup_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bytes" diff --git a/cmd/nerdctl/container_run_freebsd.go b/cmd/nerdctl/container/container_run_freebsd.go similarity index 98% rename from cmd/nerdctl/container_run_freebsd.go rename to cmd/nerdctl/container/container_run_freebsd.go index e93f88cba9f..fa3c4ec5782 100644 --- a/cmd/nerdctl/container_run_freebsd.go +++ b/cmd/nerdctl/container/container_run_freebsd.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/container_run_gpus_test.go b/cmd/nerdctl/container/container_run_gpus_test.go similarity index 98% rename from cmd/nerdctl/container_run_gpus_test.go rename to cmd/nerdctl/container/container_run_gpus_test.go index fea180b732b..820acd8637e 100644 --- a/cmd/nerdctl/container_run_gpus_test.go +++ b/cmd/nerdctl/container/container_run_gpus_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_run_linux.go b/cmd/nerdctl/container/container_run_linux.go similarity index 98% rename from cmd/nerdctl/container_run_linux.go rename to cmd/nerdctl/container/container_run_linux.go index 92dace222b0..5c188aa3a25 100644 --- a/cmd/nerdctl/container_run_linux.go +++ b/cmd/nerdctl/container/container_run_linux.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "strings" diff --git a/cmd/nerdctl/container_run_linux_test.go b/cmd/nerdctl/container/container_run_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_linux_test.go rename to cmd/nerdctl/container/container_run_linux_test.go index 04b7b851a1d..2d114e93ddb 100644 --- a/cmd/nerdctl/container_run_linux_test.go +++ b/cmd/nerdctl/container/container_run_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bufio" diff --git a/cmd/nerdctl/container_run_log_driver_syslog_test.go b/cmd/nerdctl/container/container_run_log_driver_syslog_test.go similarity index 99% rename from cmd/nerdctl/container_run_log_driver_syslog_test.go rename to cmd/nerdctl/container/container_run_log_driver_syslog_test.go index 7e836c61149..36bf6c046d5 100644 --- a/cmd/nerdctl/container_run_log_driver_syslog_test.go +++ b/cmd/nerdctl/container/container_run_log_driver_syslog_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_mount_linux_test.go b/cmd/nerdctl/container/container_run_mount_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_mount_linux_test.go rename to cmd/nerdctl/container/container_run_mount_linux_test.go index ab382c7cf5f..3f4c1972ee9 100644 --- a/cmd/nerdctl/container_run_mount_linux_test.go +++ b/cmd/nerdctl/container/container_run_mount_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_mount_windows_test.go b/cmd/nerdctl/container/container_run_mount_windows_test.go similarity index 99% rename from cmd/nerdctl/container_run_mount_windows_test.go rename to cmd/nerdctl/container/container_run_mount_windows_test.go index 3ad3f9506f9..b0e6afde18a 100644 --- a/cmd/nerdctl/container_run_mount_windows_test.go +++ b/cmd/nerdctl/container/container_run_mount_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_network.go b/cmd/nerdctl/container/container_run_network.go similarity index 99% rename from cmd/nerdctl/container_run_network.go rename to cmd/nerdctl/container/container_run_network.go index 178d277232a..f731a53aaef 100644 --- a/cmd/nerdctl/container_run_network.go +++ b/cmd/nerdctl/container/container_run_network.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "net" diff --git a/cmd/nerdctl/container_run_network_base_test.go b/cmd/nerdctl/container/container_run_network_base_test.go similarity index 99% rename from cmd/nerdctl/container_run_network_base_test.go rename to cmd/nerdctl/container/container_run_network_base_test.go index e6f5c112ca7..4e75448975b 100644 --- a/cmd/nerdctl/container_run_network_base_test.go +++ b/cmd/nerdctl/container/container_run_network_base_test.go @@ -16,7 +16,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_network_linux_test.go b/cmd/nerdctl/container/container_run_network_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_network_linux_test.go rename to cmd/nerdctl/container/container_run_network_linux_test.go index a9420fcfaca..8a21e54b5a1 100644 --- a/cmd/nerdctl/container_run_network_linux_test.go +++ b/cmd/nerdctl/container/container_run_network_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_network_windows_test.go b/cmd/nerdctl/container/container_run_network_windows_test.go similarity index 99% rename from cmd/nerdctl/container_run_network_windows_test.go rename to cmd/nerdctl/container/container_run_network_windows_test.go index e8c42559ad8..9cd8430e5d3 100644 --- a/cmd/nerdctl/container_run_network_windows_test.go +++ b/cmd/nerdctl/container/container_run_network_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_restart_linux_test.go b/cmd/nerdctl/container/container_run_restart_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_restart_linux_test.go rename to cmd/nerdctl/container/container_run_restart_linux_test.go index 6aeb08613b6..c3411c6aaeb 100644 --- a/cmd/nerdctl/container_run_restart_linux_test.go +++ b/cmd/nerdctl/container/container_run_restart_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_runtime_linux_test.go b/cmd/nerdctl/container/container_run_runtime_linux_test.go similarity index 98% rename from cmd/nerdctl/container_run_runtime_linux_test.go rename to cmd/nerdctl/container/container_run_runtime_linux_test.go index 90c61594009..ea7473f2d20 100644 --- a/cmd/nerdctl/container_run_runtime_linux_test.go +++ b/cmd/nerdctl/container/container_run_runtime_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_run_security_linux_test.go b/cmd/nerdctl/container/container_run_security_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_security_linux_test.go rename to cmd/nerdctl/container/container_run_security_linux_test.go index be1191f1d1e..aa0775bbf9c 100644 --- a/cmd/nerdctl/container_run_security_linux_test.go +++ b/cmd/nerdctl/container/container_run_security_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_soci_linux_test.go b/cmd/nerdctl/container/container_run_soci_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_soci_linux_test.go rename to cmd/nerdctl/container/container_run_soci_linux_test.go index a8085164168..57cf0599525 100644 --- a/cmd/nerdctl/container_run_soci_linux_test.go +++ b/cmd/nerdctl/container/container_run_soci_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "os/exec" diff --git a/cmd/nerdctl/container_run_stargz_linux_test.go b/cmd/nerdctl/container/container_run_stargz_linux_test.go similarity index 98% rename from cmd/nerdctl/container_run_stargz_linux_test.go rename to cmd/nerdctl/container/container_run_stargz_linux_test.go index 2b8a79ad975..70e3b0c7d55 100644 --- a/cmd/nerdctl/container_run_stargz_linux_test.go +++ b/cmd/nerdctl/container/container_run_stargz_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "runtime" diff --git a/cmd/nerdctl/container_run_systemd_linux_test.go b/cmd/nerdctl/container/container_run_systemd_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_systemd_linux_test.go rename to cmd/nerdctl/container/container_run_systemd_linux_test.go index e1757c3950b..065e450873c 100644 --- a/cmd/nerdctl/container_run_systemd_linux_test.go +++ b/cmd/nerdctl/container/container_run_systemd_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_run_test.go b/cmd/nerdctl/container/container_run_test.go similarity index 99% rename from cmd/nerdctl/container_run_test.go rename to cmd/nerdctl/container/container_run_test.go index d1ece634eed..8d64226198c 100644 --- a/cmd/nerdctl/container_run_test.go +++ b/cmd/nerdctl/container/container_run_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bufio" diff --git a/cmd/nerdctl/container_run_user_linux_test.go b/cmd/nerdctl/container/container_run_user_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_user_linux_test.go rename to cmd/nerdctl/container/container_run_user_linux_test.go index 114ba251d00..73d1753a867 100644 --- a/cmd/nerdctl/container_run_user_linux_test.go +++ b/cmd/nerdctl/container/container_run_user_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_run_user_windows_test.go b/cmd/nerdctl/container/container_run_user_windows_test.go similarity index 98% rename from cmd/nerdctl/container_run_user_windows_test.go rename to cmd/nerdctl/container/container_run_user_windows_test.go index d6e45171680..e92dc595598 100644 --- a/cmd/nerdctl/container_run_user_windows_test.go +++ b/cmd/nerdctl/container/container_run_user_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_run_verify_linux_test.go b/cmd/nerdctl/container/container_run_verify_linux_test.go similarity index 99% rename from cmd/nerdctl/container_run_verify_linux_test.go rename to cmd/nerdctl/container/container_run_verify_linux_test.go index bb3556b7da6..7d12342cbb3 100644 --- a/cmd/nerdctl/container_run_verify_linux_test.go +++ b/cmd/nerdctl/container/container_run_verify_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_run_windows.go b/cmd/nerdctl/container/container_run_windows.go similarity index 98% rename from cmd/nerdctl/container_run_windows.go rename to cmd/nerdctl/container/container_run_windows.go index e93f88cba9f..fa3c4ec5782 100644 --- a/cmd/nerdctl/container_run_windows.go +++ b/cmd/nerdctl/container/container_run_windows.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" diff --git a/cmd/nerdctl/container_run_windows_test.go b/cmd/nerdctl/container/container_run_windows_test.go similarity index 99% rename from cmd/nerdctl/container_run_windows_test.go rename to cmd/nerdctl/container/container_run_windows_test.go index d3af8a6534f..c7ec6e72aa1 100644 --- a/cmd/nerdctl/container_run_windows_test.go +++ b/cmd/nerdctl/container/container_run_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bytes" diff --git a/cmd/nerdctl/container_start.go b/cmd/nerdctl/container/container_start.go similarity index 98% rename from cmd/nerdctl/container_start.go rename to cmd/nerdctl/container/container_start.go index 9a2443d0aa2..1bf361b8e6a 100644 --- a/cmd/nerdctl/container_start.go +++ b/cmd/nerdctl/container/container_start.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -29,7 +29,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/consoleutil" ) -func newStartCommand() *cobra.Command { +func NewStartCommand() *cobra.Command { var startCommand = &cobra.Command{ Use: "start [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_start_linux_test.go b/cmd/nerdctl/container/container_start_linux_test.go similarity index 99% rename from cmd/nerdctl/container_start_linux_test.go rename to cmd/nerdctl/container/container_start_linux_test.go index 91df7a1b9cb..4fe6f2d249c 100644 --- a/cmd/nerdctl/container_start_linux_test.go +++ b/cmd/nerdctl/container/container_start_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "bytes" diff --git a/cmd/nerdctl/container_start_test.go b/cmd/nerdctl/container/container_start_test.go similarity index 98% rename from cmd/nerdctl/container_start_test.go rename to cmd/nerdctl/container/container_start_test.go index 374ee77c821..60369433d24 100644 --- a/cmd/nerdctl/container_start_test.go +++ b/cmd/nerdctl/container/container_start_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "runtime" diff --git a/cmd/nerdctl/container_stats.go b/cmd/nerdctl/container/container_stats.go similarity index 98% rename from cmd/nerdctl/container_stats.go rename to cmd/nerdctl/container/container_stats.go index 985ac9cef5b..10b927a750b 100644 --- a/cmd/nerdctl/container_stats.go +++ b/cmd/nerdctl/container/container_stats.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newStatsCommand() *cobra.Command { +func NewStatsCommand() *cobra.Command { var statsCommand = &cobra.Command{ Use: "stats", Short: "Display a live stream of container(s) resource usage statistics.", diff --git a/cmd/nerdctl/container_stats_linux_test.go b/cmd/nerdctl/container/container_stats_linux_test.go similarity index 99% rename from cmd/nerdctl/container_stats_linux_test.go rename to cmd/nerdctl/container/container_stats_linux_test.go index 6cd7ca3aaf8..5ea2017d949 100644 --- a/cmd/nerdctl/container_stats_linux_test.go +++ b/cmd/nerdctl/container/container_stats_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container_stop.go b/cmd/nerdctl/container/container_stop.go similarity index 97% rename from cmd/nerdctl/container_stop.go rename to cmd/nerdctl/container/container_stop.go index d35458d09ca..cbc533b801f 100644 --- a/cmd/nerdctl/container_stop.go +++ b/cmd/nerdctl/container/container_stop.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "time" @@ -30,7 +30,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newStopCommand() *cobra.Command { +func NewStopCommand() *cobra.Command { var stopCommand = &cobra.Command{ Use: "stop [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_stop_linux_test.go b/cmd/nerdctl/container/container_stop_linux_test.go similarity index 99% rename from cmd/nerdctl/container_stop_linux_test.go rename to cmd/nerdctl/container/container_stop_linux_test.go index fedd791fabd..2c0e0bc2bea 100644 --- a/cmd/nerdctl/container_stop_linux_test.go +++ b/cmd/nerdctl/container/container_stop_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/container/container_test.go b/cmd/nerdctl/container/container_test.go new file mode 100644 index 00000000000..dcd08829fcd --- /dev/null +++ b/cmd/nerdctl/container/container_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 container + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +} diff --git a/cmd/nerdctl/container_top.go b/cmd/nerdctl/container/container_top.go similarity index 97% rename from cmd/nerdctl/container_top.go rename to cmd/nerdctl/container/container_top.go index bc22587287c..0f20e45a2c0 100644 --- a/cmd/nerdctl/container_top.go +++ b/cmd/nerdctl/container/container_top.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "errors" @@ -33,7 +33,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) -func newTopCommand() *cobra.Command { +func NewTopCommand() *cobra.Command { var topCommand = &cobra.Command{ Use: "top CONTAINER [ps OPTIONS]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_top_unix_test.go b/cmd/nerdctl/container/container_top_unix_test.go similarity index 98% rename from cmd/nerdctl/container_top_unix_test.go rename to cmd/nerdctl/container/container_top_unix_test.go index 3e22f6f11db..d68d42302ee 100644 --- a/cmd/nerdctl/container_top_unix_test.go +++ b/cmd/nerdctl/container/container_top_unix_test.go @@ -16,7 +16,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_top_windows_test.go b/cmd/nerdctl/container/container_top_windows_test.go similarity index 98% rename from cmd/nerdctl/container_top_windows_test.go rename to cmd/nerdctl/container/container_top_windows_test.go index 93d9616ce83..690e52d50f4 100644 --- a/cmd/nerdctl/container_top_windows_test.go +++ b/cmd/nerdctl/container/container_top_windows_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/container_unpause.go b/cmd/nerdctl/container/container_unpause.go similarity index 97% rename from cmd/nerdctl/container_unpause.go rename to cmd/nerdctl/container/container_unpause.go index aaa72ce1475..8d835c9388e 100644 --- a/cmd/nerdctl/container_unpause.go +++ b/cmd/nerdctl/container/container_unpause.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newUnpauseCommand() *cobra.Command { +func NewUnpauseCommand() *cobra.Command { var unpauseCommand = &cobra.Command{ Use: "unpause [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_update.go b/cmd/nerdctl/container/container_update.go similarity index 99% rename from cmd/nerdctl/container_update.go rename to cmd/nerdctl/container/container_update.go index 3ca9f9a030c..15c8e6526ef 100644 --- a/cmd/nerdctl/container_update.go +++ b/cmd/nerdctl/container/container_update.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "context" @@ -56,7 +56,7 @@ type updateResourceOptions struct { BlkioWeight uint16 } -func newUpdateCommand() *cobra.Command { +func NewUpdateCommand() *cobra.Command { var updateCommand = &cobra.Command{ Use: "update [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_wait.go b/cmd/nerdctl/container/container_wait.go similarity index 97% rename from cmd/nerdctl/container_wait.go rename to cmd/nerdctl/container/container_wait.go index b9af63c37ed..c28168b090d 100644 --- a/cmd/nerdctl/container_wait.go +++ b/cmd/nerdctl/container/container_wait.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "github.com/spf13/cobra" @@ -28,7 +28,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/container" ) -func newWaitCommand() *cobra.Command { +func NewWaitCommand() *cobra.Command { var waitCommand = &cobra.Command{ Use: "wait [flags] CONTAINER [CONTAINER, ...]", Args: cobra.MinimumNArgs(1), diff --git a/cmd/nerdctl/container_wait_test.go b/cmd/nerdctl/container/container_wait_test.go similarity index 98% rename from cmd/nerdctl/container_wait_test.go rename to cmd/nerdctl/container/container_wait_test.go index d2048dd6618..d6a3203480a 100644 --- a/cmd/nerdctl/container_wait_test.go +++ b/cmd/nerdctl/container/container_wait_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "testing" diff --git a/cmd/nerdctl/multi_platform_linux_test.go b/cmd/nerdctl/container/multi_platform_linux_test.go similarity index 99% rename from cmd/nerdctl/multi_platform_linux_test.go rename to cmd/nerdctl/container/multi_platform_linux_test.go index 411df72b308..3acb0e3ef87 100644 --- a/cmd/nerdctl/multi_platform_linux_test.go +++ b/cmd/nerdctl/container/multi_platform_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package container import ( "fmt" diff --git a/cmd/nerdctl/helpers/cobra.go b/cmd/nerdctl/helpers/cobra.go index b41c851bb77..d35030ea8cf 100644 --- a/cmd/nerdctl/helpers/cobra.go +++ b/cmd/nerdctl/helpers/cobra.go @@ -24,6 +24,7 @@ import ( "time" "github.com/spf13/cobra" + "github.com/spf13/pflag" "github.com/containerd/log" ) @@ -138,3 +139,147 @@ func AddDurationFlag(cmd *cobra.Command, name string, aliases []string, value ti } } } + +func GlobalFlags(cmd *cobra.Command) (string, []string) { + args0, err := os.Executable() + if err != nil { + log.L.WithError(err).Warnf("cannot call os.Executable(), assuming the executable to be %q", os.Args[0]) + args0 = os.Args[0] + } + if len(os.Args) < 2 { + return args0, nil + } + + rootCmd := cmd.Root() + flagSet := rootCmd.Flags() + args := []string{} + flagSet.VisitAll(func(f *pflag.Flag) { + key := f.Name + val := f.Value.String() + if f.Changed { + args = append(args, "--"+key+"="+val) + } + }) + return args0, args +} + +// AddPersistentStringArrayFlag is similar to cmd.Flags().StringArray but supports aliases and env var and persistent. +// See https://github.com/spf13/cobra/blob/main/user_guide.md#persistent-flags to learn what is "persistent". +func AddPersistentStringArrayFlag(cmd *cobra.Command, name string, aliases, nonPersistentAliases []string, value []string, env string, usage string) { + if env != "" { + usage = fmt.Sprintf("%s [$%s]", usage, env) + } + if envV, ok := os.LookupEnv(env); ok { + value = []string{envV} + } + aliasesUsage := fmt.Sprintf("Alias of --%s", name) + p := new([]string) + flags := cmd.Flags() + for _, a := range nonPersistentAliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + flags.StringArrayVarP(p, a, a, value, aliasesUsage) + } else { + flags.StringArrayVar(p, a, value, aliasesUsage) + } + } + + persistentFlags := cmd.PersistentFlags() + persistentFlags.StringArrayVar(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 + persistentFlags.StringArrayVarP(p, a, a, value, aliasesUsage) + } else { + persistentFlags.StringArrayVar(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) { + 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 is full set of flag(s) + // flags can redefine alias already used in subcommands + flags := cmd.Flags() + 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) + } + // non-persistent flags are not added to the InheritedFlags, so we should add them manually + f := flags.Lookup(a) + aliasToBeInherited.AddFlag(f) + } + + // localFlags are local to the rootCmd + localFlags := cmd.LocalFlags() + for _, a := range localAliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + localFlags.StringVarP(p, a, a, value, aliasesUsage) + } else { + localFlags.StringVar(p, a, value, aliasesUsage) + } + } + + // persistentFlags cannot redefine alias already used in subcommands + persistentFlags := cmd.PersistentFlags() + persistentFlags.StringVar(p, name, value, usage) + for _, a := range persistentAliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + persistentFlags.StringVarP(p, a, a, value, aliasesUsage) + } else { + persistentFlags.StringVar(p, a, value, aliasesUsage) + } + } +} + +// AddPersistentBoolFlag is similar to AddBoolFlag but persistent. +// See https://github.com/spf13/cobra/blob/main/user_guide.md#persistent-flags to learn what is "persistent". +func AddPersistentBoolFlag(cmd *cobra.Command, name string, aliases, nonPersistentAliases []string, value bool, env, usage string) { + if env != "" { + usage = fmt.Sprintf("%s [$%s]", usage, env) + } + if envV, ok := os.LookupEnv(env); ok { + var err error + value, err = strconv.ParseBool(envV) + if err != nil { + log.L.WithError(err).Warnf("Invalid boolean value for `%s`", env) + } + } + aliasesUsage := fmt.Sprintf("Alias of --%s", name) + p := new(bool) + flags := cmd.Flags() + for _, a := range nonPersistentAliases { + if len(a) == 1 { + // pflag doesn't support short-only flags, so we have to register long one as well here + flags.BoolVarP(p, a, a, value, aliasesUsage) + } else { + flags.BoolVar(p, a, value, aliasesUsage) + } + } + + persistentFlags := cmd.PersistentFlags() + persistentFlags.BoolVar(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 + persistentFlags.BoolVarP(p, a, a, value, aliasesUsage) + } else { + persistentFlags.BoolVar(p, a, value, aliasesUsage) + } + } +} diff --git a/cmd/nerdctl/helpers/testing.go b/cmd/nerdctl/helpers/testing.go index d3197a531a5..a9c633faa13 100644 --- a/cmd/nerdctl/helpers/testing.go +++ b/cmd/nerdctl/helpers/testing.go @@ -17,165 +17,16 @@ package helpers import ( - "context" - "fmt" - "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 { - 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) -} - -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 } - -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 4aed02384bf..ec638021b11 100644 --- a/cmd/nerdctl/helpers/testing_linux.go +++ b/cmd/nerdctl/helpers/testing_linux.go @@ -17,10 +17,12 @@ package helpers import ( + "context" "encoding/json" "errors" "fmt" "io" + "net" "os" "os/exec" "path/filepath" @@ -28,10 +30,154 @@ import ( "testing" "time" + "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" "github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil" ) +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) +} + +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 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) + }, + } +} + +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 ComposeUp(t *testing.T, base *testutil.Base, dockerComposeYAML string, opts ...string) { comp := testutil.NewComposeDir(t, dockerComposeYAML) defer comp.CleanUp() diff --git a/cmd/nerdctl/inspect.go b/cmd/nerdctl/inspect/inspect.go similarity index 96% rename from cmd/nerdctl/inspect.go rename to cmd/nerdctl/inspect/inspect.go index c9cf64c6949..a91624b27f1 100644 --- a/cmd/nerdctl/inspect.go +++ b/cmd/nerdctl/inspect/inspect.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package inspect import ( "context" @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" + containerCmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/container" "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" @@ -33,7 +34,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker" ) -func newInspectCommand() *cobra.Command { +func NewInspectCommand() *cobra.Command { var inspectCommand = &cobra.Command{ Use: "inspect", Short: "Return low-level information on objects.", @@ -122,7 +123,7 @@ func inspectAction(cmd *cobra.Command, args []string) error { } } if inspectContainer { - containerInspectOptions, err = processContainerInspectOptions(cmd) + containerInspectOptions, err = containerCmd.ProcessContainerInspectOptions(cmd) if err != nil { return err } diff --git a/cmd/nerdctl/inspect/inspect_test.go b/cmd/nerdctl/inspect/inspect_test.go new file mode 100644 index 00000000000..18b6d6ffee5 --- /dev/null +++ b/cmd/nerdctl/inspect/inspect_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 inspect + +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 f02d8867b0e..1e086b910d6 100644 --- a/cmd/nerdctl/main.go +++ b/cmd/nerdctl/main.go @@ -21,7 +21,6 @@ import ( "fmt" "os" "runtime" - "strconv" "strings" "github.com/fatih/color" @@ -33,13 +32,17 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/builder" "github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/compose" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/container" "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" "github.com/containerd/nerdctl/v2/cmd/nerdctl/image" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/inspect" "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" "github.com/containerd/nerdctl/v2/cmd/nerdctl/network" + "github.com/containerd/nerdctl/v2/cmd/nerdctl/system" "github.com/containerd/nerdctl/v2/cmd/nerdctl/volume" "github.com/containerd/nerdctl/v2/pkg/config" ncdefaults "github.com/containerd/nerdctl/v2/pkg/defaults" @@ -162,15 +165,15 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet, rootCmd.PersistentFlags().Bool("debug", cfg.Debug, "debug mode") rootCmd.PersistentFlags().Bool("debug-full", cfg.DebugFull, "debug mode (with full output)") // -a is aliases (conflicts with nerdctl images -a) - AddPersistentStringFlag(rootCmd, "address", []string{"a", "H"}, nil, []string{"host"}, aliasToBeInherited, cfg.Address, "CONTAINERD_ADDRESS", `containerd address, optionally with "unix://" prefix`) + helpers.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`) + helpers.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.NamespaceNames) - AddPersistentStringFlag(rootCmd, "snapshotter", nil, nil, []string{"storage-driver"}, aliasToBeInherited, cfg.Snapshotter, "CONTAINERD_SNAPSHOTTER", "containerd snapshotter") + helpers.AddPersistentStringFlag(rootCmd, "snapshotter", nil, nil, []string{"storage-driver"}, aliasToBeInherited, cfg.Snapshotter, "CONTAINERD_SNAPSHOTTER", "containerd snapshotter") 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") + helpers.AddPersistentStringFlag(rootCmd, "cni-path", nil, nil, nil, aliasToBeInherited, cfg.CNIPath, "CNI_PATH", "cni plugins binary directory") + helpers.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.CgroupManagerNames) @@ -178,8 +181,8 @@ func initRootCmdFlags(rootCmd *cobra.Command, tomlPath string) (*pflag.FlagSet, // 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)") // Experimental enable experimental feature, see in https://github.com/containerd/nerdctl/blob/main/docs/experimental.md - AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md") - AddPersistentStringFlag(rootCmd, "host-gateway-ip", nil, nil, nil, aliasToBeInherited, cfg.HostGatewayIP, "NERDCTL_HOST_GATEWAY_IP", "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the host. It has no effect without setting --add-host") + helpers.AddPersistentBoolFlag(rootCmd, "experimental", nil, nil, cfg.Experimental, "NERDCTL_EXPERIMENTAL", "Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md") + helpers.AddPersistentStringFlag(rootCmd, "host-gateway-ip", nil, nil, nil, aliasToBeInherited, cfg.HostGatewayIP, "NERDCTL_HOST_GATEWAY_IP", "IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the host. It has no effect without setting --add-host") return aliasToBeInherited, nil } @@ -243,29 +246,29 @@ Config file ($NERDCTL_TOML): %s } rootCmd.RunE = helpers.UnknownSubcommandAction rootCmd.AddCommand( - newCreateCommand(), + container.NewCreateCommand(), // #region Run & Exec - newRunCommand(), - newUpdateCommand(), - newExecCommand(), + container.NewRunCommand(), + container.NewUpdateCommand(), + container.NewExecCommand(), // #endregion // #region Container management - newPsCommand(), - newLogsCommand(), - newPortCommand(), - newStopCommand(), - newStartCommand(), - newDiffCommand(), - newRestartCommand(), - newKillCommand(), - newRmCommand(), - newPauseCommand(), - newUnpauseCommand(), - newCommitCommand(), - newWaitCommand(), - newRenameCommand(), - newAttachCommand(), + container.NewPsCommand(), + container.NewLogsCommand(), + container.NewPortCommand(), + container.NewStopCommand(), + container.NewStartCommand(), + container.NewDiffCommand(), + container.NewRestartCommand(), + container.NewKillCommand(), + container.NewRmCommand(), + container.NewPauseCommand(), + container.NewUnpauseCommand(), + container.NewCommitCommand(), + container.NewWaitCommand(), + container.NewRenameCommand(), + container.NewAttachCommand(), // #endregion // Build @@ -283,24 +286,24 @@ Config file ($NERDCTL_TOML): %s // #endregion // #region System - newEventsCommand(), - newInfoCommand(), + system.NewEventsCommand(), + system.NewInfoCommand(), newVersionCommand(), // #endregion // Inspect - newInspectCommand(), + inspect.NewInspectCommand(), // stats - newTopCommand(), - newStatsCommand(), + container.NewTopCommand(), + container.NewStatsCommand(), // #region helpers.Management - newContainerCommand(), + container.NewContainerCommand(), image.NewImageCommand(), network.NewNetworkCommand(), volume.NewVolumeCommand(), - newSystemCommand(), + system.NewSystemCommand(), namespace.NewNamespaceCommand(), builder.NewBuilderCommand(), // #endregion @@ -315,13 +318,13 @@ Config file ($NERDCTL_TOML): %s login.NewLogoutCommand(), // Compose - newComposeCommand(), + compose.NewComposeCommand(), // IPFS ipfs.NewIPFSCommand(), ) addApparmorCommand(rootCmd) - addCpCommand(rootCmd) + container.AddCpCommand(rootCmd) // add aliasToBeInherited to subCommand(s) InheritedFlags for _, subCmd := range rootCmd.Commands() { @@ -329,147 +332,3 @@ Config file ($NERDCTL_TOML): %s } return rootCmd, nil } - -func globalFlags(cmd *cobra.Command) (string, []string) { - args0, err := os.Executable() - if err != nil { - log.L.WithError(err).Warnf("cannot call os.Executable(), assuming the executable to be %q", os.Args[0]) - args0 = os.Args[0] - } - if len(os.Args) < 2 { - return args0, nil - } - - rootCmd := cmd.Root() - flagSet := rootCmd.Flags() - args := []string{} - flagSet.VisitAll(func(f *pflag.Flag) { - key := f.Name - val := f.Value.String() - if f.Changed { - args = append(args, "--"+key+"="+val) - } - }) - return args0, args -} - -// 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) { - 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 is full set of flag(s) - // flags can redefine alias already used in subcommands - flags := cmd.Flags() - 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) - } - // non-persistent flags are not added to the InheritedFlags, so we should add them manually - f := flags.Lookup(a) - aliasToBeInherited.AddFlag(f) - } - - // localFlags are local to the rootCmd - localFlags := cmd.LocalFlags() - for _, a := range localAliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - localFlags.StringVarP(p, a, a, value, aliasesUsage) - } else { - localFlags.StringVar(p, a, value, aliasesUsage) - } - } - - // persistentFlags cannot redefine alias already used in subcommands - persistentFlags := cmd.PersistentFlags() - persistentFlags.StringVar(p, name, value, usage) - for _, a := range persistentAliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - persistentFlags.StringVarP(p, a, a, value, aliasesUsage) - } else { - persistentFlags.StringVar(p, a, value, aliasesUsage) - } - } -} - -// AddPersistentBoolFlag is similar to AddBoolFlag but persistent. -// See https://github.com/spf13/cobra/blob/main/user_guide.md#persistent-flags to learn what is "persistent". -func AddPersistentBoolFlag(cmd *cobra.Command, name string, aliases, nonPersistentAliases []string, value bool, env, usage string) { - if env != "" { - usage = fmt.Sprintf("%s [$%s]", usage, env) - } - if envV, ok := os.LookupEnv(env); ok { - var err error - value, err = strconv.ParseBool(envV) - if err != nil { - log.L.WithError(err).Warnf("Invalid boolean value for `%s`", env) - } - } - aliasesUsage := fmt.Sprintf("Alias of --%s", name) - p := new(bool) - flags := cmd.Flags() - for _, a := range nonPersistentAliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - flags.BoolVarP(p, a, a, value, aliasesUsage) - } else { - flags.BoolVar(p, a, value, aliasesUsage) - } - } - - persistentFlags := cmd.PersistentFlags() - persistentFlags.BoolVar(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 - persistentFlags.BoolVarP(p, a, a, value, aliasesUsage) - } else { - persistentFlags.BoolVar(p, a, value, aliasesUsage) - } - } -} - -// AddPersistentStringArrayFlag is similar to cmd.Flags().StringArray but supports aliases and env var and persistent. -// See https://github.com/spf13/cobra/blob/main/user_guide.md#persistent-flags to learn what is "persistent". -func AddPersistentStringArrayFlag(cmd *cobra.Command, name string, aliases, nonPersistentAliases []string, value []string, env string, usage string) { - if env != "" { - usage = fmt.Sprintf("%s [$%s]", usage, env) - } - if envV, ok := os.LookupEnv(env); ok { - value = []string{envV} - } - aliasesUsage := fmt.Sprintf("Alias of --%s", name) - p := new([]string) - flags := cmd.Flags() - for _, a := range nonPersistentAliases { - if len(a) == 1 { - // pflag doesn't support short-only flags, so we have to register long one as well here - flags.StringArrayVarP(p, a, a, value, aliasesUsage) - } else { - flags.StringArrayVar(p, a, value, aliasesUsage) - } - } - - persistentFlags := cmd.PersistentFlags() - persistentFlags.StringArrayVar(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 - persistentFlags.StringArrayVarP(p, a, a, value, aliasesUsage) - } else { - persistentFlags.StringArrayVar(p, a, value, aliasesUsage) - } - } -} diff --git a/cmd/nerdctl/main_freebsd.go b/cmd/nerdctl/main_freebsd.go index 46880c81e87..391d34cfeed 100644 --- a/cmd/nerdctl/main_freebsd.go +++ b/cmd/nerdctl/main_freebsd.go @@ -27,7 +27,3 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { func addApparmorCommand(rootCmd *cobra.Command) { // NOP } - -func addCpCommand(rootCmd *cobra.Command) { - // NOP -} diff --git a/cmd/nerdctl/main_linux.go b/cmd/nerdctl/main_linux.go index d1dfbeaf615..44694688c61 100644 --- a/cmd/nerdctl/main_linux.go +++ b/cmd/nerdctl/main_linux.go @@ -66,7 +66,3 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { func addApparmorCommand(rootCmd *cobra.Command) { rootCmd.AddCommand(apparmor.NewApparmorCommand()) } - -func addCpCommand(rootCmd *cobra.Command) { - rootCmd.AddCommand(newCpCommand()) -} diff --git a/cmd/nerdctl/main_windows.go b/cmd/nerdctl/main_windows.go index 46880c81e87..391d34cfeed 100644 --- a/cmd/nerdctl/main_windows.go +++ b/cmd/nerdctl/main_windows.go @@ -27,7 +27,3 @@ func appNeedsRootlessParentMain(cmd *cobra.Command, args []string) bool { func addApparmorCommand(rootCmd *cobra.Command) { // NOP } - -func addCpCommand(rootCmd *cobra.Command) { - // NOP -} diff --git a/cmd/nerdctl/system.go b/cmd/nerdctl/system/system.go similarity index 91% rename from cmd/nerdctl/system.go rename to cmd/nerdctl/system/system.go index 1119fded332..f4a41aba5ca 100644 --- a/cmd/nerdctl/system.go +++ b/cmd/nerdctl/system/system.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "github.com/spf13/cobra" @@ -22,7 +22,7 @@ import ( "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" ) -func newSystemCommand() *cobra.Command { +func NewSystemCommand() *cobra.Command { var systemCommand = &cobra.Command{ Annotations: map[string]string{helpers.Category: helpers.Management}, Use: "system", @@ -33,8 +33,8 @@ func newSystemCommand() *cobra.Command { } // versionCommand is not here systemCommand.AddCommand( - newEventsCommand(), - newInfoCommand(), + NewEventsCommand(), + NewInfoCommand(), newSystemPruneCommand(), ) return systemCommand diff --git a/cmd/nerdctl/system_events.go b/cmd/nerdctl/system/system_events.go similarity index 97% rename from cmd/nerdctl/system_events.go rename to cmd/nerdctl/system/system_events.go index bc4dcfb57f6..69832df35ce 100644 --- a/cmd/nerdctl/system_events.go +++ b/cmd/nerdctl/system/system_events.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "github.com/spf13/cobra" @@ -25,7 +25,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/system" ) -func newEventsCommand() *cobra.Command { +func NewEventsCommand() *cobra.Command { shortHelp := `Get real time events from the server` longHelp := shortHelp + "\nNOTE: The output format is not compatible with Docker." var eventsCommand = &cobra.Command{ diff --git a/cmd/nerdctl/system_events_linux_test.go b/cmd/nerdctl/system/system_events_linux_test.go similarity index 99% rename from cmd/nerdctl/system_events_linux_test.go rename to cmd/nerdctl/system/system_events_linux_test.go index 28c442fa595..5838e710866 100644 --- a/cmd/nerdctl/system_events_linux_test.go +++ b/cmd/nerdctl/system/system_events_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "fmt" diff --git a/cmd/nerdctl/system_info.go b/cmd/nerdctl/system/system_info.go similarity index 98% rename from cmd/nerdctl/system_info.go rename to cmd/nerdctl/system/system_info.go index 0d461c99a77..10eeed9cd21 100644 --- a/cmd/nerdctl/system_info.go +++ b/cmd/nerdctl/system/system_info.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "github.com/spf13/cobra" @@ -25,7 +25,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/cmd/system" ) -func newInfoCommand() *cobra.Command { +func NewInfoCommand() *cobra.Command { var infoCommand = &cobra.Command{ Use: "info", Args: cobra.NoArgs, diff --git a/cmd/nerdctl/system_info_test.go b/cmd/nerdctl/system/system_info_test.go similarity index 99% rename from cmd/nerdctl/system_info_test.go rename to cmd/nerdctl/system/system_info_test.go index 7d7ec7a7c19..3a10033930d 100644 --- a/cmd/nerdctl/system_info_test.go +++ b/cmd/nerdctl/system/system_info_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "encoding/json" diff --git a/cmd/nerdctl/system_prune.go b/cmd/nerdctl/system/system_prune.go similarity index 99% rename from cmd/nerdctl/system_prune.go rename to cmd/nerdctl/system/system_prune.go index cfe1cb41661..29a43cb187e 100644 --- a/cmd/nerdctl/system_prune.go +++ b/cmd/nerdctl/system/system_prune.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "fmt" diff --git a/cmd/nerdctl/system_prune_linux_test.go b/cmd/nerdctl/system/system_prune_linux_test.go similarity index 99% rename from cmd/nerdctl/system_prune_linux_test.go rename to cmd/nerdctl/system/system_prune_linux_test.go index 3070cdde5fb..d792282398f 100644 --- a/cmd/nerdctl/system_prune_linux_test.go +++ b/cmd/nerdctl/system/system_prune_linux_test.go @@ -14,7 +14,7 @@ limitations under the License. */ -package main +package system import ( "bytes" diff --git a/cmd/nerdctl/system/system_test.go b/cmd/nerdctl/system/system_test.go new file mode 100644 index 00000000000..2c066490dd6 --- /dev/null +++ b/cmd/nerdctl/system/system_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 system + +import ( + "testing" + + "github.com/containerd/nerdctl/v2/pkg/testutil" +) + +func TestMain(m *testing.M) { + testutil.M(m) +}