mirror of
https://github.com/containers/podman.git
synced 2025-12-09 23:27:09 +08:00
Change exit code to 1 on podman rmi nosuch image
Make it easy for scripts to determine if an image removal failure. If only errors were no such image exit with 1 versus 125. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
This commit is contained in:
@@ -6,8 +6,6 @@ import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/varlink"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -20,22 +18,6 @@ func outputError(err error) {
|
||||
exitCode = status.ExitStatus()
|
||||
}
|
||||
}
|
||||
var ne error
|
||||
switch e := err.(type) {
|
||||
// For some reason golang wont let me list them with commas so listing them all.
|
||||
case *iopodman.ImageNotFound:
|
||||
ne = errors.New(e.Reason)
|
||||
case *iopodman.ContainerNotFound:
|
||||
ne = errors.New(e.Reason)
|
||||
case *iopodman.PodNotFound:
|
||||
ne = errors.New(e.Reason)
|
||||
case *iopodman.VolumeNotFound:
|
||||
ne = errors.New(e.Reason)
|
||||
case *iopodman.ErrorOccurred:
|
||||
ne = errors.New(e.Reason)
|
||||
default:
|
||||
ne = err
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, "Error:", ne.Error())
|
||||
fmt.Fprintln(os.Stderr, "Error:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||
"github.com/containers/libpod/cmd/podman/varlink"
|
||||
"github.com/containers/libpod/libpod/image"
|
||||
"github.com/containers/libpod/pkg/adapter"
|
||||
"github.com/containers/storage"
|
||||
"github.com/pkg/errors"
|
||||
@@ -29,6 +31,17 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func imageNotFound(err error) bool {
|
||||
if errors.Cause(err) == image.ErrNoSuchImage {
|
||||
return true
|
||||
}
|
||||
switch err.(type) {
|
||||
case *iopodman.ImageNotFound:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func init() {
|
||||
rmiCommand.Command = _rmiCommand
|
||||
rmiCommand.SetUsageTemplate(UsageTemplate())
|
||||
@@ -39,10 +52,8 @@ func init() {
|
||||
|
||||
func rmiCmd(c *cliconfig.RmiValues) error {
|
||||
var (
|
||||
lastError error
|
||||
deleted bool
|
||||
deleteErr error
|
||||
msg string
|
||||
lastError error
|
||||
failureCnt int
|
||||
)
|
||||
|
||||
ctx := getContext()
|
||||
@@ -64,19 +75,21 @@ func rmiCmd(c *cliconfig.RmiValues) error {
|
||||
images := args[:]
|
||||
|
||||
removeImage := func(img *adapter.ContainerImage) {
|
||||
deleted = true
|
||||
msg, deleteErr = runtime.RemoveImage(ctx, img, c.Force)
|
||||
if deleteErr != nil {
|
||||
if errors.Cause(deleteErr) == storage.ErrImageUsedByContainer {
|
||||
msg, err := runtime.RemoveImage(ctx, img, c.Force)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == storage.ErrImageUsedByContainer {
|
||||
fmt.Printf("A container associated with containers/storage, i.e. via Buildah, CRI-O, etc., may be associated with this image: %-12.12s\n", img.ID())
|
||||
}
|
||||
if !imageNotFound(err) {
|
||||
failureCnt++
|
||||
}
|
||||
if lastError != nil {
|
||||
fmt.Fprintln(os.Stderr, lastError)
|
||||
}
|
||||
lastError = deleteErr
|
||||
} else {
|
||||
fmt.Println(msg)
|
||||
lastError = err
|
||||
return
|
||||
}
|
||||
fmt.Println(msg)
|
||||
}
|
||||
|
||||
if removeAll {
|
||||
@@ -121,22 +134,21 @@ func rmiCmd(c *cliconfig.RmiValues) error {
|
||||
for _, i := range images {
|
||||
newImage, err := runtime.NewImageFromLocal(i)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
if lastError != nil {
|
||||
if !imageNotFound(lastError) {
|
||||
failureCnt++
|
||||
}
|
||||
fmt.Fprintln(os.Stderr, lastError)
|
||||
}
|
||||
lastError = err
|
||||
continue
|
||||
}
|
||||
removeImage(newImage)
|
||||
}
|
||||
}
|
||||
|
||||
// If the user calls remove all and there are none, it should not be a
|
||||
// non-zero exit
|
||||
if !deleted && removeAll {
|
||||
return nil
|
||||
}
|
||||
// the user tries to remove images that do not exist, that should be a
|
||||
// non-zero exit
|
||||
if !deleted {
|
||||
return errors.Errorf("no valid images to delete")
|
||||
if imageNotFound(lastError) && failureCnt == 0 {
|
||||
exitCode = 1
|
||||
}
|
||||
|
||||
return lastError
|
||||
|
||||
Reference in New Issue
Block a user