spiegel_podman/cmd/podman/rmi.go
baude 6847636c30 Remove by shortname
Removing by shortname was not working.  Also pruned
container storage's remove func from rmi and moved it into
an image.Remove func, which consolidates our usage of cs.

Signed-off-by: baude <bbaude@redhat.com>

Closes: #188
Approved by: baude
2018-01-08 18:15:55 +00:00

79 lines
1.7 KiB
Go

package main
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/projectatomic/libpod/libpod"
"github.com/urfave/cli"
)
var (
rmiDescription = "removes one or more locally stored images."
rmiFlags = []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "remove all images",
},
cli.BoolFlag{
Name: "force, f",
Usage: "force removal of the image",
},
}
rmiCommand = cli.Command{
Name: "rmi",
Usage: "removes one or more images from local storage",
Description: rmiDescription,
Action: rmiCmd,
ArgsUsage: "IMAGE-NAME-OR-ID [...]",
Flags: rmiFlags,
UseShortOptionHandling: true,
}
)
func rmiCmd(c *cli.Context) error {
if err := validateFlags(c, rmiFlags); err != nil {
return err
}
removeAll := c.Bool("all")
runtime, err := getRuntime(c)
if err != nil {
return errors.Wrapf(err, "could not get runtime")
}
defer runtime.Shutdown(false)
args := c.Args()
if len(args) == 0 && !removeAll {
return errors.Errorf("image name or ID must be specified")
}
if len(args) > 0 && removeAll {
return errors.Errorf("when using the --all switch, you may not pass any images names or IDs")
}
imagesToDelete := args[:]
var lastError error
if removeAll {
localImages, err := runtime.GetImages(&libpod.ImageFilterParams{})
if err != nil {
return errors.Wrapf(err, "unable to query local images")
}
for _, image := range localImages {
imagesToDelete = append(imagesToDelete, image.ID)
}
}
for _, arg := range imagesToDelete {
image := runtime.NewImage(arg)
iid, err := image.Remove(c.Bool("force"))
if err != nil {
if lastError != nil {
fmt.Fprintln(os.Stderr, lastError)
}
lastError = err
} else {
fmt.Println(iid)
}
}
return lastError
}