remove.go 1.1 KB

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