Skip to content

Commit f2bfc60

Browse files
authored
Merge pull request containerd#3379 from apostasie/refactor-cmd
Refactor cmd/nerdctl
2 parents 3aa8f4f + d5a00f4 commit f2bfc60

File tree

170 files changed

+1544
-961
lines changed

Some content is hidden

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

170 files changed

+1544
-961
lines changed

cmd/nerdctl/apparmor_inspect_linux.go renamed to cmd/nerdctl/apparmor/apparmor_inspect_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package apparmor
1818

1919
import (
2020
"fmt"

cmd/nerdctl/apparmor_linux.go renamed to cmd/nerdctl/apparmor/apparmor_linux.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package apparmor
1818

1919
import (
2020
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2123
)
2224

23-
func newApparmorCommand() *cobra.Command {
25+
func NewApparmorCommand() *cobra.Command {
2426
cmd := &cobra.Command{
25-
Annotations: map[string]string{Category: Management},
27+
Annotations: map[string]string{helpers.Category: helpers.Management},
2628
Use: "apparmor",
2729
Short: "Manage AppArmor profiles",
28-
RunE: unknownSubcommandAction,
30+
RunE: helpers.UnknownSubcommandAction,
2931
SilenceUsage: true,
3032
SilenceErrors: true,
3133
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package apparmor
18+
19+
import (
20+
"testing"
21+
22+
"github.com/containerd/nerdctl/v2/pkg/testutil"
23+
)
24+
25+
func TestMain(m *testing.M) {
26+
testutil.M(m)
27+
}

cmd/nerdctl/apparmor_list_linux.go renamed to cmd/nerdctl/apparmor/apparmor_list_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package apparmor
1818

1919
import (
2020
"github.com/spf13/cobra"

cmd/nerdctl/apparmor_load_linux.go renamed to cmd/nerdctl/apparmor/apparmor_load_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package apparmor
1818

1919
import (
2020
"fmt"

cmd/nerdctl/apparmor_unload_linux.go renamed to cmd/nerdctl/apparmor/apparmor_unload_linux.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package apparmor
1818

1919
import (
2020
"fmt"
2121

2222
"github.com/spf13/cobra"
2323

24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2425
"github.com/containerd/nerdctl/v2/pkg/cmd/apparmor"
2526
"github.com/containerd/nerdctl/v2/pkg/defaults"
2627
)
@@ -47,5 +48,5 @@ func apparmorUnloadAction(cmd *cobra.Command, args []string) error {
4748
}
4849

4950
func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
50-
return shellCompleteApparmorProfiles(cmd)
51+
return completion.ApparmorProfiles(cmd)
5152
}

cmd/nerdctl/builder.go renamed to cmd/nerdctl/builder/builder.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package builder
1818

1919
import (
2020
"fmt"
@@ -25,21 +25,22 @@ import (
2525
"github.com/docker/go-units"
2626
"github.com/spf13/cobra"
2727

28+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2829
"github.com/containerd/nerdctl/v2/pkg/api/types"
2930
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
3031
)
3132

32-
func newBuilderCommand() *cobra.Command {
33+
func NewBuilderCommand() *cobra.Command {
3334
var builderCommand = &cobra.Command{
34-
Annotations: map[string]string{Category: Management},
35+
Annotations: map[string]string{helpers.Category: helpers.Management},
3536
Use: "builder",
3637
Short: "Manage builds",
37-
RunE: unknownSubcommandAction,
38+
RunE: helpers.UnknownSubcommandAction,
3839
SilenceUsage: true,
3940
SilenceErrors: true,
4041
}
4142
builderCommand.AddCommand(
42-
newBuildCommand(),
43+
NewBuildCommand(),
4344
newBuilderPruneCommand(),
4445
newBuilderDebugCommand(),
4546
)
@@ -57,7 +58,7 @@ func newBuilderPruneCommand() *cobra.Command {
5758
SilenceErrors: true,
5859
}
5960

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

6263
buildPruneCommand.Flags().BoolP("all", "a", false, "Remove all unused build cache, not just dangling ones")
6364
buildPruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
@@ -71,23 +72,16 @@ func builderPruneAction(cmd *cobra.Command, _ []string) error {
7172
}
7273

7374
if !options.Force {
74-
var (
75-
confirm string
76-
msg string
77-
)
75+
var msg string
7876

7977
if options.All {
8078
msg = "This will remove all build cache."
8179
} else {
8280
msg = "This will remove any dangling build cache."
8381
}
84-
msg += " Are you sure you want to continue? [y/N] "
8582

86-
fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
87-
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
88-
89-
if strings.ToLower(confirm) != "y" {
90-
return nil
83+
if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed {
84+
return err
9185
}
9286
}
9387

@@ -108,12 +102,12 @@ func builderPruneAction(cmd *cobra.Command, _ []string) error {
108102
}
109103

110104
func processBuilderPruneOptions(cmd *cobra.Command) (types.BuilderPruneOptions, error) {
111-
globalOptions, err := processRootCmdFlags(cmd)
105+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
112106
if err != nil {
113107
return types.BuilderPruneOptions{}, err
114108
}
115109

116-
buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
110+
buildkitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace)
117111
if err != nil {
118112
return types.BuilderPruneOptions{}, err
119113
}
@@ -142,7 +136,7 @@ func newBuilderDebugCommand() *cobra.Command {
142136
var buildDebugCommand = &cobra.Command{
143137
Use: "debug",
144138
Short: shortHelp,
145-
PreRunE: checkExperimental("`nerdctl builder debug`"),
139+
PreRunE: helpers.CheckExperimental("`nerdctl builder debug`"),
146140
RunE: builderDebugAction,
147141
SilenceUsage: true,
148142
SilenceErrors: true,
@@ -157,7 +151,7 @@ func newBuilderDebugCommand() *cobra.Command {
157151
}
158152

159153
func builderDebugAction(cmd *cobra.Command, args []string) error {
160-
globalOptions, err := processRootCmdFlags(cmd)
154+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
161155
if err != nil {
162156
return err
163157
}

cmd/nerdctl/builder_build.go renamed to cmd/nerdctl/builder/builder_build.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package builder
1818

1919
import (
2020
"errors"
@@ -25,14 +25,16 @@ import (
2525

2626
"github.com/spf13/cobra"
2727

28+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
29+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2830
"github.com/containerd/nerdctl/v2/pkg/api/types"
2931
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
3032
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3133
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
3234
"github.com/containerd/nerdctl/v2/pkg/strutil"
3335
)
3436

35-
func newBuildCommand() *cobra.Command {
37+
func NewBuildCommand() *cobra.Command {
3638
var buildCommand = &cobra.Command{
3739
Use: "build [flags] PATH",
3840
Short: "Build an image from a Dockerfile. Needs buildkitd to be running.",
@@ -42,7 +44,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
4244
SilenceUsage: true,
4345
SilenceErrors: true,
4446
}
45-
AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
47+
helpers.AddStringFlag(buildCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
4648
buildCommand.Flags().StringArrayP("tag", "t", nil, "Name and optionally a tag in the 'name:tag' format")
4749
buildCommand.Flags().StringP("file", "f", "", "Name of the Dockerfile")
4850
buildCommand.Flags().String("target", "", "Set the target build stage to build")
@@ -71,7 +73,7 @@ If Dockerfile is not present and -f is not specified, it will look for Container
7173
// #region platform flags
7274
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
7375
buildCommand.Flags().StringSlice("platform", []string{}, "Set target platform for build (e.g., \"amd64\", \"arm64\")")
74-
buildCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
76+
buildCommand.RegisterFlagCompletionFunc("platform", completion.Platforms)
7577
buildCommand.Flags().StringArray("build-context", []string{}, "Additional build contexts (e.g., name=path)")
7678
// #endregion
7779

@@ -82,11 +84,11 @@ If Dockerfile is not present and -f is not specified, it will look for Container
8284
}
8385

8486
func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBuildOptions, error) {
85-
globalOptions, err := processRootCmdFlags(cmd)
87+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
8688
if err != nil {
8789
return types.BuilderBuildOptions{}, err
8890
}
89-
buildKitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
91+
buildKitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace)
9092
if err != nil {
9193
return types.BuilderBuildOptions{}, err
9294
}
@@ -233,7 +235,7 @@ func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBu
233235
}, nil
234236
}
235237

236-
func getBuildkitHost(cmd *cobra.Command, namespace string) (string, error) {
238+
func GetBuildkitHost(cmd *cobra.Command, namespace string) (string, error) {
237239
if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" {
238240
// If address is explicitly specified, use it.
239241
buildkitHost, err := cmd.Flags().GetString("buildkit-host")

cmd/nerdctl/builder_build_linux_test.go renamed to cmd/nerdctl/builder/builder_build_linux_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
limitations under the License.
1515
*/
1616

17-
package main
17+
package builder
1818

1919
import (
2020
"fmt"
2121
"testing"
2222

2323
"gotest.tools/v3/assert"
2424

25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2526
"github.com/containerd/nerdctl/v2/pkg/testutil"
2627
)
2728

@@ -51,7 +52,7 @@ func TestBuildContextWithOCILayout(t *testing.T) {
5152
dockerfile := fmt.Sprintf(`FROM %s
5253
LABEL layer=oci-layout-parent
5354
CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonImage)
54-
buildCtx := createBuildContext(t, dockerfile)
55+
buildCtx := helpers.CreateBuildContext(t, dockerfile)
5556

5657
tarPath := fmt.Sprintf("%s/%s.tar", buildCtx, ociLayout)
5758

@@ -61,12 +62,12 @@ CMD ["echo", "test-nerdctl-build-context-oci-layout-parent"]`, testutil.CommonIm
6162

6263
// Unpack OCI archive into OCI layout directory.
6364
ociLayoutDir := t.TempDir()
64-
err := extractTarFile(ociLayoutDir, tarPath)
65+
err := helpers.ExtractTarFile(ociLayoutDir, tarPath)
6566
assert.NilError(t, err)
6667

6768
dockerfile = fmt.Sprintf(`FROM %s
6869
CMD ["echo", "test-nerdctl-build-context-oci-layout"]`, ociLayout)
69-
buildCtx = createBuildContext(t, dockerfile)
70+
buildCtx = helpers.CreateBuildContext(t, dockerfile)
7071

7172
var buildArgs = []string{}
7273
if testutil.IsDocker() {

0 commit comments

Comments
 (0)