down.go 3.0 KB

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