leave.go 886 B

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