Skip to content

Commit 313b82e

Browse files
committed
ignore services without a build section
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 5e3e217 commit 313b82e

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

pkg/compose/watch.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Trigger struct {
4949

5050
const quietPeriod = 2 * time.Second
5151

52-
func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error {
52+
func (s *composeService) Watch(ctx context.Context, project *types.Project, services []string, options api.WatchOptions) error { //nolint:gocyclo
5353
needRebuild := make(chan string)
5454
needSync := make(chan api.CopyOptions, 5)
5555

@@ -62,27 +62,37 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
6262

6363
eg.Go(s.makeSyncFn(ctx, project, needSync))
6464

65-
err := project.WithServices(services, func(service types.ServiceConfig) error {
65+
ss, err := project.GetServices(services...)
66+
if err != nil {
67+
return err
68+
}
69+
for _, service := range ss {
6670
config, err := loadDevelopmentConfig(service, project)
6771
if err != nil {
6872
return err
6973
}
74+
name := service.Name
7075
if service.Build == nil {
71-
return errors.New("can't watch a service without a build section")
76+
if len(services) != 0 || len(config.Watch) != 0 {
77+
// watch explicitly requested on service, but no build section set
78+
return fmt.Errorf("service %s doesn't have a build section", name)
79+
}
80+
logrus.Infof("service %s ignored. Can't watch a service without a build section", name)
81+
continue
7282
}
73-
context := service.Build.Context
83+
bc := service.Build.Context
7484

75-
ignore, err := watch.LoadDockerIgnore(context)
85+
ignore, err := watch.LoadDockerIgnore(bc)
7686
if err != nil {
7787
return err
7888
}
7989

80-
watcher, err := watch.NewWatcher([]string{context}, ignore)
90+
watcher, err := watch.NewWatcher([]string{bc}, ignore)
8191
if err != nil {
8292
return err
8393
}
8494

85-
fmt.Fprintf(s.stderr(), "watching %s\n", context)
95+
fmt.Fprintf(s.stderr(), "watching %s\n", bc)
8696
err = watcher.Start()
8797
if err != nil {
8898
return err
@@ -113,11 +123,11 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
113123
dest := filepath.Join(trigger.Target, rel)
114124
needSync <- api.CopyOptions{
115125
Source: path,
116-
Destination: fmt.Sprintf("%s:%s", service.Name, dest),
126+
Destination: fmt.Sprintf("%s:%s", name, dest),
117127
}
118128
case WatchActionRebuild:
119129
logrus.Debugf("modified file %s require image to be rebuilt", path)
120-
needRebuild <- service.Name
130+
needRebuild <- name
121131
default:
122132
return fmt.Errorf("watch action %q is not supported", trigger)
123133
}
@@ -126,17 +136,13 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
126136
}
127137

128138
// default
129-
needRebuild <- service.Name
139+
needRebuild <- name
130140

131141
case err := <-watcher.Errors():
132142
return err
133143
}
134144
}
135145
})
136-
return nil
137-
})
138-
if err != nil {
139-
return err
140146
}
141147

142148
return eg.Wait()

0 commit comments

Comments
 (0)