(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int)
| 88 | } |
| 89 | |
| 90 | func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (container.Summary, error) { |
| 91 | defaultFilters := getDefaultFilters(projectName, oneOff, serviceName) |
| 92 | if containerIndex > 0 { |
| 93 | defaultFilters.Add("label", containerNumberFilter(containerIndex)) |
| 94 | } |
| 95 | res, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{ |
| 96 | Filters: defaultFilters, |
| 97 | All: all, |
| 98 | }) |
| 99 | if err != nil { |
| 100 | return container.Summary{}, err |
| 101 | } |
| 102 | containers := res.Items |
| 103 | if len(containers) < 1 { |
| 104 | if containerIndex > 0 { |
| 105 | return container.Summary{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex) |
| 106 | } |
| 107 | return container.Summary{}, fmt.Errorf("service %q is not running", serviceName) |
| 108 | } |
| 109 | |
| 110 | // Sort by container number first, then put one-off containers at the end |
| 111 | sort.Slice(containers, func(i, j int) bool { |
| 112 | numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel]) |
| 113 | numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel]) |
| 114 | IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True" |
| 115 | IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True" |
| 116 | |
| 117 | if IsOneOffLabelTrueX || IsOneOffLabelTrueY { |
| 118 | return !IsOneOffLabelTrueX && IsOneOffLabelTrueY |
| 119 | } |
| 120 | |
| 121 | return numberLabelX < numberLabelY |
| 122 | }) |
| 123 | return containers[0], nil |
| 124 | } |
| 125 | |
| 126 | // containerPredicate define a predicate we want container to satisfy for filtering operations |
| 127 | type containerPredicate func(c container.Summary) bool |
no test coverage detected