refactor: removed redundant code

Signed-off-by: Mehrad Dadar <mehrad.dadar@gmail.com>
This commit is contained in:
Mehrad Dadar 2022-02-17 07:55:11 +03:30
parent 65ed8cf4c2
commit ced9eba940
3 changed files with 8 additions and 33 deletions

View File

@ -115,7 +115,7 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
if len(dependencies) > 0 {
service.DependsOn = types.DependsOnConfig{}
for _, d := range strings.Split(dependencies, ",") {
service.DependsOn[d] = types.ServiceDependency{}
service.DependsOn[d] = types.ServiceDependency{Condition: ServiceConditionRunningOrHealthy}
}
}
project.Services = append(project.Services, service)

View File

@ -41,6 +41,7 @@ func (s *composeService) Down(ctx context.Context, projectName string, options a
}
func (s *composeService) down(ctx context.Context, projectName string, options api.DownOptions) error {
builtFromResources := options.Project == nil
w := progress.ContextWriter(ctx)
resourceToRemove := false
@ -50,8 +51,8 @@ func (s *composeService) down(ctx context.Context, projectName string, options a
return err
}
if options.Project == nil {
options.Project, err = s.projectFromLabels(ctx, containers.filter(isNotOneOff), projectName)
if builtFromResources {
options.Project, err = s.getProjectWithVolumes(ctx, containers, projectName)
if err != nil {
return err
}
@ -232,34 +233,9 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
return eg.Wait()
}
// projectFromLabels builds a types.Project based on actual resources with compose labels set
func (s *composeService) projectFromLabels(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
project := &types.Project{
Name: projectName,
}
if len(containers) == 0 {
return project, nil
}
set := map[string]moby.Container{}
for _, c := range containers {
set[c.Labels[api.ServiceLabel]] = c
}
for s, c := range set {
service := types.ServiceConfig{
Name: s,
Image: c.Image,
Labels: c.Labels,
}
dependencies := c.Labels[api.DependenciesLabel]
if len(dependencies) > 0 {
service.DependsOn = types.DependsOnConfig{}
for _, d := range strings.Split(dependencies, ",") {
service.DependsOn[d] = types.ServiceDependency{}
}
}
project.Services = append(project.Services, service)
}
func (s *composeService) getProjectWithVolumes(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
containers = containers.filter(isNotOneOff)
project := s.projectFromName(containers, projectName)
volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
if err != nil {
return nil, err
@ -273,6 +249,5 @@ func (s *composeService) projectFromLabels(ctx context.Context, containers Conta
Labels: vol.Labels,
}
}
return project, nil
}

View File

@ -36,7 +36,7 @@ func (s *composeService) Start(ctx context.Context, projectName string, options
func (s *composeService) start(ctx context.Context, projectName string, options api.StartOptions, listener api.ContainerEventListener) error {
var containers Containers
containers, err := s.getContainers(ctx, projectName, oneOffInclude, true, options.AttachTo...)
containers, err := s.getContainers(ctx, projectName, oneOffExclude, true, options.AttachTo...)
if err != nil {
return err
}