MCPcopy
hub / github.com/docker/compose / projectFromName

Method projectFromName

pkg/compose/compose.go:359–425  ·  pkg/compose/compose.go::composeService.projectFromName

projectFromName builds a types.Project based on actual resources with compose labels set

(containers Containers, projectName string, services ...string)

Source from the content-addressed store, hash-verified

357
358// projectFromName builds a types.Project based on actual resources with compose labels set
359func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
360 project := &types.Project{
361 Name: projectName,
362 Services: types.Services{},
363 }
364 if len(containers) == 0 {
365 return project, fmt.Errorf("no container found for project %q: %w", projectName, api.ErrNotFound)
366 }
367 set := types.Services{}
368 for _, c := range containers {
369 serviceLabel, ok := c.Labels[api.ServiceLabel]
370 if !ok {
371 serviceLabel = getCanonicalContainerName(c)
372 }
373 service, ok := set[serviceLabel]
374 if !ok {
375 service = types.ServiceConfig{
376 Name: serviceLabel,
377 Image: c.Image,
378 Labels: c.Labels,
379 }
380 }
381 service.Scale = increment(service.Scale)
382 set[serviceLabel] = service
383 }
384 for name, service := range set {
385 dependencies := service.Labels[api.DependenciesLabel]
386 if dependencies != "" {
387 service.DependsOn = types.DependsOnConfig{}
388 for dc := range strings.SplitSeq(dependencies, ",") {
389 dcArr := strings.Split(dc, ":")
390 condition := ServiceConditionRunningOrHealthy
391 // Let's restart the dependency by default if we don't have the info stored in the label
392 restart := true
393 required := true
394 dependency := dcArr[0]
395
396 // backward compatibility
397 if len(dcArr) > 1 {
398 condition = dcArr[1]
399 if len(dcArr) > 2 {
400 restart, _ = strconv.ParseBool(dcArr[2])
401 }
402 }
403 service.DependsOn[dependency] = types.ServiceDependency{Condition: condition, Restart: restart, Required: required}
404 }
405 set[name] = service
406 }
407 }
408 project.Services = set
409
410SERVICES:
411 for _, qs := range services {
412 for _, es := range project.Services {
413 if es.Name == qs {
414 continue SERVICES
415 }
416 }

Callers 2

startMethod · 0.95

Calls 2

incrementFunction · 0.85

Tested by

no test coverage detected