@@ -28,12 +28,23 @@ import (
28
28
"github.com/jonboulle/clockwork"
29
29
"github.com/mitchellh/mapstructure"
30
30
"github.com/pkg/errors"
31
+ "github.com/sirupsen/logrus"
31
32
"golang.org/x/sync/errgroup"
32
33
)
33
34
34
35
type DevelopmentConfig struct {
35
- Sync map [string ]string `json:"sync,omitempty"`
36
- Excludes []string `json:"excludes,omitempty"`
36
+ Watch []Trigger `json:"watch,omitempty"`
37
+ }
38
+
39
+ const (
40
+ WatchActionSync = "sync"
41
+ WatchActionRebuild = "rebuild"
42
+ )
43
+
44
+ type Trigger struct {
45
+ Path string `json:"path,omitempty"`
46
+ Action string `json:"action,omitempty"`
47
+ Target string `json:"target,omitempty"`
37
48
}
38
49
39
50
const quietPeriod = 2 * time .Second
@@ -85,26 +96,38 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv
85
96
case <- ctx .Done ():
86
97
return nil
87
98
case event := <- watcher .Events ():
88
- fmt .Fprintf (s .stderr (), "change detected on %s\n " , event .Path ())
89
-
90
- for src , dest := range config .Sync {
91
- path := filepath .Clean (event .Path ())
92
- src = filepath .Clean (src )
93
- if watch .IsChild (path , src ) {
94
- rel , err := filepath .Rel (src , path )
95
- if err != nil {
96
- return err
97
- }
98
- dest = filepath .Join (dest , rel )
99
- needSync <- api.CopyOptions {
100
- Source : path ,
101
- Destination : fmt .Sprintf ("%s:%s" , service .Name , dest ),
99
+ path := event .Path ()
100
+
101
+ for _ , trigger := range config .Watch {
102
+ logrus .Debugf ("change deteced on %s - comparing with %s" , path , trigger .Path )
103
+ if watch .IsChild (trigger .Path , path ) {
104
+ fmt .Fprintf (s .stderr (), "change detected on %s\n " , path )
105
+
106
+ switch trigger .Action {
107
+ case WatchActionSync :
108
+ logrus .Debugf ("modified file %s triggered sync" , path )
109
+ rel , err := filepath .Rel (trigger .Path , path )
110
+ if err != nil {
111
+ return err
112
+ }
113
+ dest := filepath .Join (trigger .Target , rel )
114
+ needSync <- api.CopyOptions {
115
+ Source : path ,
116
+ Destination : fmt .Sprintf ("%s:%s" , service .Name , dest ),
117
+ }
118
+ case WatchActionRebuild :
119
+ logrus .Debugf ("modified file %s require image to be rebuilt" , path )
120
+ needRebuild <- service .Name
121
+ default :
122
+ return fmt .Errorf ("watch action %q is not supported" , trigger )
102
123
}
103
124
continue WATCH
104
125
}
105
126
}
106
127
128
+ // default
107
129
needRebuild <- service .Name
130
+
108
131
case err := <- watcher .Errors ():
109
132
return err
110
133
}
@@ -124,14 +147,17 @@ func loadDevelopmentConfig(service types.ServiceConfig, project *types.Project)
124
147
if y , ok := service .Extensions ["x-develop" ]; ok {
125
148
err := mapstructure .Decode (y , & config )
126
149
if err != nil {
127
- return DevelopmentConfig {} , err
150
+ return config , err
128
151
}
129
- for src , dest := range config .Sync {
130
- if ! filepath .IsAbs (src ) {
131
- delete (config .Sync , src )
132
- src = filepath .Join (project .WorkingDir , src )
133
- config .Sync [src ] = dest
152
+ for i , trigger := range config .Watch {
153
+ if ! filepath .IsAbs (trigger .Path ) {
154
+ trigger .Path = filepath .Join (project .WorkingDir , trigger .Path )
155
+ }
156
+ trigger .Path = filepath .Clean (trigger .Path )
157
+ if trigger .Path == "" {
158
+ return config , errors .New ("watch rules MUST define a path" )
134
159
}
160
+ config .Watch [i ] = trigger
135
161
}
136
162
}
137
163
return config , nil
0 commit comments