github.com/portworx/docker@v1.12.1/api/client/node/remove.go (about)

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