2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm SERVICE [SERVICE...]",
|
|
|
|
Aliases: []string{"remove"},
|
|
|
|
Short: "Remove one or more services",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runRemove(dockerCli, args)
|
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
return CompletionFn(dockerCli)(cmd, args, toComplete)
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
cmd.Flags()
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-07-20 04:32:51 -04:00
|
|
|
func runRemove(dockerCli command.Cli, sids []string) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
var errs []string
|
|
|
|
for _, sid := range sids {
|
|
|
|
err := client.ServiceRemove(ctx, sid)
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", sid)
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf(strings.Join(errs, "\n"))
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|