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

log.Logger api changes #1617

Merged
merged 2 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -288,9 +287,9 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
return errors.WithMessage(sw.Write(p.AbsRoot, sm, true, logger), "grouped write of manifest, lock and vendor")
}
Expand All @@ -312,9 +311,9 @@ func (cmd *ensureCommand) runDefault(ctx *dep.Ctx, args []string, p *dep.Project
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
return errors.Wrap(sw.Write(p.AbsRoot, sm, false, logger), "grouped write of manifest, lock and vendor")
}
Expand All @@ -338,9 +337,9 @@ func (cmd *ensureCommand) runVendorOnly(ctx *dep.Ctx, args []string, p *dep.Proj
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
return errors.WithMessage(sw.Write(p.AbsRoot, sm, true, logger), "grouped write of manifest, lock and vendor")
}
Expand Down Expand Up @@ -402,9 +401,9 @@ func (cmd *ensureCommand) runUpdate(ctx *dep.Ctx, args []string, p *dep.Project,
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
return errors.Wrap(sw.Write(p.AbsRoot, sm, false, logger), "grouped write of manifest, lock and vendor")
}
Expand Down Expand Up @@ -702,9 +701,9 @@ func (cmd *ensureCommand) runAdd(ctx *dep.Ctx, args []string, p *dep.Project, sm
return sw.PrintPreparedActions(ctx.Out, ctx.Verbose)
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
if err := errors.Wrap(sw.Write(p.AbsRoot, sm, true, logger), "grouped write of manifest, lock and vendor"); err != nil {
return err
Expand Down
7 changes: 3 additions & 4 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"context"
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -185,9 +184,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Wrap(err, "init failed: unable to create a SafeWriter")
}

logger := ctx.Err
if !ctx.Verbose {
logger = log.New(ioutil.Discard, "", 0)
var logger *log.Logger
if ctx.Verbose {
logger = ctx.Err
}
if err := sw.Write(root, sm, !cmd.noExamples, logger); err != nil {
return errors.Wrap(err, "init failed: unable to write the manifest, lock and vendor directory to disk")
Expand Down
5 changes: 4 additions & 1 deletion cmd/dep/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ func pruneProject(p *dep.Project, sm gps.SourceManager, logger *log.Logger) erro
}
defer os.RemoveAll(td)

if err := gps.WriteDepTree(td, p.Lock, sm, gps.CascadingPruneOptions{DefaultOptions: gps.PruneNestedVendorDirs}, logger); err != nil {
onWrite := func(progress gps.WriteProgress) {
logger.Println(progress)
}
if err := gps.WriteDepTree(td, p.Lock, sm, gps.CascadingPruneOptions{DefaultOptions: gps.PruneNestedVendorDirs}, onWrite); err != nil {
return err
}

Expand Down
5 changes: 4 additions & 1 deletion gps/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func main() {
// If no failure, blow away the vendor dir and write a new one out,
// stripping nested vendor directories as we go.
os.RemoveAll(filepath.Join(root, "vendor"))
gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, true)
pruneOpts := gps.CascadingPruneOptions{
DefaultOptions: gps.PruneNestedVendorDirs | gps.PruneUnusedPackages | gps.PruneGoTestFiles,
}
gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, pruneOpts, nil)
}
}

Expand Down
3 changes: 1 addition & 2 deletions gps/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package gps

import (
"log"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -138,7 +137,7 @@ var (

// PruneProject remove excess files according to the options passed, from
// the lp directory in baseDir.
func PruneProject(baseDir string, lp LockedProject, options PruneOptions, logger *log.Logger) error {
func PruneProject(baseDir string, lp LockedProject, options PruneOptions) error {
fsState, err := deriveFilesystemState(baseDir)

if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions gps/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package gps

import (
"io/ioutil"
"log"
"os"
"testing"

Expand Down Expand Up @@ -120,9 +119,8 @@ func TestPruneProject(t *testing.T) {
}

options := PruneNestedVendorDirs | PruneNonGoFiles | PruneGoTestFiles | PruneUnusedPackages
logger := log.New(ioutil.Discard, "", 0)

err := PruneProject(baseDir, lp, options, logger)
err := PruneProject(baseDir, lp, options)
if err != nil {
t.Fatal(err)
}
Expand Down
45 changes: 32 additions & 13 deletions gps/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package gps
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -48,6 +47,22 @@ type solution struct {
solv Solver
}

// WriteProgress informs about the progress of WriteDepTree.
type WriteProgress struct {
Count int
Total int
LP LockedProject
Failure bool
}

func (p WriteProgress) String() string {
msg := "Wrote"
if p.Failure {
msg = "Failed to write"
}
return fmt.Sprintf("(%d/%d) %s %s@%s", p.Count, p.Total, msg, p.LP.Ident(), p.LP.Version())
}

const concurrentWriters = 16

// WriteDepTree takes a basedir, a Lock and a RootPruneOptions and exports all
Expand All @@ -58,7 +73,9 @@ const concurrentWriters = 16
//
// It requires a SourceManager to do the work. Prune options are read from the
// passed manifest.
func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOptions, logger *log.Logger) error {
//
// If onWrite is not nil, it will be called after each project write. Calls are ordered and atomic.
func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOptions, onWrite func(WriteProgress)) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than log or nothing, callers get a rich type they can inspect and which also knows how to log itself.

if l == nil {
return fmt.Errorf("must provide non-nil Lock to WriteDepTree")
}
Expand Down Expand Up @@ -95,7 +112,7 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOpt
return errors.Wrapf(err, "failed to export %s", projectRoot)
}

err := PruneProject(to, p, co.PruneOptionsFor(ident.ProjectRoot), logger)
err := PruneProject(to, p, co.PruneOptionsFor(ident.ProjectRoot))
if err != nil {
return errors.Wrapf(err, "failed to prune %s", projectRoot)
}
Expand All @@ -105,18 +122,20 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, co CascadingPruneOpt

switch err {
case context.Canceled, context.DeadlineExceeded:
// Don't log "secondary" errors.
// Don't report "secondary" errors.
default:
msg := "Wrote"
if err != nil {
msg = "Failed to write"
if onWrite != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more locking to serialize to ioutil.Discard

// Increment and call atomically to prevent re-ordering.
cnt.Lock()
cnt.i++
onWrite(WriteProgress{
Count: cnt.i,
Total: len(lps),
LP: p,
Failure: err != nil,
})
cnt.Unlock()
}

// Log and increment atomically to prevent re-ordering.
cnt.Lock()
cnt.i++
logger.Printf("(%d/%d) %s %s@%s\n", cnt.i, len(lps), msg, p.Ident(), p.Version())
cnt.Unlock()
}

return err
Expand Down
11 changes: 3 additions & 8 deletions gps/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import (
"github.com/golang/dep/internal/test"
)

func discardLogger() *log.Logger {
return log.New(ioutil.Discard, "", 0)
}

var basicResult solution

func pi(n string) ProjectIdentifier {
Expand Down Expand Up @@ -109,12 +105,12 @@ func testWriteDepTree(t *testing.T) {
}

// nil lock/result should err immediately
err = WriteDepTree(tmp, nil, sm, defaultCascadingPruneOptions(), discardLogger())
err = WriteDepTree(tmp, nil, sm, defaultCascadingPruneOptions(), nil)
if err == nil {
t.Errorf("Should error if nil lock is passed to WriteDepTree")
}

err = WriteDepTree(tmp, r, sm, defaultCascadingPruneOptions(), discardLogger())
err = WriteDepTree(tmp, r, sm, defaultCascadingPruneOptions(), nil)
if err != nil {
t.Errorf("Unexpected error while creating vendor tree: %s", err)
}
Expand Down Expand Up @@ -157,7 +153,6 @@ func BenchmarkCreateVendorTree(b *testing.B) {
}

if clean {
logger := discardLogger()
b.ResetTimer()
b.StopTimer()
exp := path.Join(tmp, "export")
Expand All @@ -166,7 +161,7 @@ func BenchmarkCreateVendorTree(b *testing.B) {
// ease manual inspection
os.RemoveAll(exp)
b.StartTimer()
err = WriteDepTree(exp, r, sm, defaultCascadingPruneOptions(), logger)
err = WriteDepTree(exp, r, sm, defaultCascadingPruneOptions(), nil)
b.StopTimer()
if err != nil {
b.Errorf("unexpected error after %v iterations: %s", i, err)
Expand Down
10 changes: 9 additions & 1 deletion txn_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ func (sw SafeWriter) validate(root string, sm gps.SourceManager) error {
// operations succeeded. It also does its best to roll back if any moves fail.
// This mostly guarantees that dep cannot exit with a partial write that would
// leave an undefined state on disk.
//
// If logger is not nil, progress will be logged after each project write.
func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool, logger *log.Logger) error {
err := sw.validate(root, sm)
if err != nil {
Expand Down Expand Up @@ -319,7 +321,13 @@ func (sw *SafeWriter) Write(root string, sm gps.SourceManager, examples bool, lo
}

if sw.writeVendor {
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.lock, sm, sw.pruneOptions, logger)
var onWrite func(gps.WriteProgress)
if logger != nil {
onWrite = func(progress gps.WriteProgress) {
logger.Println(progress)
}
}
err = gps.WriteDepTree(filepath.Join(td, "vendor"), sw.lock, sm, sw.pruneOptions, onWrite)
if err != nil {
return errors.Wrap(err, "error while writing out vendor tree")
}
Expand Down
Loading