Skip to content

Commit

Permalink
Merge pull request #612 from fahedouch/refacto-cobra-minimumnargs
Browse files Browse the repository at this point in the history
Refacto cobra cmd
  • Loading branch information
AkihiroSuda authored Dec 14, 2021
2 parents e671087 + c73b46e commit e81f1ca
Show file tree
Hide file tree
Showing 29 changed files with 6 additions and 112 deletions.
6 changes: 1 addition & 5 deletions cmd/nerdctl/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -34,6 +33,7 @@ func newCommitCommand() *cobra.Command {
var commitCommand = &cobra.Command{
Use: "commit [flags] CONTAINER REPOSITORY[:TAG]",
Short: "Create a new image from a container's changes",
Args: cobra.ExactArgs(2),
RunE: commitAction,
ValidArgsFunction: commitShellComplete,
SilenceUsage: true,
Expand All @@ -46,10 +46,6 @@ func newCommitCommand() *cobra.Command {
}

func commitAction(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("need container and commit image name")
}

opts, err := newCommitOpts(cmd, args)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions cmd/nerdctl/container_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func newContainerInspectCommand() *cobra.Command {
Use: "inspect [flags] CONTAINER [CONTAINER, ...]",
Short: "Display detailed information on one or more containers.",
Long: "Hint: set `--mode=native` for showing the full output",
Args: cobra.MinimumNArgs(1),
RunE: containerInspectAction,
ValidArgsFunction: containerInspectShellComplete,
SilenceUsage: true,
Expand All @@ -50,10 +51,6 @@ func newContainerInspectCommand() *cobra.Command {
}

func containerInspectAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ type Out struct {

// eventsActions is from https://github.com/containerd/containerd/blob/v1.4.3/cmd/ctr/commands/events/events.go
func eventsAction(cmd *cobra.Command, args []string) error {
if len(args) != 0 {
return fmt.Errorf("accepts no arguments")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ func execAction(cmd *cobra.Command, args []string) error {
newArg = append(newArg, args[2:]...)
args = newArg
}
if len(args) < 2 {
return fmt.Errorf("requires at least 2 arguments")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ func newImageInspectCommand() *cobra.Command {
}

func imageInspectAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

var clientOpts []containerd.ClientOpt
platform, err := cmd.Flags().GetString("platform")
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ func newImagesCommand() *cobra.Command {
func imagesAction(cmd *cobra.Command, args []string) error {
var filters []string

if len(args) > 1 {
return errors.New("cannot have more than one argument")
}

if len(args) > 0 {
canonicalRef, err := referenceutil.ParseAny(args[0])
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/nerdctl/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func newInspectCommand() *cobra.Command {
var inspectCommand = &cobra.Command{
Use: "inspect",
Short: "Return low-level information on objects.",
Args: cobra.MinimumNArgs(1),
RunE: inspectAction,
ValidArgsFunction: inspectShellComplete,
SilenceUsage: true,
Expand All @@ -53,10 +54,6 @@ func addInspectFlags(cmd *cobra.Command) {
}

func inspectAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions cmd/nerdctl/kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func newKillCommand() *cobra.Command {
var killCommand = &cobra.Command{
Use: "kill [flags] CONTAINER [CONTAINER, ...]",
Short: "Kill one or more running containers",
Args: cobra.MinimumNArgs(1),
RunE: killAction,
ValidArgsFunction: killShellComplete,
SilenceUsage: true,
Expand All @@ -59,10 +60,6 @@ func killAction(cmd *cobra.Command, args []string) error {
return err
}

if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ func newLogsCommand() *cobra.Command {
}

func logsAction(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires exactly 1 argument")
}

dataStore, err := getDataStore(cmd)
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func newNetworkCreateCommand() *cobra.Command {
}

func networkCreateAction(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires exactly 1 argument")
}
name := args[0]
if err := identifiers.Validate(name); err != nil {
return fmt.Errorf("malformed name %s: %w", name, err)
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/network_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func newNetworkInspectCommand() *cobra.Command {
}

func networkInspectAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

cniPath, err := cmd.Flags().GetString("cni-path")
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/network_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ func newNetworkRmCommand() *cobra.Command {
}

func networkRmAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}
cniPath, err := cmd.Flags().GetString("cni-path")
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func newPauseCommand() *cobra.Command {
}

func pauseAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ func newPortCommand() *cobra.Command {
}

func portAction(cmd *cobra.Command, args []string) error {
if len(args) != 1 && len(args) != 2 {
return fmt.Errorf("requires at least 1 and at most 2 arguments")
}

argPort := -1
argProto := ""
Expand Down
4 changes: 1 addition & 3 deletions cmd/nerdctl/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newPullCommand() *cobra.Command {
var pullCommand = &cobra.Command{
Use: "pull",
Short: "Pull an image from a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to pull image from IPFS.",
Args: cobra.ExactArgs(1),
RunE: pullAction,
SilenceUsage: true,
SilenceErrors: true,
Expand Down Expand Up @@ -70,9 +71,6 @@ func newPullCommand() *cobra.Command {
}

func pullAction(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("image name needs to be specified")
}
rawRef := args[0]
client, ctx, cancel, err := newClient(cmd)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions cmd/nerdctl/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -49,6 +48,7 @@ func newPushCommand() *cobra.Command {
var pushCommand = &cobra.Command{
Use: "push NAME[:TAG]",
Short: "Push an image or a repository to a registry. Optionally specify \"ipfs://\" or \"ipns://\" scheme to push image to IPFS.",
Args: cobra.ExactArgs(1),
RunE: pushAction,
ValidArgsFunction: pushShellComplete,
SilenceUsage: true,
Expand Down Expand Up @@ -76,9 +76,6 @@ func newPushCommand() *cobra.Command {
}

func pushAction(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("image name needs to be specified")
}
rawRef := args[0]

client, ctx, cancel, err := newClient(cmd)
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ func newRmCommand() *cobra.Command {
}

func rmAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ func newRmiCommand() *cobra.Command {
}

func rmiAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ func runAction(cmd *cobra.Command, args []string) error {
args = newArg
}

if len(args) < 1 {
return errors.New("image name needs to be specified")
}

ns, err := cmd.Flags().GetString("namespace")
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func newSaveCommand() *cobra.Command {
}

func saveAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

var (
images = args
saveOpts = []archive.ExportOpt{}
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ func newStartCommand() *cobra.Command {
}

func startAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ func stopAction(cmd *cobra.Command, args []string) error {
}
timeoutStr = timeoutStr + "s"

if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

timeout, err := time.ParseDuration(timeoutStr)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func newTagCommand() *cobra.Command {
}

func tagAction(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("requires exactly 2 arguments")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions cmd/nerdctl/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ func newTopCommand() *cobra.Command {
}

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

if len(args) < 1 {
return fmt.Errorf("requires at least 1 argument")
}

// NOTE: rootless container does not rely on cgroupv1.
// more details about possible ways to resolve this concern: #223
if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" {
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/unpause.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func newUnpauseCommand() *cobra.Command {
}

func unpauseAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/volume_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ func newVolumeCreateCommand() *cobra.Command {
}

func volumeCreateAction(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("requires exactly 1 argument")
}
name := args[0]
if err := identifiers.Validate(name); err != nil {
return fmt.Errorf("malformed name %s: %w", name, err)
Expand Down
5 changes: 0 additions & 5 deletions cmd/nerdctl/volume_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package main

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -40,9 +38,6 @@ func newVolumeInspectCommand() *cobra.Command {
}

func volumeInspectAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

volStore, err := getVolumeStore(cmd)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions cmd/nerdctl/volume_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ func newVolumeRmCommand() *cobra.Command {
}

func volumeRmAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}
volStore, err := getVolumeStore(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/nerdctl/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ func newWaitCommand() *cobra.Command {
}

func containerWaitAction(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("requires at least 1 argument")
}

client, ctx, cancel, err := newClient(cmd)
if err != nil {
return err
Expand Down

0 comments on commit e81f1ca

Please sign in to comment.