pkg/podman: Wrap 'podman container exists' and 'podman image exists'

https://github.com/containers/toolbox/pull/318
This commit is contained in:
Harry Míchal 2020-05-11 16:58:48 +02:00 committed by Debarshi Ray
parent 936f22ff15
commit 8aea0b0521
1 changed files with 38 additions and 0 deletions

View File

@ -44,6 +44,25 @@ func CheckVersion(requiredVersion string) bool {
return version.CompareSimple(podmanVersion, requiredVersion) >= 0
}
// ContainerExists checks using Podman if a container with given ID/name exists.
//
// Parameter container is a name or an id of a container.
func ContainerExists(container string) (bool, error) {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "container", "exists", container}
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to find container %s", container)
}
if err != nil {
return false, err
}
return true, nil
}
// GetContainers is a wrapper function around `podman ps --format json` command.
//
// Parameter args accepts an array of strings to be passed to the wrapped command (eg. ["-a", "--filter", "123"]).
@ -125,6 +144,25 @@ func GetVersion() (string, error) {
return podmanVersion, nil
}
// ImageExists checks using Podman if an image with given ID/name exists.
//
// Parameter image is a name or an id of an image.
func ImageExists(image string) (bool, error) {
logLevelString := LogLevel.String()
args := []string{"--log-level", logLevelString, "image", "exists", image}
exitCode, err := shell.RunWithExitCode("podman", nil, nil, nil, args...)
if exitCode != 0 && err == nil {
err = fmt.Errorf("failed to find image %s", image)
}
if err != nil {
return false, err
}
return true, nil
}
// Inspect is a wrapper around 'podman inspect' command
//
// Parameter 'typearg' takes in values 'container' or 'image' that is passed to the --type flag