github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_prune.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  	"fmt"
    21  	"strings"
    22  
    23  	"github.com/containerd/nerdctl/pkg/api/types"
    24  	"github.com/containerd/nerdctl/pkg/clientutil"
    25  	"github.com/containerd/nerdctl/pkg/cmd/image"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func newImagePruneCommand() *cobra.Command {
    30  	imagePruneCommand := &cobra.Command{
    31  		Use:           "prune [flags]",
    32  		Short:         "Remove unused images",
    33  		Args:          cobra.NoArgs,
    34  		RunE:          imagePruneAction,
    35  		SilenceUsage:  true,
    36  		SilenceErrors: true,
    37  	}
    38  
    39  	imagePruneCommand.Flags().BoolP("all", "a", false, "Remove all unused images, not just dangling ones")
    40  	imagePruneCommand.Flags().BoolP("force", "f", false, "Do not prompt for confirmation")
    41  	return imagePruneCommand
    42  }
    43  
    44  func processImagePruneOptions(cmd *cobra.Command) (types.ImagePruneOptions, error) {
    45  	globalOptions, err := processRootCmdFlags(cmd)
    46  	if err != nil {
    47  		return types.ImagePruneOptions{}, err
    48  	}
    49  	all, err := cmd.Flags().GetBool("all")
    50  	if err != nil {
    51  		return types.ImagePruneOptions{}, err
    52  	}
    53  
    54  	force, err := cmd.Flags().GetBool("force")
    55  	if err != nil {
    56  		return types.ImagePruneOptions{}, err
    57  	}
    58  
    59  	return types.ImagePruneOptions{
    60  		Stdout:   cmd.OutOrStdout(),
    61  		GOptions: globalOptions,
    62  		All:      all,
    63  		Force:    force,
    64  	}, err
    65  }
    66  
    67  func imagePruneAction(cmd *cobra.Command, _ []string) error {
    68  	options, err := processImagePruneOptions(cmd)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	if !options.Force {
    74  		var (
    75  			confirm string
    76  			msg     string
    77  		)
    78  		if !options.All {
    79  			msg = "This will remove all dangling images."
    80  		} else {
    81  			msg = "This will remove all images without at least one container associated to them."
    82  		}
    83  		msg += "\nAre you sure you want to continue? [y/N] "
    84  
    85  		fmt.Fprintf(cmd.OutOrStdout(), "WARNING! %s", msg)
    86  		fmt.Fscanf(cmd.InOrStdin(), "%s", &confirm)
    87  
    88  		if strings.ToLower(confirm) != "y" {
    89  			return nil
    90  		}
    91  	}
    92  
    93  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	defer cancel()
    98  
    99  	return image.Prune(ctx, client, options)
   100  }