containers.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "sort"
  18. "github.com/compose-spec/compose-go/types"
  19. moby "github.com/docker/docker/api/types"
  20. "github.com/docker/docker/api/types/filters"
  21. "github.com/docker/compose-cli/utils"
  22. )
  23. // Containers is a set of moby Container
  24. type Containers []moby.Container
  25. type oneOff int
  26. const (
  27. oneOffInclude = oneOff(iota)
  28. oneOffExclude
  29. oneOffOnly
  30. )
  31. func (s *composeService) getContainers(ctx context.Context, project *types.Project, oneOff oneOff, selectedServices []string) (Containers, error) {
  32. var containers Containers
  33. f := filters.NewArgs(
  34. projectFilter(project.Name),
  35. )
  36. switch oneOff {
  37. case oneOffOnly:
  38. f.Add("label", fmt.Sprintf("%s=%s", oneoffLabel, "True"))
  39. case oneOffExclude:
  40. f.Add("label", fmt.Sprintf("%s=%s", oneoffLabel, "False"))
  41. case oneOffInclude:
  42. }
  43. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  44. Filters: f,
  45. All: true,
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. if len(selectedServices) == 0 {
  51. selectedServices = project.ServiceNames()
  52. }
  53. containers = containers.filter(isService(selectedServices...))
  54. return containers, nil
  55. }
  56. // containerPredicate define a predicate we want container to satisfy for filtering operations
  57. type containerPredicate func(c moby.Container) bool
  58. func isService(services ...string) containerPredicate {
  59. return func(c moby.Container) bool {
  60. service := c.Labels[serviceLabel]
  61. return utils.StringContains(services, service)
  62. }
  63. }
  64. func isNotService(services ...string) containerPredicate {
  65. return func(c moby.Container) bool {
  66. service := c.Labels[serviceLabel]
  67. return !utils.StringContains(services, service)
  68. }
  69. }
  70. // filter return Containers with elements to match predicate
  71. func (containers Containers) filter(predicate containerPredicate) Containers {
  72. var filtered Containers
  73. for _, c := range containers {
  74. if predicate(c) {
  75. filtered = append(filtered, c)
  76. }
  77. }
  78. return filtered
  79. }
  80. func (containers Containers) names() []string {
  81. var names []string
  82. for _, c := range containers {
  83. names = append(names, getCanonicalContainerName(c))
  84. }
  85. return names
  86. }
  87. func (containers Containers) forEach(fn func(moby.Container)) {
  88. for _, c := range containers {
  89. fn(c)
  90. }
  91. }
  92. func (containers Containers) sorted() Containers {
  93. sort.Slice(containers, func(i, j int) bool {
  94. return getCanonicalContainerName(containers[i]) < getCanonicalContainerName(containers[j])
  95. })
  96. return containers
  97. }