github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/common_libpod.go (about) 1 //build !remoteclient 2 3 package main 4 5 import ( 6 "fmt" 7 "os" 8 9 "github.com/containers/libpod/cmd/podman/cliconfig" 10 "github.com/containers/libpod/libpod" 11 "github.com/containers/libpod/libpod/define" 12 "github.com/pkg/errors" 13 ) 14 15 // getAllOrLatestContainers tries to return the correct list of containers 16 // depending if --all, --latest or <container-id> is used. 17 // It requires the Context (c) and the Runtime (runtime). As different 18 // commands are using different container state for the --all option 19 // the desired state has to be specified in filterState. If no filter 20 // is desired a -1 can be used to get all containers. For a better 21 // error message, if the filter fails, a corresponding verb can be 22 // specified which will then appear in the error message. 23 func getAllOrLatestContainers(c *cliconfig.PodmanCommand, runtime *libpod.Runtime, filterState define.ContainerStatus, verb string) ([]*libpod.Container, error) { 24 var containers []*libpod.Container 25 var lastError error 26 var err error 27 switch { 28 case c.Bool("all"): 29 if filterState != -1 { 30 var filterFuncs []libpod.ContainerFilter 31 filterFuncs = append(filterFuncs, func(c *libpod.Container) bool { 32 state, _ := c.State() 33 return state == filterState 34 }) 35 containers, err = runtime.GetContainers(filterFuncs...) 36 } else { 37 containers, err = runtime.GetContainers() 38 } 39 if err != nil { 40 return nil, errors.Wrapf(err, "unable to get %s containers", verb) 41 } 42 case c.Bool("latest"): 43 lastCtr, err := runtime.GetLatestContainer() 44 if err != nil { 45 return nil, errors.Wrapf(err, "unable to get latest container") 46 } 47 containers = append(containers, lastCtr) 48 default: 49 args := c.InputArgs 50 for _, i := range args { 51 container, err := runtime.LookupContainer(i) 52 if err != nil { 53 if lastError != nil { 54 fmt.Fprintln(os.Stderr, lastError) 55 } 56 lastError = errors.Wrapf(err, "unable to find container %s", i) 57 } 58 if container != nil { 59 // This is here to make sure this does not return [<nil>] but only nil 60 containers = append(containers, container) 61 } 62 } 63 } 64 65 return containers, lastError 66 }