remove.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "github.com/docker/cli/cli/command"
  17. "github.com/docker/compose/v2/pkg/api"
  18. "github.com/spf13/cobra"
  19. )
  20. type removeOptions struct {
  21. *ProjectOptions
  22. force bool
  23. stop bool
  24. volumes bool
  25. }
  26. func removeCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  27. opts := removeOptions{
  28. ProjectOptions: p,
  29. }
  30. cmd := &cobra.Command{
  31. Use: "rm [OPTIONS] [SERVICE...]",
  32. Short: "Removes stopped service containers",
  33. Long: `Removes stopped service containers
  34. By default, anonymous volumes attached to containers will not be removed. You
  35. can override this with -v. To list all volumes, use "docker volume ls".
  36. Any data which is not in a volume will be lost.`,
  37. RunE: Adapt(func(ctx context.Context, args []string) error {
  38. return runRemove(ctx, dockerCli, backend, opts, args)
  39. }),
  40. ValidArgsFunction: completeServiceNames(dockerCli, p),
  41. }
  42. f := cmd.Flags()
  43. f.BoolVarP(&opts.force, "force", "f", false, "Don't ask to confirm removal")
  44. f.BoolVarP(&opts.stop, "stop", "s", false, "Stop the containers, if required, before removing")
  45. f.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove any anonymous volumes attached to containers")
  46. f.BoolP("all", "a", false, "Deprecated - no effect")
  47. f.MarkHidden("all") //nolint:errcheck
  48. return cmd
  49. }
  50. func runRemove(ctx context.Context, dockerCli command.Cli, backend api.Service, opts removeOptions, services []string) error {
  51. project, name, err := opts.projectOrName(ctx, dockerCli, services...)
  52. if err != nil {
  53. return err
  54. }
  55. return backend.Remove(ctx, name, api.RemoveOptions{
  56. Services: services,
  57. Force: opts.force,
  58. Volumes: opts.volumes,
  59. Project: project,
  60. Stop: opts.stop,
  61. })
  62. }