Skip to content

Commit

Permalink
Merge pull request #3379 from apostasie/refactor-cmd
Browse files Browse the repository at this point in the history
Refactor cmd/nerdctl
  • Loading branch information
AkihiroSuda committed Sep 2, 2024
2 parents 3aa8f4f + d5a00f4 commit f2bfc60
Show file tree
Hide file tree
Showing 170 changed files with 1,544 additions and 961 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
limitations under the License.
*/

package main
package apparmor

import (
"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
)

func newApparmorCommand() *cobra.Command {
func NewApparmorCommand() *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{Category: Management},
Annotations: map[string]string{helpers.Category: helpers.Management},
Use: "apparmor",
Short: "Manage AppArmor profiles",
RunE: unknownSubcommandAction,
RunE: helpers.UnknownSubcommandAction,
SilenceUsage: true,
SilenceErrors: true,
}
Expand Down
27 changes: 27 additions & 0 deletions cmd/nerdctl/apparmor/apparmor_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apparmor

import (
"testing"

"github.com/containerd/nerdctl/v2/pkg/testutil"
)

func TestMain(m *testing.M) {
testutil.M(m)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"github.com/spf13/cobra"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
limitations under the License.
*/

package main
package apparmor

import (
"fmt"

"github.com/spf13/cobra"

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

func apparmorUnloadShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return shellCompleteApparmorProfiles(cmd)
return completion.ApparmorProfiles(cmd)
}
34 changes: 14 additions & 20 deletions cmd/nerdctl/builder.go → cmd/nerdctl/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package builder

import (
"fmt"
Expand All @@ -25,21 +25,22 @@ import (
"github.com/docker/go-units"
"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
)

func newBuilderCommand() *cobra.Command {
func NewBuilderCommand() *cobra.Command {
var builderCommand = &cobra.Command{
Annotations: map[string]string{Category: Management},
Annotations: map[string]string{helpers.Category: helpers.Management},
Use: "builder",
Short: "Manage builds",
RunE: unknownSubcommandAction,
RunE: helpers.UnknownSubcommandAction,
SilenceUsage: true,
SilenceErrors: true,
}
builderCommand.AddCommand(
newBuildCommand(),
NewBuildCommand(),
newBuilderPruneCommand(),
newBuilderDebugCommand(),
)
Expand All @@ -57,7 +58,7 @@ func newBuilderPruneCommand() *cobra.Command {
SilenceErrors: true,
}

AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")
helpers.AddStringFlag(buildPruneCommand, "buildkit-host", nil, "", "BUILDKIT_HOST", "BuildKit address")

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

if !options.Force {
var (
confirm string
msg string
)
var msg string

if options.All {
msg = "This will remove all build cache."
} else {
msg = "This will remove any dangling build cache."
}
msg += " Are you sure you want to continue? [y/N] "

fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)

if strings.ToLower(confirm) != "y" {
return nil
if confirmed, err := helpers.Confirm(cmd, fmt.Sprintf("WARNING! %s.", msg)); err != nil || !confirmed {
return err
}
}

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

func processBuilderPruneOptions(cmd *cobra.Command) (types.BuilderPruneOptions, error) {
globalOptions, err := processRootCmdFlags(cmd)
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return types.BuilderPruneOptions{}, err
}

buildkitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
buildkitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace)
if err != nil {
return types.BuilderPruneOptions{}, err
}
Expand Down Expand Up @@ -142,7 +136,7 @@ func newBuilderDebugCommand() *cobra.Command {
var buildDebugCommand = &cobra.Command{
Use: "debug",
Short: shortHelp,
PreRunE: checkExperimental("`nerdctl builder debug`"),
PreRunE: helpers.CheckExperimental("`nerdctl builder debug`"),
RunE: builderDebugAction,
SilenceUsage: true,
SilenceErrors: true,
Expand All @@ -157,7 +151,7 @@ func newBuilderDebugCommand() *cobra.Command {
}

func builderDebugAction(cmd *cobra.Command, args []string) error {
globalOptions, err := processRootCmdFlags(cmd)
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

package main
package builder

import (
"errors"
Expand All @@ -25,14 +25,16 @@ import (

"github.com/spf13/cobra"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/api/types"
"github.com/containerd/nerdctl/v2/pkg/buildkitutil"
"github.com/containerd/nerdctl/v2/pkg/clientutil"
"github.com/containerd/nerdctl/v2/pkg/cmd/builder"
"github.com/containerd/nerdctl/v2/pkg/strutil"
)

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

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

func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBuildOptions, error) {
globalOptions, err := processRootCmdFlags(cmd)
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
if err != nil {
return types.BuilderBuildOptions{}, err
}
buildKitHost, err := getBuildkitHost(cmd, globalOptions.Namespace)
buildKitHost, err := GetBuildkitHost(cmd, globalOptions.Namespace)
if err != nil {
return types.BuilderBuildOptions{}, err
}
Expand Down Expand Up @@ -233,7 +235,7 @@ func processBuildCommandFlag(cmd *cobra.Command, args []string) (types.BuilderBu
}, nil
}

func getBuildkitHost(cmd *cobra.Command, namespace string) (string, error) {
func GetBuildkitHost(cmd *cobra.Command, namespace string) (string, error) {
if cmd.Flags().Changed("buildkit-host") || os.Getenv("BUILDKIT_HOST") != "" {
// If address is explicitly specified, use it.
buildkitHost, err := cmd.Flags().GetString("buildkit-host")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
limitations under the License.
*/

package main
package builder

import (
"fmt"
"testing"

"gotest.tools/v3/assert"

"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
"github.com/containerd/nerdctl/v2/pkg/testutil"
)

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

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

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

// Unpack OCI archive into OCI layout directory.
ociLayoutDir := t.TempDir()
err := extractTarFile(ociLayoutDir, tarPath)
err := helpers.ExtractTarFile(ociLayoutDir, tarPath)
assert.NilError(t, err)

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

var buildArgs = []string{}
if testutil.IsDocker() {
Expand Down
Loading

0 comments on commit f2bfc60

Please sign in to comment.