Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --all, -a flag to image removal (rmi) #523

Merged
merged 1 commit into from
Sep 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,39 +284,70 @@ var imageStatusCommand = cli.Command{
}

var removeImageCommand = cli.Command{
Name: "rmi",
Usage: "Remove one or more images",
ArgsUsage: "IMAGE-ID [IMAGE-ID...]",
Action: func(context *cli.Context) error {
if context.NArg() == 0 {
return cli.ShowSubcommandHelp(context)
}
imageClient, conn, err := getImageClient(context)
Name: "rmi",
Usage: "Remove one or more images",
ArgsUsage: "IMAGE-ID [IMAGE-ID...]",
SkipArgReorder: true,
UseShortOptionHandling: true,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Remove all images",
},
},
Action: func(ctx *cli.Context) error {
imageClient, conn, err := getImageClient(ctx)
if err != nil {
return err
}
defer closeConnection(context, conn)
defer closeConnection(ctx, conn)

for i := 0; i < context.NArg(); i++ {
id := context.Args().Get(i)
ids := ctx.Args()
if ctx.Bool("all") {
r, err := imageClient.ListImages(context.Background(),
&pb.ListImagesRequest{})
if err != nil {
return err
}
ids = nil
for _, img := range r.GetImages() {
ids = append(ids, img.GetId())
}
}

if len(ids) == 0 {
return cli.ShowSubcommandHelp(ctx)
}

var verbose = false
status, err := ImageStatus(imageClient, id, verbose)
errored := false
for _, id := range ids {
status, err := ImageStatus(imageClient, id, false)
if err != nil {
return fmt.Errorf("image status request for %q failed: %v", id, err)
logrus.Errorf("image status request for %q failed: %v", id, err)
errored = true
continue
}
if status.Image == nil {
return fmt.Errorf("no such image %s", id)
logrus.Errorf("no such image %s", id)
errored = true
continue
}

_, err = RemoveImage(imageClient, id)
if err != nil {
return fmt.Errorf("error of removing image %q: %v", id, err)
logrus.Errorf("error of removing image %q: %v", id, err)
errored = true
continue
}
for _, repoTag := range status.Image.RepoTags {
fmt.Printf("Deleted: %s\n", repoTag)
}
}

if errored {
return fmt.Errorf("unable to remove the image(s)")
}

return nil
},
}
Expand Down