This replaces the visitAll recursive function with a test that verifies that the option is set for all commands and subcommands, so that it doesn't have to be modified at runtime. We currently still have to loop over all functions for the setValidateArgs call, but that can be looked at separately. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
43 lines
969 B
Go
43 lines
969 B
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func newRemoveCommand(dockerCLI command.Cli) *cobra.Command {
|
|
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(cmd.Context(), dockerCLI, args)
|
|
},
|
|
ValidArgsFunction: completeServiceNames(dockerCLI),
|
|
DisableFlagsInUseLine: true,
|
|
}
|
|
cmd.Flags()
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runRemove(ctx context.Context, dockerCLI command.Cli, serviceIDs []string) error {
|
|
apiClient := dockerCLI.Client()
|
|
|
|
var errs []error
|
|
for _, id := range serviceIDs {
|
|
if err := apiClient.ServiceRemove(ctx, id); err != nil {
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
_, _ = fmt.Fprintln(dockerCLI.Out(), id)
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|