1
0

remove.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.New(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, compose.StopOptions{
  63. Services: services,
  64. })
  65. return "", err
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. }
  71. reosurces, err := c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
  72. DryRun: true,
  73. Services: services,
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. if len(reosurces) == 0 {
  79. fmt.Println("No stopped containers")
  80. return nil
  81. }
  82. msg := fmt.Sprintf("Going to remove %s", strings.Join(reosurces, ", "))
  83. if opts.force {
  84. fmt.Println(msg)
  85. } else {
  86. confirm, err := prompt.User{}.Confirm(msg, false)
  87. if err != nil {
  88. return err
  89. }
  90. if !confirm {
  91. return nil
  92. }
  93. }
  94. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  95. _, err = c.ComposeService().Remove(ctx, project, compose.RemoveOptions{
  96. Volumes: opts.volumes,
  97. Force: opts.force,
  98. })
  99. return "", err
  100. })
  101. return err
  102. }