github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/varlinkapi/shortcuts.go (about)

     1  package varlinkapi
     2  
     3  import (
     4  	"github.com/containers/podman/v2/libpod"
     5  	"github.com/sirupsen/logrus"
     6  )
     7  
     8  // getPodsByContext returns a slice of pods. Note that all, latest and pods are
     9  // mutually exclusive arguments.
    10  func getPodsByContext(all, latest bool, pods []string, runtime *libpod.Runtime) ([]*libpod.Pod, error) {
    11  	var outpods []*libpod.Pod
    12  	if all {
    13  		return runtime.GetAllPods()
    14  	}
    15  	if latest {
    16  		p, err := runtime.GetLatestPod()
    17  		if err != nil {
    18  			return nil, err
    19  		}
    20  		outpods = append(outpods, p)
    21  		return outpods, nil
    22  	}
    23  	var err error
    24  	for _, p := range pods {
    25  		pod, e := runtime.LookupPod(p)
    26  		if e != nil {
    27  			// Log all errors here, so callers don't need to.
    28  			logrus.Debugf("Error looking up pod %q: %v", p, e)
    29  			if err == nil {
    30  				err = e
    31  			}
    32  		} else {
    33  			outpods = append(outpods, pod)
    34  		}
    35  	}
    36  	return outpods, err
    37  }
    38  
    39  // getContainersByContext gets pods whether all, latest, or a slice of names/ids
    40  // is specified.
    41  func getContainersByContext(all, latest bool, names []string, runtime *libpod.Runtime) (ctrs []*libpod.Container, err error) {
    42  	var ctr *libpod.Container
    43  	ctrs = []*libpod.Container{}
    44  
    45  	switch {
    46  	case all:
    47  		ctrs, err = runtime.GetAllContainers()
    48  	case latest:
    49  		ctr, err = runtime.GetLatestContainer()
    50  		ctrs = append(ctrs, ctr)
    51  	default:
    52  		for _, n := range names {
    53  			ctr, e := runtime.LookupContainer(n)
    54  			if e != nil {
    55  				// Log all errors here, so callers don't need to.
    56  				logrus.Debugf("Error looking up container %q: %v", n, e)
    57  				if err == nil {
    58  					err = e
    59  				}
    60  			} else {
    61  				ctrs = append(ctrs, ctr)
    62  			}
    63  		}
    64  	}
    65  	return
    66  }