github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_remove.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "github.com/containerd/nerdctl/pkg/api/types" 21 "github.com/containerd/nerdctl/pkg/clientutil" 22 "github.com/containerd/nerdctl/pkg/cmd/image" 23 "github.com/spf13/cobra" 24 ) 25 26 func newRmiCommand() *cobra.Command { 27 var rmiCommand = &cobra.Command{ 28 Use: "rmi [flags] IMAGE [IMAGE, ...]", 29 Short: "Remove one or more images", 30 Args: cobra.MinimumNArgs(1), 31 RunE: rmiAction, 32 ValidArgsFunction: rmiShellComplete, 33 SilenceUsage: true, 34 SilenceErrors: true, 35 } 36 rmiCommand.Flags().BoolP("force", "f", false, "Force removal of the image") 37 // Alias `-a` is reserved for `--all`. Should be compatible with `podman rmi --all`. 38 rmiCommand.Flags().Bool("async", false, "Asynchronous mode") 39 return rmiCommand 40 } 41 42 func processImageRemoveOptions(cmd *cobra.Command) (types.ImageRemoveOptions, error) { 43 globalOptions, err := processRootCmdFlags(cmd) 44 if err != nil { 45 return types.ImageRemoveOptions{}, err 46 } 47 48 force, err := cmd.Flags().GetBool("force") 49 if err != nil { 50 return types.ImageRemoveOptions{}, err 51 } 52 async, err := cmd.Flags().GetBool("async") 53 if err != nil { 54 return types.ImageRemoveOptions{}, err 55 } 56 57 return types.ImageRemoveOptions{ 58 Stdout: cmd.OutOrStdout(), 59 GOptions: globalOptions, 60 Force: force, 61 Async: async, 62 }, nil 63 } 64 65 func rmiAction(cmd *cobra.Command, args []string) error { 66 options, err := processImageRemoveOptions(cmd) 67 if err != nil { 68 return err 69 } 70 71 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 72 if err != nil { 73 return err 74 } 75 defer cancel() 76 77 return image.Remove(ctx, client, args, options) 78 } 79 80 func rmiShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 81 // show image names 82 return shellCompleteImageNames(cmd) 83 }