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

Commit

Permalink
ensure: tweaks to align no-vendor behavior and verbose/dry-run logging (
Browse files Browse the repository at this point in the history
#1039)

* ensure: tweaks to align no-vendor behavior and verbose/dry-run logging
* ensure: dry-run: always log lock diff
* log PrintPreparedActions to ctx Out not Err
  • Loading branch information
jmank88 authored and ibrasho committed Aug 21, 2017
1 parent 1bef80e commit d895a66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
32 changes: 14 additions & 18 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,15 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project

if p.Lock != nil && bytes.Equal(p.Lock.InputHash(), solver.HashInputs()) {
// Memo matches, so there's probably nothing to do.
if ctx.Verbose {
ctx.Out.Printf("%s was already in sync with imports and %s\n", dep.LockName, dep.ManifestName)
}

if cmd.noVendor {
// The user said not to touch vendor/, so definitely nothing to do.
return nil
}

if ctx.Verbose {
ctx.Out.Printf("%s was already in sync with imports and %s, recreating vendor/ directory", dep.LockName, dep.ManifestName)
}

// TODO(sdboyer) The desired behavior at this point is to determine
// whether it's necessary to write out vendor, or if it's already
// consistent with the lock. However, we haven't yet determined what
Expand All @@ -244,8 +244,7 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
}

if cmd.dryRun {
ctx.Out.Printf("Would have populated vendor/ directory from %s", dep.LockName)
return nil
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
Expand All @@ -261,12 +260,16 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
return errors.Wrap(err, "ensure Solve()")
}

sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), dep.VendorOnChanged)
vendorBehavior := dep.VendorOnChanged
if cmd.noVendor {
vendorBehavior = dep.VendorNever
}
sw, err := dep.NewSafeWriter(nil, p.Lock, dep.LockFromSolution(solution), vendorBehavior)
if err != nil {
return err
}
if cmd.dryRun {
return sw.PrintPreparedActions(ctx.Out)
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
Expand All @@ -292,14 +295,7 @@ func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Proj
}

if cmd.dryRun {
ctx.Out.Printf("Would have populated vendor/ directory from %s", dep.LockName)
if ctx.Verbose {
err := sw.PrintPreparedActions(ctx.Err)
if err != nil {
return errors.WithMessage(err, "prepared actions")
}
}
return nil
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
Expand Down Expand Up @@ -394,7 +390,7 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
return err
}
if cmd.dryRun {
return sw.PrintPreparedActions(ctx.Out)
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
Expand Down Expand Up @@ -648,7 +644,7 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
}

if cmd.dryRun {
return sw.PrintPreparedActions(ctx.Out)
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
Expand Down
38 changes: 24 additions & 14 deletions txn_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,24 +418,30 @@ fail:
}

// PrintPreparedActions logs the actions a call to Write would perform.
func (sw *SafeWriter) PrintPreparedActions(output *log.Logger) error {
func (sw *SafeWriter) PrintPreparedActions(output *log.Logger, verbose bool) error {
if sw.HasManifest() {
output.Printf("Would have written the following %s:\n", ManifestName)
m, err := sw.Manifest.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
if verbose {
m, err := sw.Manifest.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize manifest")
}
output.Printf("Would have written the following %s:\n%s\n", ManifestName, string(m))
} else {
output.Printf("Would have written %s.\n", ManifestName)
}
output.Println(string(m))
}

if sw.writeLock {
if sw.lockDiff == nil {
output.Printf("Would have written the following %s:\n", LockName)
l, err := sw.lock.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
if verbose {
l, err := sw.lock.MarshalTOML()
if err != nil {
return errors.Wrap(err, "ensure DryRun cannot serialize lock")
}
output.Printf("Would have written the following %s:\n%s\n", LockName, string(l))
} else {
output.Printf("Would have written %s.\n", LockName)
}
output.Println(string(l))
} else {
output.Printf("Would have written the following changes to %s:\n", LockName)
diff, err := formatLockDiff(*sw.lockDiff)
Expand All @@ -447,9 +453,13 @@ func (sw *SafeWriter) PrintPreparedActions(output *log.Logger) error {
}

if sw.writeVendor {
output.Println("Would have written the following projects to the vendor directory:")
for _, project := range sw.lock.Projects() {
output.Println(project)
if verbose {
output.Printf("Would have written the following %d projects to the vendor directory:\n", len(sw.lock.Projects()))
for _, project := range sw.lock.Projects() {
output.Println(project)
}
} else {
output.Printf("Would have written %d projects to the vendor directory.\n", len(sw.lock.Projects()))
}
}

Expand Down

0 comments on commit d895a66

Please sign in to comment.