github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/secret/remove.go (about)

     1  package secret
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/khulnasoft/cli/cli"
     9  	"github.com/khulnasoft/cli/cli/command"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type removeOptions struct {
    15  	names []string
    16  }
    17  
    18  func newSecretRemoveCommand(dockerCli command.Cli) *cobra.Command {
    19  	return &cobra.Command{
    20  		Use:     "rm SECRET [SECRET...]",
    21  		Aliases: []string{"remove"},
    22  		Short:   "Remove one or more secrets",
    23  		Args:    cli.RequiresMinArgs(1),
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			opts := removeOptions{
    26  				names: args,
    27  			}
    28  			return runSecretRemove(cmd.Context(), dockerCli, opts)
    29  		},
    30  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    31  			return completeNames(dockerCli)(cmd, args, toComplete)
    32  		},
    33  	}
    34  }
    35  
    36  func runSecretRemove(ctx context.Context, dockerCli command.Cli, opts removeOptions) error {
    37  	client := dockerCli.Client()
    38  
    39  	var errs []string
    40  
    41  	for _, name := range opts.names {
    42  		if err := client.SecretRemove(ctx, name); err != nil {
    43  			errs = append(errs, err.Error())
    44  			continue
    45  		}
    46  
    47  		fmt.Fprintln(dockerCli.Out(), name)
    48  	}
    49  
    50  	if len(errs) > 0 {
    51  		return errors.Errorf("%s", strings.Join(errs, "\n"))
    52  	}
    53  
    54  	return nil
    55  }