@@ -6,6 +6,7 @@ package dep
6
6
7
7
import (
8
8
"bytes"
9
+ "encoding/hex"
9
10
"encoding/json"
10
11
"fmt"
11
12
"io/ioutil"
@@ -30,10 +31,10 @@ type SafeWriter struct {
30
31
31
32
// SafeWriterPayload represents the actions SafeWriter will execute when SafeWriter.Write is called.
32
33
type SafeWriterPayload struct {
33
- Manifest * Manifest
34
- Lock * Lock
35
- LockDiff * LockDiff
36
- ForceWriteVendor bool
34
+ Manifest * Manifest
35
+ Lock * Lock
36
+ LockDiff * LockDiff
37
+ WriteVendor bool
37
38
}
38
39
39
40
func (payload * SafeWriterPayload ) HasLock () bool {
@@ -45,10 +46,7 @@ func (payload *SafeWriterPayload) HasManifest() bool {
45
46
}
46
47
47
48
func (payload * SafeWriterPayload ) HasVendor () bool {
48
- // TODO(carolynvs) this can be calculated based on if we are writing the lock
49
- // init -> switch to newlock
50
- // ensure checks existence, why not move that into the prep?
51
- return payload .ForceWriteVendor
49
+ return payload .WriteVendor
52
50
}
53
51
54
52
// LockDiff is the set of differences between an existing lock file and an updated lock file.
@@ -68,6 +66,10 @@ func (diff *LockDiff) Format() (string, error) {
68
66
69
67
var buf bytes.Buffer
70
68
69
+ if diff .HashDiff != nil {
70
+ buf .WriteString (fmt .Sprintf ("Memo: %s\n " , diff .HashDiff ))
71
+ }
72
+
71
73
if len (diff .Add ) > 0 {
72
74
buf .WriteString ("Add: " )
73
75
@@ -124,63 +126,80 @@ type StringDiff struct {
124
126
Current string
125
127
}
126
128
127
- func (diff StringDiff ) MarshalJSON () ([]byte , error ) {
128
- var value string
129
-
129
+ func (diff StringDiff ) String () string {
130
130
if diff .Previous == "" && diff .Current != "" {
131
- value = fmt .Sprintf ("+ %s" , diff .Current )
132
- } else if diff .Previous != "" && diff .Current == "" {
133
- value = fmt .Sprintf ("- %s" , diff .Previous )
134
- } else if diff .Previous != diff .Current {
135
- value = fmt .Sprintf ("%s -> %s" , diff .Previous , diff .Current )
136
- } else {
137
- value = diff .Current
131
+ return fmt .Sprintf ("+ %s" , diff .Current )
132
+ }
133
+
134
+ if diff .Previous != "" && diff .Current == "" {
135
+ return fmt .Sprintf ("- %s" , diff .Previous )
136
+ }
137
+
138
+ if diff .Previous != diff .Current {
139
+ return fmt .Sprintf ("%s -> %s" , diff .Previous , diff .Current )
138
140
}
139
141
142
+ return diff .Current
143
+ }
144
+
145
+ func (diff StringDiff ) MarshalJSON () ([]byte , error ) {
140
146
var buf bytes.Buffer
141
147
enc := json .NewEncoder (& buf )
142
148
enc .SetEscapeHTML (false )
143
- err := enc .Encode (value )
149
+ err := enc .Encode (diff . String () )
144
150
145
151
return buf .Bytes (), err
146
152
}
147
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
+
148
166
// Prepare to write a set of config yaml, lock and vendor tree.
149
167
//
150
168
// - If manifest is provided, it will be written to the standard manifest file
151
169
// name beneath root.
152
- // - If lock is provided it will be written to the standard
153
- // lock file name in the root dir, but vendor will NOT be written
154
- // - If lock and newLock are both provided and are equivalent, then neither lock
155
- // nor vendor will be written
156
- // - If lock and newLock are both provided and are not equivalent,
157
- // the newLock will be written to the same location as above, and a vendor
158
- // tree will be written to the vendor directory
159
- // - If newLock is provided and lock is not, it will write both a lock
160
- // and the vendor directory in the same way
161
- // - If the forceVendor param is true, then vendor/ will be unconditionally
162
- // written out based on newLock if present, else lock, else error.
163
- func (sw * SafeWriter ) Prepare (manifest * Manifest , lock * 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 {
164
177
sw .Payload = & SafeWriterPayload {
165
- Manifest : manifest ,
166
- ForceWriteVendor : forceVendor ,
178
+ Manifest : manifest ,
179
+ Lock : newLock ,
167
180
}
168
181
169
- if newLock != nil {
170
- if lock == nil {
171
- sw .Payload .Lock = newLock
172
- sw .Payload .ForceWriteVendor = true
173
- } else {
174
- diff := diffLocks (lock , newLock )
175
- if diff != nil {
176
- sw .Payload .Lock = newLock
177
- sw .Payload .LockDiff = diff
178
- sw .Payload .ForceWriteVendor = true
179
- }
182
+ if oldLock != nil {
183
+ if newLock == nil {
184
+ return errors .New ("must provide newLock when oldLock is specified" )
185
+ }
186
+ sw .Payload .LockDiff = diffLocks (oldLock , newLock )
187
+ }
188
+
189
+ switch vendor {
190
+ case VendorAlways :
191
+ sw .Payload .WriteVendor = true
192
+ case VendorOnChanged :
193
+ if sw .Payload .LockDiff != nil || (newLock != nil && oldLock == nil ) {
194
+ sw .Payload .WriteVendor = true
180
195
}
181
- } else if lock != nil {
182
- sw .Payload .Lock = lock
183
196
}
197
+
198
+ if sw .Payload .WriteVendor && newLock == nil {
199
+ return errors .New ("must provide newLock in order to write out vendor" )
200
+ }
201
+
202
+ return nil
184
203
}
185
204
186
205
func (payload SafeWriterPayload ) validate (root string , sm gps.SourceManager ) error {
@@ -198,10 +217,6 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err
198
217
return errors .New ("must provide a SourceManager if writing out a vendor dir" )
199
218
}
200
219
201
- if payload .HasVendor () && payload .Lock == nil {
202
- return errors .New ("must provide a lock in order to write out vendor" )
203
- }
204
-
205
220
return nil
206
221
}
207
222
@@ -358,12 +373,21 @@ func (sw *SafeWriter) PrintPreparedActions() error {
358
373
}
359
374
360
375
if sw .Payload .HasLock () {
361
- fmt .Println ("Would have written the following changes to lock.json:" )
362
- diff , err := sw .Payload .LockDiff .Format ()
363
- if err != nil {
364
- return errors .Wrap (err , "ensure DryRun cannot serialize the lock diff" )
376
+ if sw .Payload .LockDiff == nil {
377
+ fmt .Println ("Would have written the following lock.json:" )
378
+ l , err := sw .Payload .Lock .MarshalJSON ()
379
+ if err != nil {
380
+ return errors .Wrap (err , "ensure DryRun cannot serialize lock" )
381
+ }
382
+ fmt .Println (string (l ))
383
+ } else {
384
+ fmt .Println ("Would have written the following changes to lock.json:" )
385
+ diff , err := sw .Payload .LockDiff .Format ()
386
+ if err != nil {
387
+ return errors .Wrap (err , "ensure DryRun cannot serialize the lock diff" )
388
+ }
389
+ fmt .Println (diff )
365
390
}
366
- fmt .Println (diff )
367
391
}
368
392
369
393
if sw .Payload .HasVendor () {
@@ -413,10 +437,10 @@ func diffLocks(l1 gps.Lock, l2 gps.Lock) *LockDiff {
413
437
414
438
diff := LockDiff {}
415
439
416
- h1 := l1 .InputHash ()
417
- h2 := l2 .InputHash ()
418
- if ! bytes . Equal ( h1 , h2 ) {
419
- diff .HashDiff = & StringDiff {Previous : string ( h1 ) , Current : string ( h2 ) }
440
+ h1 := hex . EncodeToString ( l1 .InputHash () )
441
+ h2 := hex . EncodeToString ( l2 .InputHash () )
442
+ if h1 != h2 {
443
+ diff .HashDiff = & StringDiff {Previous : h1 , Current : h2 }
420
444
}
421
445
422
446
var i2next int
0 commit comments