summaryrefslogtreecommitdiff
path: root/cmd/kpod/save.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
committerMatthew Heon <matthew.heon@gmail.com>2017-11-01 11:24:59 -0400
commita031b83a09a8628435317a03f199cdc18b78262f (patch)
treebc017a96769ce6de33745b8b0b1304ccf38e9df0 /cmd/kpod/save.go
parent2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff)
downloadpodman-a031b83a09a8628435317a03f199cdc18b78262f.tar.gz
podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.bz2
podman-a031b83a09a8628435317a03f199cdc18b78262f.zip
Initial checkin from CRI-O repo
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'cmd/kpod/save.go')
-rw-r--r--cmd/kpod/save.go98
1 files changed, 98 insertions, 0 deletions
diff --git a/cmd/kpod/save.go b/cmd/kpod/save.go
new file mode 100644
index 000000000..cfe90a95e
--- /dev/null
+++ b/cmd/kpod/save.go
@@ -0,0 +1,98 @@
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/kubernetes-incubator/cri-o/libpod"
+ "github.com/pkg/errors"
+ "github.com/sirupsen/logrus"
+ "github.com/urfave/cli"
+)
+
+var (
+ saveFlags = []cli.Flag{
+ cli.StringFlag{
+ Name: "output, o",
+ Usage: "Write to a file, default is STDOUT",
+ Value: "/dev/stdout",
+ },
+ cli.BoolFlag{
+ Name: "quiet, q",
+ Usage: "Suppress the output",
+ },
+ cli.StringFlag{
+ Name: "format",
+ Usage: "Save image to oci-archive",
+ },
+ }
+ saveDescription = `
+ Save an image to docker-archive or oci-archive on the local machine.
+ Default is docker-archive`
+
+ saveCommand = cli.Command{
+ Name: "save",
+ Usage: "Save image to an archive",
+ Description: saveDescription,
+ Flags: saveFlags,
+ Action: saveCmd,
+ ArgsUsage: "",
+ }
+)
+
+// saveCmd saves the image to either docker-archive or oci
+func saveCmd(c *cli.Context) error {
+ args := c.Args()
+ if len(args) == 0 {
+ return errors.Errorf("need at least 1 argument")
+ }
+ if err := validateFlags(c, saveFlags); err != nil {
+ return err
+ }
+
+ runtime, err := getRuntime(c)
+ if err != nil {
+ return errors.Wrapf(err, "could not create runtime")
+ }
+ defer runtime.Shutdown(false)
+
+ var writer io.Writer
+ if !c.Bool("quiet") {
+ writer = os.Stdout
+ }
+
+ output := c.String("output")
+ if output == "/dev/stdout" {
+ fi := os.Stdout
+ if logrus.IsTerminal(fi) {
+ return errors.Errorf("refusing to save to terminal. Use -o flag or redirect")
+ }
+ }
+
+ var dst string
+ switch c.String("format") {
+ case libpod.OCIArchive:
+ dst = libpod.OCIArchive + ":" + output
+ case libpod.DockerArchive:
+ fallthrough
+ case "":
+ dst = libpod.DockerArchive + ":" + output
+ default:
+ return errors.Errorf("unknown format option %q", c.String("format"))
+ }
+
+ saveOpts := libpod.CopyOptions{
+ SignaturePolicyPath: "",
+ Writer: writer,
+ }
+
+ // only one image is supported for now
+ // future pull requests will fix this
+ for _, image := range args {
+ dest := dst + ":" + image
+ if err := runtime.PushImage(image, dest, saveOpts); err != nil {
+ return errors.Wrapf(err, "unable to save %q", image)
+ }
+ }
+ return nil
+}