@@ -151,39 +151,55 @@ func (diff StringDiff) MarshalJSON() ([]byte, error) {
151
151
return buf .Bytes (), err
152
152
}
153
153
154
+ // VendorBehavior defines when the vendor directory should be written.
155
+ type VendorBehavior int
156
+
157
+ const (
158
+ // VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
159
+ VendorOnChanged VendorBehavior = iota
160
+ // VendorAlways forces the vendor directory to always be written.
161
+ VendorAlways
162
+ // VendorNever indicates the vendor directory should never be written.
163
+ VendorNever
164
+ )
165
+
154
166
// Prepare to write a set of config yaml, lock and vendor tree.
155
167
//
156
168
// - If manifest is provided, it will be written to the standard manifest file
157
169
// name beneath root.
158
- // - If lock is provided it will be written to the standard
159
- // lock file name in the root dir, but vendor will NOT be written
160
- // - If lock and newLock are both provided and are equivalent, then neither lock
161
- // nor vendor will be written
162
- // - If lock and newLock are both provided and are not equivalent,
163
- // the newLock will be written to the same location as above, and a vendor
164
- // tree will be written to the vendor directory
165
- // - If newLock is provided and lock is not, it will write both a lock
166
- // and the vendor directory in the same way
167
- // - If the forceVendor param is true, then vendor/ will be unconditionally
168
- // written out based on newLock if present, else lock, else error.
169
- func (sw * SafeWriter ) Prepare (manifest * Manifest , oldLock * Lock , newLock * Lock , forceVendor bool ) {
170
+ // - If newLock is provided, it will be written to the standard lock file
171
+ // name beneath root.
172
+ // - If vendor is VendorAlways, or is VendorOnChanged and the locks are different,
173
+ // the vendor directory will be written beneath root based on newLock.
174
+ // - If oldLock is provided without newLock, error.
175
+ // - If vendor is VendorAlways without a newLock, error.
176
+ func (sw * SafeWriter ) Prepare (manifest * Manifest , oldLock , newLock * Lock , vendor VendorBehavior ) error {
170
177
sw .Payload = & SafeWriterPayload {
171
178
Manifest : manifest ,
179
+ Lock : newLock ,
172
180
}
173
181
174
- if oldLock != nil && newLock != nil {
182
+ if oldLock != nil {
183
+ if newLock == nil {
184
+ return errors .New ("must provide newLock when oldLock is specified" )
185
+ }
175
186
sw .Payload .LockDiff = diffLocks (oldLock , newLock )
176
187
}
177
188
178
- if forceVendor || sw . Payload . LockDiff != nil {
179
- sw . Payload . Lock = newLock
189
+ switch vendor {
190
+ case VendorAlways :
180
191
sw .Payload .WriteVendor = true
192
+ case VendorOnChanged :
193
+ if sw .Payload .LockDiff != nil || (newLock != nil && oldLock == nil ) {
194
+ sw .Payload .WriteVendor = true
195
+ }
181
196
}
182
197
183
- if oldLock == nil && newLock != nil {
184
- sw .Payload .Lock = newLock
185
- sw .Payload .WriteVendor = true
198
+ if sw .Payload .WriteVendor && newLock == nil {
199
+ return errors .New ("must provide newLock in order to write out vendor" )
186
200
}
201
+
202
+ return nil
187
203
}
188
204
189
205
func (payload SafeWriterPayload ) validate (root string , sm gps.SourceManager ) error {
@@ -201,10 +217,6 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err
201
217
return errors .New ("must provide a SourceManager if writing out a vendor dir" )
202
218
}
203
219
204
- if payload .HasVendor () && payload .Lock == nil {
205
- return errors .New ("must provide a lock in order to write out vendor" )
206
- }
207
-
208
220
return nil
209
221
}
210
222
0 commit comments