remove.go 1.2 KB

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