github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/swarm/leave.go (about)

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