Add rm command

Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
Ulysses Souza 2020-05-10 22:37:28 +02:00
parent 1a3365fa37
commit 40fa78ac5d
7 changed files with 96 additions and 7 deletions

51
cli/cmd/rm.go Normal file
View file

@ -0,0 +1,51 @@
package cmd
import (
"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 {
errs = append(errs, err.Error())
continue
}
println(id)
}
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
}