diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2018-10-23 09:22:03 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-23 09:22:03 -0700 |
commit | c019830a750d3c2cdb20d2568c9e0f9fdc35165e (patch) | |
tree | ffd123dcfa33f64a27d783eb70edf9332f2bc30f /cmd/podman/common.go | |
parent | 41a8bbdbe01d76d39a7a670ac17df1d3da8b28d5 (diff) | |
parent | e8d69030b621874159176eb67292ce06632ea2fd (diff) | |
download | podman-c019830a750d3c2cdb20d2568c9e0f9fdc35165e.tar.gz podman-c019830a750d3c2cdb20d2568c9e0f9fdc35165e.tar.bz2 podman-c019830a750d3c2cdb20d2568c9e0f9fdc35165e.zip |
Merge pull request #1662 from adrianreber/all-and-latest
Add --all and --latest to checkpoint/restore
Diffstat (limited to 'cmd/podman/common.go')
-rw-r--r-- | cmd/podman/common.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/cmd/podman/common.go b/cmd/podman/common.go index 8ae1c9e0f..f9e746b28 100644 --- a/cmd/podman/common.go +++ b/cmd/podman/common.go @@ -89,6 +89,73 @@ func validateFlags(c *cli.Context, flags []cli.Flag) error { return nil } +// checkAllAndLatest checks that --all and --latest are used correctly +func checkAllAndLatest(c *cli.Context) error { + argLen := len(c.Args()) + if (c.Bool("all") || c.Bool("latest")) && argLen > 0 { + return errors.Errorf("no arguments are needed with --all or --latest") + } + if c.Bool("all") && c.Bool("latest") { + return errors.Errorf("--all and --latest cannot be used together") + } + if argLen < 1 && !c.Bool("all") && !c.Bool("latest") { + return errors.Errorf("you must provide at least one pod name or id") + } + return nil +} + +// getAllOrLatestContainers tries to return the correct list of containers +// depending if --all, --latest or <container-id> is used. +// It requires the Context (c) and the Runtime (runtime). As different +// commands are using different container state for the --all option +// the desired state has to be specified in filterState. If no filter +// is desired a -1 can be used to get all containers. For a better +// error message, if the filter fails, a corresponding verb can be +// specified which will then appear in the error message. +func getAllOrLatestContainers(c *cli.Context, runtime *libpod.Runtime, filterState libpod.ContainerStatus, verb string) ([]*libpod.Container, error) { + var containers []*libpod.Container + var lastError error + var err error + if c.Bool("all") { + if filterState != -1 { + var filterFuncs []libpod.ContainerFilter + filterFuncs = append(filterFuncs, func(c *libpod.Container) bool { + state, _ := c.State() + return state == filterState + }) + containers, err = runtime.GetContainers(filterFuncs...) + } else { + containers, err = runtime.GetContainers() + } + if err != nil { + return nil, errors.Wrapf(err, "unable to get %s containers", verb) + } + } else if c.Bool("latest") { + lastCtr, err := runtime.GetLatestContainer() + if err != nil { + return nil, errors.Wrapf(err, "unable to get latest container") + } + containers = append(containers, lastCtr) + } else { + args := c.Args() + for _, i := range args { + container, err := runtime.LookupContainer(i) + if err != nil { + if lastError != nil { + fmt.Fprintln(os.Stderr, lastError) + } + lastError = errors.Wrapf(err, "unable to find container %s", i) + } + if container != nil { + // This is here to make sure this does not return [<nil>] but only nil + containers = append(containers, container) + } + } + } + + return containers, lastError +} + // getContext returns a non-nil, empty context func getContext() context.Context { return context.TODO() |