github.com/kunnos/engine@v1.13.1/cli/command/node/remove.go (about)

     1  package node
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/cli/command"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type removeOptions struct {
    16  	force bool
    17  }
    18  
    19  func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
    20  	opts := removeOptions{}
    21  
    22  	cmd := &cobra.Command{
    23  		Use:     "rm [OPTIONS] NODE [NODE...]",
    24  		Aliases: []string{"remove"},
    25  		Short:   "Remove one or more nodes from the swarm",
    26  		Args:    cli.RequiresMinArgs(1),
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			return runRemove(dockerCli, args, opts)
    29  		},
    30  	}
    31  	flags := cmd.Flags()
    32  	flags.BoolVarP(&opts.force, "force", "f", false, "Force remove a node from the swarm")
    33  	return cmd
    34  }
    35  
    36  func runRemove(dockerCli *command.DockerCli, args []string, opts removeOptions) error {
    37  	client := dockerCli.Client()
    38  	ctx := context.Background()
    39  
    40  	var errs []string
    41  
    42  	for _, nodeID := range args {
    43  		err := client.NodeRemove(ctx, nodeID, types.NodeRemoveOptions{Force: opts.force})
    44  		if err != nil {
    45  			errs = append(errs, err.Error())
    46  			continue
    47  		}
    48  		fmt.Fprintf(dockerCli.Out(), "%s\n", nodeID)
    49  	}
    50  
    51  	if len(errs) > 0 {
    52  		return fmt.Errorf("%s", strings.Join(errs, "\n"))
    53  	}
    54  
    55  	return nil
    56  }