mirror of
https://github.com/docker/compose.git
synced 2025-07-27 07:34:10 +02:00
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"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/compose/v2/pkg/api"
|
"github.com/docker/compose/v2/pkg/api"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
"github.com/docker/cli/cli/config/configfile"
|
"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
|
// 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{
|
project := &types.Project{
|
||||||
Name: projectName,
|
Name: projectName,
|
||||||
}
|
}
|
||||||
if len(containers) == 0 {
|
if len(containers) == 0 {
|
||||||
return project
|
return project, nil
|
||||||
}
|
}
|
||||||
set := map[string]types.ServiceConfig{}
|
set := map[string]types.ServiceConfig{}
|
||||||
for _, c := range containers {
|
for _, c := range containers {
|
||||||
@ -130,6 +131,15 @@ func (s *composeService) projectFromName(containers Containers, projectName stri
|
|||||||
}
|
}
|
||||||
project.Services = append(project.Services, service)
|
project.Services = append(project.Services, service)
|
||||||
}
|
}
|
||||||
|
SERVICES:
|
||||||
return project
|
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, 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) {
|
func (s *composeService) getProjectWithVolumes(ctx context.Context, containers Containers, projectName string) (*types.Project, error) {
|
||||||
containers = containers.filter(isNotOneOff)
|
containers = containers.filter(isNotOneOff)
|
||||||
project := s.projectFromName(containers, projectName)
|
project, _ := s.projectFromName(containers, projectName)
|
||||||
volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
|
volumes, err := s.apiClient.VolumeList(ctx, filters.NewArgs(projectFilter(projectName)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -39,7 +39,10 @@ func (s *composeService) restart(ctx context.Context, projectName string, option
|
|||||||
return err
|
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 {
|
if len(options.Services) == 0 {
|
||||||
options.Services = project.ServiceNames()
|
options.Services = project.ServiceNames()
|
||||||
|
@ -41,7 +41,10 @@ func (s *composeService) start(ctx context.Context, projectName string, options
|
|||||||
return err
|
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)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
if listener != nil {
|
if listener != nil {
|
||||||
|
@ -43,7 +43,10 @@ func (s *composeService) stop(ctx context.Context, projectName string, options a
|
|||||||
return err
|
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 InReverseDependencyOrder(ctx, project, func(c context.Context, service string) error {
|
||||||
return s.stopContainers(ctx, w, containers.filter(isService(service)), options.Timeout)
|
return s.stopContainers(ctx, w, containers.filter(isService(service)), options.Timeout)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user