github.com/thajeztah/cli@v0.0.0-20240223162942-dc6bfac81a8b/cli/command/plugin/remove.go (about)

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/cli/cli"
     8  	"github.com/docker/cli/cli/command"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type rmOptions struct {
    14  	force bool
    15  
    16  	plugins []string
    17  }
    18  
    19  func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
    20  	var opts rmOptions
    21  
    22  	cmd := &cobra.Command{
    23  		Use:     "rm [OPTIONS] PLUGIN [PLUGIN...]",
    24  		Short:   "Remove one or more plugins",
    25  		Aliases: []string{"remove"},
    26  		Args:    cli.RequiresMinArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			opts.plugins = args
    29  			return runRemove(cmd.Context(), dockerCli, &opts)
    30  		},
    31  	}
    32  
    33  	flags := cmd.Flags()
    34  	flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of an active plugin")
    35  	return cmd
    36  }
    37  
    38  func runRemove(ctx context.Context, dockerCli command.Cli, opts *rmOptions) error {
    39  	var errs cli.Errors
    40  	for _, name := range opts.plugins {
    41  		if err := dockerCli.Client().PluginRemove(ctx, name, types.PluginRemoveOptions{Force: opts.force}); err != nil {
    42  			errs = append(errs, err)
    43  			continue
    44  		}
    45  		fmt.Fprintln(dockerCli.Out(), name)
    46  	}
    47  	// Do not simplify to `return errs` because even if errs == nil, it is not a nil-error interface value.
    48  	if errs != nil {
    49  		return errs
    50  	}
    51  	return nil
    52  }