remove.go 988 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/pkg/errors"
  8. "github.com/spf13/cobra"
  9. "golang.org/x/net/context"
  10. )
  11. func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
  12. cmd := &cobra.Command{
  13. Use: "rm SERVICE [SERVICE...]",
  14. Aliases: []string{"remove"},
  15. Short: "Remove one or more services",
  16. Args: cli.RequiresMinArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. return runRemove(dockerCli, args)
  19. },
  20. }
  21. cmd.Flags()
  22. return cmd
  23. }
  24. func runRemove(dockerCli *command.DockerCli, sids []string) error {
  25. client := dockerCli.Client()
  26. ctx := context.Background()
  27. var errs []string
  28. for _, sid := range sids {
  29. err := client.ServiceRemove(ctx, sid)
  30. if err != nil {
  31. errs = append(errs, err.Error())
  32. continue
  33. }
  34. fmt.Fprintf(dockerCli.Out(), "%s\n", sid)
  35. }
  36. if len(errs) > 0 {
  37. return errors.Errorf(strings.Join(errs, "\n"))
  38. }
  39. return nil
  40. }