Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 25ff41c

Browse files
committed
Print exactly what will be written to file during a real ensure run
1 parent fd77463 commit 25ff41c

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

cmd/dep/ensure.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
119119
defer sm.Release()
120120

121121
params := p.MakeParams()
122-
if *verbose || cmd.dryRun {
122+
if *verbose {
123123
params.Trace = true
124124
params.TraceLogger = log.New(os.Stderr, "", 0)
125125
}
@@ -166,12 +166,47 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
166166
writeV := !vendorExists && solution != nil
167167

168168
if cmd.dryRun {
169-
return nil
169+
return printDryRunActions(sw, writeV)
170170
}
171171

172172
return errors.Wrap(sw.WriteAllSafe(writeV), "grouped write of manifest, lock and vendor")
173173
}
174174

175+
func printDryRunActions(sw dep.SafeWriter, writeV bool) error {
176+
if sw.Manifest != nil {
177+
fmt.Println("Would have written the following manifest.json:")
178+
m, err := sw.Manifest.MarshalJSON()
179+
if err != nil {
180+
return errors.Wrap(err, "ensure DryRun cannot read manifest")
181+
}
182+
fmt.Println(string(m))
183+
}
184+
185+
if sw.Lock != nil {
186+
fmt.Println("Would have written the following lock.json:")
187+
m, err := sw.Lock.MarshalJSON()
188+
if err != nil {
189+
return errors.Wrap(err, "ensure DryRun cannot read lock")
190+
}
191+
fmt.Println(string(m))
192+
}
193+
194+
if writeV {
195+
fmt.Println("Would have written the following projects to the vendor directory:")
196+
for _, project := range sw.NewLock.Projects() {
197+
prj := project.Ident()
198+
rev := dep.GetRevisionFromVersion(project.Version())
199+
if prj.Source == "" {
200+
fmt.Printf("%s@%s\n", prj.ProjectRoot, rev)
201+
} else {
202+
fmt.Printf("%s -> %s@%s\n", prj.ProjectRoot, prj.Source, rev)
203+
}
204+
}
205+
}
206+
207+
return nil
208+
}
209+
175210
func applyUpdateArgs(args []string, params *gps.SolveParameters) {
176211
// When -update is specified without args, allow every project to change versions, regardless of the lock file
177212
if len(args) == 0 {

lock.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,7 @@ func (l *Lock) MarshalJSON() ([]byte, error) {
102102
}
103103

104104
v := lp.Version()
105-
// Figure out how to get the underlying revision
106-
switch tv := v.(type) {
107-
case gps.UnpairedVersion:
108-
// TODO we could error here, if we want to be very defensive about not
109-
// allowing a lock to be written if without an immmutable revision
110-
case gps.Revision:
111-
ld.Revision = tv.String()
112-
case gps.PairedVersion:
113-
ld.Revision = tv.Underlying().String()
114-
}
105+
ld.Revision = GetRevisionFromVersion(v)
115106

116107
switch v.Type() {
117108
case gps.IsBranch:
@@ -134,6 +125,20 @@ func (l *Lock) MarshalJSON() ([]byte, error) {
134125
return buf.Bytes(), err
135126
}
136127

128+
func GetRevisionFromVersion(v gps.Version) string {
129+
// Figure out how to get the underlying revision
130+
switch tv := v.(type) {
131+
case gps.UnpairedVersion:
132+
// TODO we could error here, if we want to be very defensive about not
133+
// allowing a lock to be written if without an immmutable revision
134+
case gps.Revision:
135+
return tv.String()
136+
case gps.PairedVersion:
137+
return tv.Underlying().String()
138+
}
139+
return ""
140+
}
141+
137142
// LockFromInterface converts an arbitrary gps.Lock to dep's representation of a
138143
// lock. If the input is already dep's *lock, the input is returned directly.
139144
//

0 commit comments

Comments
 (0)