1
0

remove.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "fmt"
  17. "strings"
  18. "github.com/docker/compose-cli/api/compose"
  19. "github.com/docker/compose-cli/api/progress"
  20. "github.com/docker/compose-cli/utils/prompt"
  21. "github.com/spf13/cobra"
  22. )
  23. type removeOptions struct {
  24. *projectOptions
  25. force bool
  26. stop bool
  27. volumes bool
  28. }
  29. func removeCommand(p *projectOptions, backend compose.Service) *cobra.Command {
  30. opts := removeOptions{
  31. projectOptions: p,
  32. }
  33. cmd := &cobra.Command{
  34. Use: "rm [SERVICE...]",
  35. Short: "Removes stopped service containers",
  36. Long: `Removes stopped service containers
  37. By default, anonymous volumes attached to containers will not be removed. You
  38. can override this with -v. To list all volumes, use "docker volume ls".
  39. Any data which is not in a volume will be lost.`,
  40. RunE: Adapt(func(ctx context.Context, args []string) error {
  41. return runRemove(ctx, backend, opts, args)
  42. }),
  43. }
  44. f := cmd.Flags()
  45. f.BoolVarP(&opts.force, "force", "f", false, "Don't ask to confirm removal")
  46. f.BoolVarP(&opts.stop, "stop", "s", false, "Stop the containers, if required, before removing")
  47. f.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove any anonymous volumes attached to containers")
  48. f.BoolP("all", "a", false, "Deprecated - no effect")
  49. f.MarkHidden("all") //nolint:errcheck
  50. return cmd
  51. }
  52. func runRemove(ctx context.Context, backend compose.Service, opts removeOptions, services []string) error {
  53. project, err := opts.toProject(services)
  54. if err != nil {
  55. return err
  56. }
  57. if opts.stop {
  58. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  59. err := backend.Stop(ctx, project, compose.StopOptions{
  60. Services: services,
  61. })
  62. return "", err
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. reosurces, err := backend.Remove(ctx, project, compose.RemoveOptions{
  69. DryRun: true,
  70. Services: services,
  71. })
  72. if err != nil {
  73. return err
  74. }
  75. if len(reosurces) == 0 {
  76. fmt.Println("No stopped containers")
  77. return nil
  78. }
  79. msg := fmt.Sprintf("Going to remove %s", strings.Join(reosurces, ", "))
  80. if opts.force {
  81. fmt.Println(msg)
  82. } else {
  83. confirm, err := prompt.User{}.Confirm(msg, false)
  84. if err != nil {
  85. return err
  86. }
  87. if !confirm {
  88. return nil
  89. }
  90. }
  91. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  92. _, err = backend.Remove(ctx, project, compose.RemoveOptions{
  93. Volumes: opts.volumes,
  94. Force: opts.force,
  95. })
  96. return "", err
  97. })
  98. return err
  99. }