remove.go 2.9 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/client"
  19. "github.com/docker/compose-cli/api/compose"
  20. "github.com/docker/compose-cli/api/progress"
  21. "github.com/docker/compose-cli/utils/prompt"
  22. "github.com/spf13/cobra"
  23. )
  24. type removeOptions struct {
  25. *projectOptions
  26. force bool
  27. stop bool
  28. volumes bool
  29. }
  30. func removeCommand(p *projectOptions) *cobra.Command {
  31. opts := removeOptions{
  32. projectOptions: p,
  33. }
  34. cmd := &cobra.Command{
  35. Use: "rm [SERVICE...]",
  36. Short: "Removes stopped service containers",
  37. Long: `Removes stopped service containers
  38. By default, anonymous volumes attached to containers will not be removed. You
  39. can override this with -v. To list all volumes, use "docker volume ls".
  40. Any data which is not in a volume will be lost.`,
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. return runRemove(cmd.Context(), opts, args)
  43. },
  44. }
  45. f := cmd.Flags()
  46. f.BoolVarP(&opts.force, "force", "f", false, "Don't ask to confirm removal")
  47. f.BoolVarP(&opts.stop, "stop", "s", false, "Stop the containers, if required, before removing")
  48. f.BoolVarP(&opts.volumes, "volumes", "v", false, "Remove any anonymous volumes attached to containers")
  49. return cmd
  50. }
  51. func runRemove(ctx context.Context, opts removeOptions, services []string) error {
  52. c, err := client.NewWithDefaultLocalBackend(ctx)
  53. if err != nil {
  54. return err
  55. }
  56. project, err := opts.toProject(services)
  57. if err != nil {
  58. return err
  59. }
  60. if opts.stop {
  61. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  62. err := c.ComposeService().Stop(ctx, project)
  63. return "", err
  64. })
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
  70. DryRun: true,
  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 = c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
  93. Volumes: opts.volumes,
  94. Force: opts.force,
  95. })
  96. return "", err
  97. })
  98. return err
  99. }