V2 enable ps tests

* Combine cobra.Command helper functions into validate package
  from registry and common packages
* Introduce ChoiceValue for flags

Signed-off-by: Jhon Honce <jhonce@redhat.com>
This commit is contained in:
Jhon Honce
2020-04-29 15:44:44 -07:00
parent 99f8cfc2dc
commit 22d5b2e305
33 changed files with 156 additions and 89 deletions

View File

@ -528,7 +528,7 @@ func FillOutSpecGen(s *specgen.SpecGenerator, c *ContainerCLIOpts, args []string
// Only add read-only tmpfs mounts in case that we are read-only and the // Only add read-only tmpfs mounts in case that we are read-only and the
// read-only tmpfs flag has been set. // read-only tmpfs flag has been set.
mounts, volumes, err := parseVolumes(c.Volume, c.Mount, c.TmpFS, (c.ReadOnlyTmpFS && c.ReadOnly)) mounts, volumes, err := parseVolumes(c.Volume, c.Mount, c.TmpFS, c.ReadOnlyTmpFS && c.ReadOnly)
if err != nil { if err != nil {
return err return err
} }

View File

@ -1,11 +1,8 @@
package common package common
import ( import (
"fmt"
"strconv" "strconv"
"github.com/spf13/cobra"
"github.com/cri-o/ocicni/pkg/ocicni" "github.com/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -44,11 +41,3 @@ func createPortBindings(ports []string) ([]ocicni.PortMapping, error) {
} }
return portBindings, nil return portBindings, nil
} }
// NoArgs returns an error if any args are included.
func NoArgs(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("`%s` takes no arguments", cmd.CommandPath())
}
return nil
}

View File

@ -2,6 +2,7 @@ package containers
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/util" "github.com/containers/libpod/pkg/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -17,7 +18,7 @@ var (
Short: "Manage containers", Short: "Manage containers",
Long: "Manage containers", Long: "Manage containers",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
containerConfig = util.DefaultContainerConfig() containerConfig = util.DefaultContainerConfig()

View File

@ -3,6 +3,7 @@ package containers
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/report" "github.com/containers/libpod/cmd/podman/report"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -12,7 +13,7 @@ var (
// podman container _diff_ // podman container _diff_
diffCmd = &cobra.Command{ diffCmd = &cobra.Command{
Use: "diff [flags] CONTAINER", Use: "diff [flags] CONTAINER",
Args: registry.IdOrLatestArgs, Args: validate.IdOrLatestArgs,
Short: "Inspect changes on container's file systems", Short: "Inspect changes on container's file systems",
Long: `Displays changes on a container filesystem. The container will be compared to its parent layer.`, Long: `Displays changes on a container filesystem. The container will be compared to its parent layer.`,
RunE: diff, RunE: diff,

View File

@ -1,8 +1,8 @@
package containers package containers
import ( import (
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -12,7 +12,7 @@ var (
listCmd = &cobra.Command{ listCmd = &cobra.Command{
Use: "list", Use: "list",
Aliases: []string{"ls"}, Aliases: []string{"ls"},
Args: common.NoArgs, Args: validate.NoArgs,
Short: "List containers", Short: "List containers",
Long: "Prints out information about the containers", Long: "Prints out information about the containers",
RunE: ps, RunE: ps,

View File

@ -12,8 +12,8 @@ import (
tm "github.com/buger/goterm" tm "github.com/buger/goterm"
"github.com/containers/buildah/pkg/formats" "github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/cri-o/ocicni/pkg/ocicni" "github.com/cri-o/ocicni/pkg/ocicni"
"github.com/docker/go-units" "github.com/docker/go-units"
@ -26,7 +26,7 @@ var (
psDescription = "Prints out information about the containers" psDescription = "Prints out information about the containers"
psCommand = &cobra.Command{ psCommand = &cobra.Command{
Use: "ps", Use: "ps",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "List containers", Short: "List containers",
Long: psDescription, Long: psDescription,
RunE: ps, RunE: ps,
@ -41,7 +41,7 @@ var (
} }
filters []string filters []string
noTrunc bool noTrunc bool
defaultHeaders string = "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES" defaultHeaders = "CONTAINER ID\tIMAGE\tCOMMAND\tCREATED\tSTATUS\tPORTS\tNAMES"
) )
func init() { func init() {
@ -64,9 +64,12 @@ func listFlagSet(flags *pflag.FlagSet) {
flags.BoolVarP(&listOpts.Pod, "pod", "p", false, "Print the ID and name of the pod the containers are associated with") flags.BoolVarP(&listOpts.Pod, "pod", "p", false, "Print the ID and name of the pod the containers are associated with")
flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Print the numeric IDs of the containers only") flags.BoolVarP(&listOpts.Quiet, "quiet", "q", false, "Print the numeric IDs of the containers only")
flags.BoolVarP(&listOpts.Size, "size", "s", false, "Display the total file sizes") flags.BoolVarP(&listOpts.Size, "size", "s", false, "Display the total file sizes")
flags.StringVar(&listOpts.Sort, "sort", "created", "Sort output by command, created, id, image, names, runningfor, size, or status")
flags.BoolVar(&listOpts.Sync, "sync", false, "Sync container state with OCI runtime") flags.BoolVar(&listOpts.Sync, "sync", false, "Sync container state with OCI runtime")
flags.UintVarP(&listOpts.Watch, "watch", "w", 0, "Watch the ps output on an interval in seconds") flags.UintVarP(&listOpts.Watch, "watch", "w", 0, "Watch the ps output on an interval in seconds")
created := validate.ChoiceValue(&listOpts.Sort, "command", "created", "id", "image", "names", "runningfor", "size", "status")
flags.Var(created, "sort", "Sort output by: "+created.Choices())
if registry.IsRemote() { if registry.IsRemote() {
_ = flags.MarkHidden("latest") _ = flags.MarkHidden("latest")
} }
@ -175,7 +178,7 @@ func ps(cmd *cobra.Command, args []string) error {
headers, format := createPsOut() headers, format := createPsOut()
if cmd.Flag("format").Changed { if cmd.Flag("format").Changed {
format = listOpts.Format format = strings.TrimPrefix(listOpts.Format, "table ")
if !strings.HasPrefix(format, "\n") { if !strings.HasPrefix(format, "\n") {
format += "\n" format += "\n"
} }

View File

@ -7,6 +7,7 @@ import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/utils" "github.com/containers/libpod/cmd/podman/utils"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -22,7 +23,7 @@ var (
Short: "Block on one or more containers", Short: "Block on one or more containers",
Long: waitDescription, Long: waitDescription,
RunE: wait, RunE: wait,
Args: registry.IdOrLatestArgs, Args: validate.IdOrLatestArgs,
Example: `podman wait --latest Example: `podman wait --latest
podman wait --interval 5000 ctrID podman wait --interval 5000 ctrID
podman wait ctrID1 ctrID2`, podman wait ctrID1 ctrID2`,

View File

@ -6,6 +6,7 @@ import (
"github.com/containers/libpod/cmd/podman/containers" "github.com/containers/libpod/cmd/podman/containers"
"github.com/containers/libpod/cmd/podman/images" "github.com/containers/libpod/cmd/podman/images"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -17,7 +18,7 @@ var (
diffDescription = `Displays changes on a container or image's filesystem. The container or image will be compared to its parent layer.` diffDescription = `Displays changes on a container or image's filesystem. The container or image will be compared to its parent layer.`
diffCmd = &cobra.Command{ diffCmd = &cobra.Command{
Use: "diff [flags] {CONTAINER_ID | IMAGE_ID}", Use: "diff [flags] {CONTAINER_ID | IMAGE_ID}",
Args: registry.IdOrLatestArgs, Args: validate.IdOrLatestArgs,
Short: "Display the changes of object's file system", Short: "Display the changes of object's file system",
Long: diffDescription, Long: diffDescription,
TraverseChildren: true, TraverseChildren: true,

View File

@ -2,6 +2,7 @@ package pods
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/util" "github.com/containers/libpod/pkg/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -14,7 +15,7 @@ var (
Short: "Generate structured data based on containers and pods.", Short: "Generate structured data based on containers and pods.",
Long: "Generate structured data (e.g., Kubernetes yaml or systemd units) based on containers and pods.", Long: "Generate structured data (e.g., Kubernetes yaml or systemd units) based on containers and pods.",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
containerConfig = util.DefaultContainerConfig() containerConfig = util.DefaultContainerConfig()
) )

View File

@ -2,6 +2,7 @@ package healthcheck
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -13,7 +14,7 @@ var (
Short: "Manage Healthcheck", Short: "Manage Healthcheck",
Long: "Manage Healthcheck", Long: "Manage Healthcheck",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
) )

View File

@ -2,6 +2,7 @@ package images
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,7 +17,7 @@ var (
Short: "Manage images", Short: "Manage images",
Long: "Manage images", Long: "Manage images",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
) )

View File

@ -6,8 +6,8 @@ import (
"os" "os"
"strings" "strings"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -19,7 +19,7 @@ var (
If an image is not being used by a container, it will be removed from the system.` If an image is not being used by a container, it will be removed from the system.`
pruneCmd = &cobra.Command{ pruneCmd = &cobra.Command{
Use: "prune", Use: "prune",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "Remove unused images", Short: "Remove unused images",
Long: pruneDescription, Long: pruneDescription,
RunE: prune, RunE: prune,

View File

@ -2,6 +2,7 @@ package manifest
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -13,7 +14,7 @@ var (
Short: "Manipulate manifest lists and image indexes", Short: "Manipulate manifest lists and image indexes",
Long: manifestDescription, Long: manifestDescription,
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
Example: `podman manifest create localhost/list Example: `podman manifest create localhost/list
podman manifest inspect localhost/list`, podman manifest inspect localhost/list`,
} }

View File

@ -2,6 +2,7 @@ package images
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -13,7 +14,7 @@ var (
Short: "Manage networks", Short: "Manage networks",
Long: "Manage networks", Long: "Manage networks",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
) )

View File

@ -9,6 +9,7 @@ import (
"github.com/containers/libpod/cmd/podman/common" "github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/parse" "github.com/containers/libpod/cmd/podman/parse"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/errorhandling" "github.com/containers/libpod/pkg/errorhandling"
"github.com/containers/libpod/pkg/specgen" "github.com/containers/libpod/pkg/specgen"
@ -24,7 +25,7 @@ var (
createCommand = &cobra.Command{ createCommand = &cobra.Command{
Use: "create", Use: "create",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "Create a new empty pod", Short: "Create a new empty pod",
Long: podCreateDescription, Long: podCreateDescription,
RunE: create, RunE: create,

View File

@ -2,6 +2,7 @@ package pods
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/util" "github.com/containers/libpod/pkg/util"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -17,7 +18,7 @@ var (
Short: "Manage pods", Short: "Manage pods",
Long: "Manage pods", Long: "Manage pods",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
containerConfig = util.DefaultContainerConfig() containerConfig = util.DefaultContainerConfig()
) )

View File

@ -11,8 +11,8 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/docker/go-units" "github.com/docker/go-units"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -29,12 +29,12 @@ var (
Short: "list pods", Short: "list pods",
Long: psDescription, Long: psDescription,
RunE: pods, RunE: pods,
Args: common.NoArgs, Args: validate.NoArgs,
} }
) )
var ( var (
defaultHeaders string = "POD ID\tNAME\tSTATUS\tCREATED" defaultHeaders = "POD ID\tNAME\tSTATUS\tCREATED"
inputFilters []string inputFilters []string
noTrunc bool noTrunc bool
psInput entities.PodPSOptions psInput entities.PodPSOptions

View File

@ -2,11 +2,9 @@ package registry
import ( import (
"context" "context"
"fmt"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/domain/infra" "github.com/containers/libpod/pkg/domain/infra"
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -77,21 +75,6 @@ func NewContainerEngine(cmd *cobra.Command, args []string) (entities.ContainerEn
return containerEngine, nil return containerEngine, nil
} }
func SubCommandExists(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.Errorf("unrecognized command `%[1]s %[2]s`\nTry '%[1]s --help' for more information.", cmd.CommandPath(), args[0])
}
return errors.Errorf("missing command '%[1]s COMMAND'\nTry '%[1]s --help' for more information.", cmd.CommandPath())
}
// IdOrLatestArgs used to validate a nameOrId was provided or the "--latest" flag
func IdOrLatestArgs(cmd *cobra.Command, args []string) error {
if len(args) > 1 || (len(args) == 0 && !cmd.Flag("latest").Changed) {
return fmt.Errorf("%s requires a name, id or the '--latest' flag", cmd.Name())
}
return nil
}
type PodmanOptionsKey struct{} type PodmanOptionsKey struct{}
func Context() context.Context { func Context() context.Context {

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/rootless" "github.com/containers/libpod/pkg/rootless"
"github.com/containers/libpod/pkg/tracing" "github.com/containers/libpod/pkg/tracing"
@ -60,7 +61,7 @@ var (
SilenceErrors: true, SilenceErrors: true,
TraverseChildren: true, TraverseChildren: true,
PersistentPreRunE: persistentPreRunE, PersistentPreRunE: persistentPreRunE,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
PersistentPostRunE: persistentPostRunE, PersistentPostRunE: persistentPostRunE,
Version: version.Version, Version: version.Version,
} }

View File

@ -7,8 +7,8 @@ import (
"os" "os"
"github.com/containers/buildah/pkg/formats" "github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/libpod/events" "github.com/containers/libpod/libpod/events"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -19,7 +19,7 @@ var (
eventsDescription = "Monitor podman events" eventsDescription = "Monitor podman events"
eventsCommand = &cobra.Command{ eventsCommand = &cobra.Command{
Use: "events", Use: "events",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "Show podman events", Short: "Show podman events",
Long: eventsDescription, Long: eventsDescription,
RunE: eventsCmd, RunE: eventsCmd,

View File

@ -5,8 +5,8 @@ import (
"os" "os"
"text/template" "text/template"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -19,7 +19,7 @@ var (
` `
infoCommand = &cobra.Command{ infoCommand = &cobra.Command{
Use: "info", Use: "info",
Args: common.NoArgs, Args: validate.NoArgs,
Long: infoDescription, Long: infoDescription,
Short: "Display podman system information", Short: "Display podman system information",
RunE: info, RunE: info,

View File

@ -2,6 +2,7 @@ package system
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,7 +17,7 @@ var (
Short: "Manage podman", Short: "Manage podman",
Long: "Manage podman", Long: "Manage podman",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
) )

View File

@ -9,8 +9,8 @@ import (
"time" "time"
"github.com/containers/buildah/pkg/formats" "github.com/containers/buildah/pkg/formats"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/define"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -20,7 +20,7 @@ import (
var ( var (
versionCommand = &cobra.Command{ versionCommand = &cobra.Command{
Use: "version", Use: "version",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "Display the Podman Version Information", Short: "Display the Podman Version Information",
RunE: version, RunE: version,
Annotations: map[string]string{ Annotations: map[string]string{

View File

@ -2,7 +2,7 @@ package utils
import "github.com/spf13/pflag" import "github.com/spf13/pflag"
// AliasFlags is a function to handle backwards compatability with old flags // AliasFlags is a function to handle backwards compatibility with old flags
func AliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName { func AliasFlags(f *pflag.FlagSet, name string) pflag.NormalizedName {
switch name { switch name {
case "healthcheck-command": case "healthcheck-command":

View File

@ -0,0 +1,32 @@
package validate
import (
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// NoArgs returns an error if any args are included.
func NoArgs(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("`%s` takes no arguments", cmd.CommandPath())
}
return nil
}
// SubCommandExists returns an error if no sub command is provided
func SubCommandExists(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return errors.Errorf("unrecognized command `%[1]s %[2]s`\nTry '%[1]s --help' for more information.", cmd.CommandPath(), args[0])
}
return errors.Errorf("missing command '%[1]s COMMAND'\nTry '%[1]s --help' for more information.", cmd.CommandPath())
}
// IdOrLatestArgs used to validate a nameOrId was provided or the "--latest" flag
func IdOrLatestArgs(cmd *cobra.Command, args []string) error {
if len(args) > 1 || (len(args) == 0 && !cmd.Flag("latest").Changed) {
return fmt.Errorf("`%s` requires a name, id or the \"--latest\" flag", cmd.CommandPath())
}
return nil
}

View File

@ -0,0 +1,46 @@
package validate
import (
"fmt"
"strings"
)
// Honors cobra.Value interface
type choiceValue struct {
value *string
choices []string
}
// ChoiceValue may be used in cobra FlagSet methods Var/VarP/VarPF() to select from a set of values
//
// Example:
// created := validate.ChoiceValue(&opts.Sort, "command", "created", "id", "image", "names", "runningfor", "size", "status")
// flags.Var(created, "sort", "Sort output by: "+created.Choices())
func ChoiceValue(p *string, choices ...string) *choiceValue {
return &choiceValue{
value: p,
choices: choices,
}
}
func (c *choiceValue) String() string {
return *c.value
}
func (c *choiceValue) Set(value string) error {
for _, v := range c.choices {
if v == value {
*c.value = value
return nil
}
}
return fmt.Errorf("%q is not a valid value. Choose from: %q", value, c.Choices())
}
func (c *choiceValue) Choices() string {
return strings.Join(c.choices, ", ")
}
func (c *choiceValue) Type() string {
return "choice"
}

View File

@ -9,8 +9,8 @@ import (
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -25,7 +25,7 @@ and the output format can be changed to JSON or a user specified Go template.`
lsCommand = &cobra.Command{ lsCommand = &cobra.Command{
Use: "ls", Use: "ls",
Aliases: []string{"list"}, Aliases: []string{"list"},
Args: common.NoArgs, Args: validate.NoArgs,
Short: "List volumes", Short: "List volumes",
Long: volumeLsDescription, Long: volumeLsDescription,
RunE: list, RunE: list,

View File

@ -7,9 +7,9 @@ import (
"os" "os"
"strings" "strings"
"github.com/containers/libpod/cmd/podman/common"
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/utils" "github.com/containers/libpod/cmd/podman/utils"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -22,7 +22,7 @@ var (
Note all data will be destroyed.` Note all data will be destroyed.`
pruneCommand = &cobra.Command{ pruneCommand = &cobra.Command{
Use: "prune", Use: "prune",
Args: common.NoArgs, Args: validate.NoArgs,
Short: "Remove all unused volumes", Short: "Remove all unused volumes",
Long: volumePruneDescription, Long: volumePruneDescription,
RunE: prune, RunE: prune,

View File

@ -2,6 +2,7 @@ package volumes
import ( import (
"github.com/containers/libpod/cmd/podman/registry" "github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/cmd/podman/validate"
"github.com/containers/libpod/pkg/domain/entities" "github.com/containers/libpod/pkg/domain/entities"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,7 +17,7 @@ var (
Short: "Manage volumes", Short: "Manage volumes",
Long: "Volumes are created in and can be shared between containers", Long: "Volumes are created in and can be shared between containers",
TraverseChildren: true, TraverseChildren: true,
RunE: registry.SubCommandExists, RunE: validate.SubCommandExists,
} }
) )