summaryrefslogtreecommitdiff
path: root/cmd/podman/save.go
diff options
context:
space:
mode:
authorbaude <bbaude@redhat.com>2019-02-20 13:46:41 -0600
committerbaude <bbaude@redhat.com>2019-02-22 14:13:08 -0600
commit328250e7bef56b0b69dbea124c1170abd289900f (patch)
tree03a9d601d3f3ce0ac0d04fc3b2841b970031ed48 /cmd/podman/save.go
parent8039ccf4d560acbc308066cf8996bc4e6167a4a9 (diff)
downloadpodman-328250e7bef56b0b69dbea124c1170abd289900f.tar.gz
podman-328250e7bef56b0b69dbea124c1170abd289900f.tar.bz2
podman-328250e7bef56b0b69dbea124c1170abd289900f.zip
Improve command line validation
Use the checkallandlatest function to validate flag usage as part of the cobra command args validation. Signed-off-by: baude <bbaude@redhat.com>
Diffstat (limited to 'cmd/podman/save.go')
-rw-r--r--cmd/podman/save.go21
1 files changed, 18 insertions, 3 deletions
diff --git a/cmd/podman/save.go b/cmd/podman/save.go
index ba5209f34..ab421add6 100644
--- a/cmd/podman/save.go
+++ b/cmd/podman/save.go
@@ -1,20 +1,25 @@
package main
import (
- "os"
-
"github.com/containers/libpod/cmd/podman/cliconfig"
"github.com/containers/libpod/libpod/adapter"
+ "github.com/containers/libpod/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
+ "os"
+ "strings"
)
const (
ociManifestDir = "oci-dir"
+ ociArchive = "oci-archive"
v2s2ManifestDir = "docker-dir"
+ v2s2Archive = "docker-archive"
)
+var validFormats = []string{ociManifestDir, ociArchive, v2s2ManifestDir, v2s2Archive}
+
var (
saveCommand cliconfig.SaveValues
saveDescription = `
@@ -30,6 +35,16 @@ var (
saveCommand.GlobalFlags = MainGlobalOpts
return saveCmd(&saveCommand)
},
+ Args: func(cmd *cobra.Command, args []string) error {
+ format, err := cmd.Flags().GetString("format")
+ if err != nil {
+ return err
+ }
+ if !util.StringInSlice(format, validFormats) {
+ return errors.Errorf("format value must be one of %s", strings.Join(validFormats, " "))
+ }
+ return nil
+ },
Example: `podman save --quiet -o myimage.tar imageID
podman save --format docker-dir -o ubuntu-dir ubuntu
podman save > alpine-all.tar alpine:latest`,
@@ -41,7 +56,7 @@ func init() {
saveCommand.SetUsageTemplate(UsageTemplate())
flags := saveCommand.Flags()
flags.BoolVar(&saveCommand.Compress, "compress", false, "Compress tarball image layers when saving to a directory using the 'dir' transport. (default is same compression type as source)")
- flags.StringVar(&saveCommand.Format, "format", "docker-archive", "Save image to oci-archive, oci-dir (directory with oci manifest type), docker-dir (directory with v2s2 manifest type)")
+ flags.StringVar(&saveCommand.Format, "format", v2s2Archive, "Save image to oci-archive, oci-dir (directory with oci manifest type), docker-archive, docker-dir (directory with v2s2 manifest type)")
flags.StringVarP(&saveCommand.Output, "output", "o", "/dev/stdout", "Write to a file, default is STDOUT")
flags.BoolVarP(&saveCommand.Quiet, "quiet", "q", false, "Suppress the output")
}