github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/command/service/remove.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"github.com/pkg/errors"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
    15  	cmd := &cobra.Command{
    16  		Use:     "rm SERVICE [SERVICE...]",
    17  		Aliases: []string{"remove"},
    18  		Short:   "Remove one or more services",
    19  		Args:    cli.RequiresMinArgs(1),
    20  		RunE: func(cmd *cobra.Command, args []string) error {
    21  			return runRemove(dockerCli, args)
    22  		},
    23  		ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    24  			return CompletionFn(dockerCli)(cmd, args, toComplete)
    25  		},
    26  	}
    27  	cmd.Flags()
    28  
    29  	return cmd
    30  }
    31  
    32  func runRemove(dockerCli command.Cli, sids []string) error {
    33  	client := dockerCli.Client()
    34  
    35  	ctx := context.Background()
    36  
    37  	var errs []string
    38  	for _, sid := range sids {
    39  		err := client.ServiceRemove(ctx, sid)
    40  		if err != nil {
    41  			errs = append(errs, err.Error())
    42  			continue
    43  		}
    44  		fmt.Fprintf(dockerCli.Out(), "%s\n", sid)
    45  	}
    46  	if len(errs) > 0 {
    47  		return errors.Errorf(strings.Join(errs, "\n"))
    48  	}
    49  	return nil
    50  }