remove.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package network
  2. import (
  3. "fmt"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/spf13/cobra"
  8. )
  9. func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
  10. return &cobra.Command{
  11. Use: "rm NETWORK [NETWORK...]",
  12. Aliases: []string{"remove"},
  13. Short: "Remove one or more networks",
  14. Args: cli.RequiresMinArgs(1),
  15. RunE: func(cmd *cobra.Command, args []string) error {
  16. return runRemove(dockerCli, args)
  17. },
  18. }
  19. }
  20. const ingressWarning = "WARNING! Before removing the routing-mesh network, " +
  21. "make sure all the nodes in your swarm run the same docker engine version. " +
  22. "Otherwise, removal may not be effective and functionality of newly create " +
  23. "ingress networks will be impaired.\nAre you sure you want to continue?"
  24. func runRemove(dockerCli *command.DockerCli, networks []string) error {
  25. client := dockerCli.Client()
  26. ctx := context.Background()
  27. status := 0
  28. for _, name := range networks {
  29. if nw, _, err := client.NetworkInspectWithRaw(ctx, name, false); err == nil &&
  30. nw.Ingress &&
  31. !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), ingressWarning) {
  32. continue
  33. }
  34. if err := client.NetworkRemove(ctx, name); err != nil {
  35. fmt.Fprintf(dockerCli.Err(), "%s\n", err)
  36. status = 1
  37. continue
  38. }
  39. fmt.Fprintf(dockerCli.Out(), "%s\n", name)
  40. }
  41. if status != 0 {
  42. return cli.StatusError{StatusCode: status}
  43. }
  44. return nil
  45. }