down.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "time"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/spf13/cobra"
  20. "github.com/docker/compose-cli/api/client"
  21. "github.com/docker/compose-cli/api/compose"
  22. "github.com/docker/compose-cli/api/context/store"
  23. "github.com/docker/compose-cli/api/progress"
  24. )
  25. type downOptions struct {
  26. *projectOptions
  27. removeOrphans bool
  28. timeChanged bool
  29. timeout int
  30. volumes bool
  31. images string
  32. }
  33. func downCommand(p *projectOptions, contextType string) *cobra.Command {
  34. opts := downOptions{
  35. projectOptions: p,
  36. }
  37. downCmd := &cobra.Command{
  38. Use: "down",
  39. Short: "Stop and remove containers, networks",
  40. RunE: func(cmd *cobra.Command, args []string) error {
  41. opts.timeChanged = cmd.Flags().Changed("timeout")
  42. if opts.images != "" {
  43. if opts.images != "all" && opts.images != "local" {
  44. return fmt.Errorf("invalid value for --rmi: %q", opts.images)
  45. }
  46. }
  47. return runDown(cmd.Context(), opts)
  48. },
  49. }
  50. flags := downCmd.Flags()
  51. flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
  52. flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Specify a shutdown timeout in seconds")
  53. switch contextType {
  54. case store.LocalContextType, store.DefaultContextType, store.EcsLocalSimulationContextType:
  55. flags.BoolVarP(&opts.volumes, "volumes", "v", false, " Remove named volumes declared in the `volumes` section of the Compose file and anonymous volumes attached to containers.")
  56. flags.StringVar(&opts.images, "rmi", "", `Remove images used by services. "local" remove only images that don't have a custom tag ("local"|"all")`)
  57. }
  58. return downCmd
  59. }
  60. func runDown(ctx context.Context, opts downOptions) error {
  61. c, err := client.New(ctx)
  62. if err != nil {
  63. return err
  64. }
  65. _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
  66. name := opts.ProjectName
  67. var project *types.Project
  68. if opts.ProjectName == "" {
  69. p, err := opts.toProject(nil)
  70. if err != nil {
  71. return "", err
  72. }
  73. project = p
  74. name = p.Name
  75. }
  76. var timeout *time.Duration
  77. if opts.timeChanged {
  78. timeoutValue := time.Duration(opts.timeout) * time.Second
  79. timeout = &timeoutValue
  80. }
  81. return name, c.ComposeService().Down(ctx, name, compose.DownOptions{
  82. RemoveOrphans: opts.removeOrphans,
  83. Project: project,
  84. Timeout: timeout,
  85. Images: opts.images,
  86. Volumes: opts.volumes,
  87. })
  88. })
  89. return err
  90. }