remove.go 1.2 KB

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