2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 18:02:44 -07:00
|
|
|
"context"
|
2025-02-03 19:36:59 +01:00
|
|
|
"errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2017-07-20 10:32:51 +02: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 {
|
2023-09-09 22:27:44 +00:00
|
|
|
return runRemove(cmd.Context(), dockerCli, args)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2025-03-27 09:55:01 +01:00
|
|
|
ValidArgsFunction: completeServiceNames(dockerCli),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
cmd.Flags()
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2025-02-01 22:51:46 +01:00
|
|
|
func runRemove(ctx context.Context, dockerCLI command.Cli, serviceIDs []string) error {
|
|
|
|
apiClient := dockerCLI.Client()
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2025-02-03 19:36:59 +01:00
|
|
|
var errs []error
|
2025-02-01 22:51:46 +01:00
|
|
|
for _, id := range serviceIDs {
|
2025-02-03 19:36:59 +01:00
|
|
|
if err := apiClient.ServiceRemove(ctx, id); err != nil {
|
|
|
|
errs = append(errs, err)
|
2016-09-08 13:11:39 -04:00
|
|
|
continue
|
|
|
|
}
|
2025-02-01 22:51:46 +01:00
|
|
|
_, _ = fmt.Fprintln(dockerCLI.Out(), id)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2025-02-03 19:36:59 +01:00
|
|
|
return errors.Join(errs...)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|