2020-05-10 22:37:28 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-05-13 09:00:48 +02:00
|
|
|
"fmt"
|
2020-05-10 22:37:28 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/docker/api/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rmOpts struct {
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// RmCommand deletes containers
|
|
|
|
func RmCommand() *cobra.Command {
|
|
|
|
var opts rmOpts
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm",
|
|
|
|
Aliases: []string{"delete"},
|
|
|
|
Short: "Remove containers",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
var errs []string
|
|
|
|
c, err := client.New(cmd.Context())
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "cannot connect to backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range args {
|
|
|
|
err := c.ContainerService().Delete(cmd.Context(), id, opts.force)
|
|
|
|
if err != nil {
|
2020-05-13 12:37:18 +02:00
|
|
|
errs = append(errs, err.Error()+" "+id)
|
2020-05-10 22:37:28 +02:00
|
|
|
continue
|
|
|
|
}
|
2020-05-13 09:00:48 +02:00
|
|
|
fmt.Println(id)
|
2020-05-10 22:37:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return errors.New(strings.Join(errs, "\n"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Flags().BoolVarP(&opts.force, "force", "f", false, "Force removal")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|