github.com/xeptore/docker-cli@v20.10.14+incompatible/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  
    16  	cmd := &cobra.Command{
    17  		Use:     "rm SERVICE [SERVICE...]",
    18  		Aliases: []string{"remove"},
    19  		Short:   "Remove one or more services",
    20  		Args:    cli.RequiresMinArgs(1),
    21  		RunE: func(cmd *cobra.Command, args []string) error {
    22  			return runRemove(dockerCli, args)
    23  		},
    24  	}
    25  	cmd.Flags()
    26  
    27  	return cmd
    28  }
    29  
    30  func runRemove(dockerCli command.Cli, sids []string) error {
    31  	client := dockerCli.Client()
    32  
    33  	ctx := context.Background()
    34  
    35  	var errs []string
    36  	for _, sid := range sids {
    37  		err := client.ServiceRemove(ctx, sid)
    38  		if err != nil {
    39  			errs = append(errs, err.Error())
    40  			continue
    41  		}
    42  		fmt.Fprintf(dockerCli.Out(), "%s\n", sid)
    43  	}
    44  	if len(errs) > 0 {
    45  		return errors.Errorf(strings.Join(errs, "\n"))
    46  	}
    47  	return nil
    48  }