containers.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/compose-spec/compose-go/types"
  17. moby "github.com/docker/docker/api/types"
  18. "github.com/docker/docker/api/types/filters"
  19. )
  20. // Containers is a set of moby Container
  21. type Containers []moby.Container
  22. func (s *composeService) getContainers(ctx context.Context, project *types.Project) (Containers, error) {
  23. var containers Containers
  24. containers, err := s.apiClient.ContainerList(ctx, moby.ContainerListOptions{
  25. Filters: filters.NewArgs(
  26. projectFilter(project.Name),
  27. ),
  28. All: true,
  29. })
  30. if err != nil {
  31. return nil, err
  32. }
  33. containers = containers.filter(isService(project.ServiceNames()...))
  34. return containers, nil
  35. }
  36. // containerPredicate define a predicate we want container to satisfy for filtering operations
  37. type containerPredicate func(c moby.Container) bool
  38. func isService(services ...string) containerPredicate {
  39. return func(c moby.Container) bool {
  40. service := c.Labels[serviceLabel]
  41. return contains(services, service)
  42. }
  43. }
  44. func isNotService(services ...string) containerPredicate {
  45. return func(c moby.Container) bool {
  46. service := c.Labels[serviceLabel]
  47. return !contains(services, service)
  48. }
  49. }
  50. // filter return Containers with elements to match predicate
  51. func (containers Containers) filter(predicate containerPredicate) Containers {
  52. var filtered Containers
  53. for _, c := range containers {
  54. if predicate(c) {
  55. filtered = append(filtered, c)
  56. }
  57. }
  58. return filtered
  59. }
  60. // split return Containers with elements to match and those not to match predicate
  61. func (containers Containers) split(predicate containerPredicate) (Containers, Containers) {
  62. var right Containers
  63. var left Containers
  64. for _, c := range containers {
  65. if predicate(c) {
  66. right = append(right, c)
  67. } else {
  68. left = append(left, c)
  69. }
  70. }
  71. return right, left
  72. }
  73. func (containers Containers) names() []string {
  74. var names []string
  75. for _, c := range containers {
  76. names = append(names, getCanonicalContainerName(c))
  77. }
  78. return names
  79. }
  80. func (containers Containers) forEach(fn func(moby.Container)) {
  81. for _, c := range containers {
  82. fn(c)
  83. }
  84. }