mirror of https://github.com/docker/compose.git
check service existence in project
Signed-off-by: Mehrad Dadar <mehrad.dadar@gmail.com>
This commit is contained in:
parent
32d44dfc25
commit
c0465616bb
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue