remove.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. return cmd
  49. }
  50. func runRemove(ctx context.Context, backend compose.Service, opts removeOptions, services []string) error {
  51. project, err := opts.toProject(services)
  52. if err != nil {
  53. return err
  54. }
  55. if opts.stop {
  56. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  57. err := backend.Stop(ctx, project, compose.StopOptions{
  58. Services: services,
  59. })
  60. return "", err
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. }
  66. reosurces, err := backend.Remove(ctx, project, compose.RemoveOptions{
  67. DryRun: true,
  68. Services: services,
  69. })
  70. if err != nil {
  71. return err
  72. }
  73. if len(reosurces) == 0 {
  74. fmt.Println("No stopped containers")
  75. return nil
  76. }
  77. msg := fmt.Sprintf("Going to remove %s", strings.Join(reosurces, ", "))
  78. if opts.force {
  79. fmt.Println(msg)
  80. } else {
  81. confirm, err := prompt.User{}.Confirm(msg, false)
  82. if err != nil {
  83. return err
  84. }
  85. if !confirm {
  86. return nil
  87. }
  88. }
  89. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  90. _, err = backend.Remove(ctx, project, compose.RemoveOptions{
  91. Volumes: opts.volumes,
  92. Force: opts.force,
  93. })
  94. return "", err
  95. })
  96. return err
  97. }