check service existence in project

Signed-off-by: Mehrad Dadar <mehrad.dadar@gmail.com>
This commit is contained in:
Mehrad Dadar 2022-02-24 23:46:07 +03:30
parent 32d44dfc25
commit c0465616bb
5 changed files with 26 additions and 7 deletions

View File

@ -24,6 +24,7 @@ import (
"strings"
"github.com/docker/compose/v2/pkg/api"
"github.com/pkg/errors"
"github.com/compose-spec/compose-go/types"
"github.com/docker/cli/cli/config/configfile"
@ -94,12 +95,12 @@ func escapeDollarSign(marshal []byte) []byte {
}
// projectFromName builds a types.Project based on actual resources with compose labels set
func (s *composeService) projectFromName(containers Containers, projectName string) *types.Project {
func (s *composeService) projectFromName(containers Containers, projectName string, services ...string) (*types.Project, error) {
project := &types.Project{
Name: projectName,
}
if len(containers) == 0 {
return project
return project, nil
}
set := map[string]types.ServiceConfig{}
for _, c := range containers {
@ -130,6 +131,15 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
}
project.Services = append(project.Services, service)
}
SERVICES:
for _, qs := range services {
for _, es := range project.Services {
if es.Name == qs {
continue SERVICES
}
}
return project, errors.New("no such service: " + qs)
}
return project
return project, nil
}

View File

@ -235,7 +235,7 @@ func (s *composeService) removeContainers(ctx context.Context, w progress.Writer
func (s *composeService) getProjectWithVolumes(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
containers = containers.filter(isNotOneOff)
project := s.projectFromName(containers, projectName)
project, _ := s.projectFromName(containers, projectName)
volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
if err != nil {
return nil, err

View File

@ -39,7 +39,10 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
return err
}
project := s.projectFromName(observedState, projectName)
project, err := s.projectFromName(observedState, projectName, options.Services...)
if err != nil {
return err
}
if len(options.Services) == 0 {
options.Services = project.ServiceNames()

View File

@ -41,7 +41,10 @@ func (s *composeService) start(ctx context.Context, projectName string, options
return err
}
project := s.projectFromName(containers, projectName)
project, err := s.projectFromName(containers, projectName, options.AttachTo...)
if err != nil {
return err
}
eg, ctx := errgroup.WithContext(ctx)
if listener != nil {

View File

@ -43,7 +43,10 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
return err
}
project := s.projectFromName(containers, projectName)
project, err := s.projectFromName(containers, projectName, services...)
if err != nil {
return err
}
return InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
return s.stopContainers(ctx, w, containers.filter(isService(service)), options.Timeout)