remove.go 967 B

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