github.com/kunnos/engine@v1.13.1/cli/command/swarm/leave.go (about)

     1  package swarm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/cli"
     9  	"github.com/docker/docker/cli/command"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type leaveOptions struct {
    14  	force bool
    15  }
    16  
    17  func newLeaveCommand(dockerCli *command.DockerCli) *cobra.Command {
    18  	opts := leaveOptions{}
    19  
    20  	cmd := &cobra.Command{
    21  		Use:   "leave [OPTIONS]",
    22  		Short: "Leave the swarm",
    23  		Args:  cli.NoArgs,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			return runLeave(dockerCli, opts)
    26  		},
    27  	}
    28  
    29  	flags := cmd.Flags()
    30  	flags.BoolVarP(&opts.force, "force", "f", false, "Force this node to leave the swarm, ignoring warnings")
    31  	return cmd
    32  }
    33  
    34  func runLeave(dockerCli *command.DockerCli, opts leaveOptions) error {
    35  	client := dockerCli.Client()
    36  	ctx := context.Background()
    37  
    38  	if err := client.SwarmLeave(ctx, opts.force); err != nil {
    39  		return err
    40  	}
    41  
    42  	fmt.Fprintln(dockerCli.Out(), "Node left the swarm.")
    43  	return nil
    44  }