rm.go 980 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package cmd
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/pkg/errors"
  6. "github.com/spf13/cobra"
  7. "github.com/docker/api/client"
  8. )
  9. type rmOpts struct {
  10. force bool
  11. }
  12. // RmCommand deletes containers
  13. func RmCommand() *cobra.Command {
  14. var opts rmOpts
  15. cmd := &cobra.Command{
  16. Use: "rm",
  17. Aliases: []string{"delete"},
  18. Short: "Remove containers",
  19. Args: cobra.MinimumNArgs(1),
  20. RunE: func(cmd *cobra.Command, args []string) error {
  21. var errs []string
  22. c, err := client.New(cmd.Context())
  23. if err != nil {
  24. return errors.Wrap(err, "cannot connect to backend")
  25. }
  26. for _, id := range args {
  27. err := c.ContainerService().Delete(cmd.Context(), id, opts.force)
  28. if err != nil {
  29. errs = append(errs, err.Error()+" "+id)
  30. continue
  31. }
  32. fmt.Println(id)
  33. }
  34. if len(errs) > 0 {
  35. return errors.New(strings.Join(errs, "\n"))
  36. }
  37. return nil
  38. },
  39. }
  40. cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force removal")
  41. return cmd
  42. }