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

Commit

Permalink
Merge pull request #673 from jmank88/godoc
Browse files Browse the repository at this point in the history
improve godoc; replace Loggers with embeded fields; refactor Ctx api
  • Loading branch information
sdboyer authored Jun 7, 2017
2 parents 9e747df + 2fd3763 commit f4027ef
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 127 deletions.
10 changes: 5 additions & 5 deletions cmd/dep/ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type ensureCommand struct {

func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
if cmd.examples {
ctx.Loggers.Err.Println(strings.TrimSpace(ensureExamples))
ctx.Err.Println(strings.TrimSpace(ensureExamples))
return nil
}

Expand All @@ -121,8 +121,8 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
defer sm.Release()

params := p.MakeParams()
if ctx.Loggers.Verbose {
params.TraceLogger = ctx.Loggers.Err
if ctx.Verbose {
params.TraceLogger = ctx.Err
}
params.RootPackageTree, err = pkgtree.ListPackages(p.AbsRoot, string(p.ImportRoot))
if err != nil {
Expand All @@ -136,7 +136,7 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
if cmd.update {
applyUpdateArgs(args, &params)
} else {
err := applyEnsureArgs(ctx.Loggers.Err, args, cmd.overrides, p, sm, &params)
err := applyEnsureArgs(ctx.Err, args, cmd.overrides, p, sm, &params)
if err != nil {
return err
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
return err
}
if cmd.dryRun {
return sw.PrintPreparedActions(ctx.Loggers.Out)
return sw.PrintPreparedActions(ctx.Out)
}

return errors.Wrap(sw.Write(p.AbsRoot, sm, false), "grouped write of manifest, lock and vendor")
Expand Down
8 changes: 4 additions & 4 deletions cmd/dep/glide_importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ import (
const testGlideProjectRoot = "github.com/golang/notexist"

var (
discardLogger = log.New(ioutil.Discard, "", 0)
discardLoggers = &dep.Loggers{Out: discardLogger, Err: discardLogger}
discardLogger = log.New(ioutil.Discard, "", 0)
)

func newTestContext(h *test.Helper) *dep.Ctx {
h.TempDir("src")
pwd := h.Path(".")
return &dep.Ctx{
GOPATH: pwd,
Loggers: discardLoggers,
GOPATH: pwd,
Out: discardLogger,
Err: discardLogger,
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/dep/gopath_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newGopathScanner(ctx *dep.Ctx, directDeps map[string]bool, sm gps.SourceMan
func (g *gopathScanner) InitializeRootManifestAndLock(rootM *dep.Manifest, rootL *dep.Lock) error {
var err error

g.ctx.Loggers.Err.Println("Searching GOPATH for projects...")
g.ctx.Err.Println("Searching GOPATH for projects...")
g.pd, err = g.scanGopathForDependencies()
if err != nil {
return err
Expand Down Expand Up @@ -123,7 +123,7 @@ func (g *gopathScanner) overlay(rootM *dep.Manifest, rootL *dep.Lock) {
unlockedProjects = append(unlockedProjects, string(pr))
}
if len(unlockedProjects) > 0 {
g.ctx.Loggers.Err.Printf("Following dependencies were not found in GOPATH. "+
g.ctx.Err.Printf("Following dependencies were not found in GOPATH. "+
"Dep will use the most recent versions of these projects.\n %s",
strings.Join(unlockedProjects, "\n "))
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (g *gopathScanner) scanGopathForDependencies() (projectData, error) {
var syncDepGroup sync.WaitGroup
syncDep := func(pr gps.ProjectRoot, sm gps.SourceManager) {
if err := sm.SyncSourceFor(gps.ProjectIdentifier{ProjectRoot: pr}); err != nil {
g.ctx.Loggers.Err.Printf("%+v", errors.Wrapf(err, "Unable to cache %s", pr))
g.ctx.Err.Printf("%+v", errors.Wrapf(err, "Unable to cache %s", pr))
}
syncDepGroup.Done()
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/dep/hash_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ func (hashinCommand) Run(ctx *dep.Ctx, args []string) error {
if err != nil {
return errors.Wrap(err, "prepare solver")
}
ctx.Loggers.Out.Println(gps.HashingInputsAsString(s))
ctx.Out.Println(gps.HashingInputsAsString(s))
return nil
}
6 changes: 3 additions & 3 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
ProjectAnalyzer: rootAnalyzer,
}

if ctx.Loggers.Verbose {
params.TraceLogger = ctx.Loggers.Err
if ctx.Verbose {
params.TraceLogger = ctx.Err
}

s, err := gps.Prepare(params, sm)
Expand Down Expand Up @@ -169,7 +169,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return err
}
if vendorbak != "" {
ctx.Loggers.Err.Printf("Old vendor backed up to %v", vendorbak)
ctx.Err.Printf("Old vendor backed up to %v", vendorbak)
}

sw, err := dep.NewSafeWriter(m, nil, l, dep.VendorAlways)
Expand Down
34 changes: 23 additions & 11 deletions cmd/dep/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io"
"log"
"os"
"path/filepath"
"strings"
"text/tabwriter"

Expand Down Expand Up @@ -46,11 +47,10 @@ func main() {

// A Config specifies a full configuration for a dep execution.
type Config struct {
// Args hold the command-line arguments, starting with the program name.
Args []string
Stdout, Stderr io.Writer
WorkingDir string
Env []string
WorkingDir string // Where to execute
Args []string // Command-line arguments, starting with the program name.
Env []string // Environment variables
Stdout, Stderr io.Writer // Log output
}

// Run executes a configuration and returns an exit code.
Expand Down Expand Up @@ -135,23 +135,23 @@ func (c *Config) Run() (exitCode int) {
}

// Parse the flags the user gave us.
// flag package automaticly prints usage and error message in err != nil
// flag package automatically prints usage and error message in err != nil
// or if '-h' flag provided
if err := fs.Parse(c.Args[2:]); err != nil {
exitCode = 1
return
}

loggers := &dep.Loggers{
// Set up the dep context.
ctx := &dep.Ctx{
Out: log.New(c.Stdout, "", 0),
Err: errLogger,
Verbose: *verbose,
}

// Set up the dep context.
ctx, err := dep.NewContext(c.WorkingDir, c.Env, loggers)
gopaths := filepath.SplitList(getEnv(c.Env, "GOPATH"))
err := ctx.SetPaths(c.WorkingDir, gopaths...)
if err != nil {
loggers.Err.Println(err)
errLogger.Printf("%q not in any GOPATH: %s\n", c.WorkingDir, err)
exitCode = 1
return
}
Expand All @@ -174,6 +174,18 @@ func (c *Config) Run() (exitCode int) {
return
}

// getEnv returns the last instance of the environment variable.
func getEnv(env []string, key string) string {
pre := key + "="
for i := len(env) - 1; i >= 0; i-- {
v := env[i]
if strings.HasPrefix(v, pre) {
return strings.TrimPrefix(v, pre)
}
}
return ""
}

func resetUsage(logger *log.Logger, fs *flag.FlagSet, name, args, longHelp string) {
var (
hasFlags bool
Expand Down
8 changes: 4 additions & 4 deletions cmd/dep/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (cmd *pruneCommand) Run(ctx *dep.Ctx, args []string) error {
params := p.MakeParams()
params.RootPackageTree = ptree

if ctx.Loggers.Verbose {
params.TraceLogger = ctx.Loggers.Err
if ctx.Verbose {
params.TraceLogger = ctx.Err
}

s, err := gps.Prepare(params, sm)
Expand All @@ -77,8 +77,8 @@ func (cmd *pruneCommand) Run(ctx *dep.Ctx, args []string) error {
}

var pruneLogger *log.Logger
if ctx.Loggers.Verbose {
pruneLogger = ctx.Loggers.Err
if ctx.Verbose {
pruneLogger = ctx.Err
}
return dep.PruneProject(p, sm, pruneLogger)
}
2 changes: 1 addition & 1 deletion cmd/dep/root_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (a *rootAnalyzer) importManifestAndLock(dir string, pr gps.ProjectRoot, sup

for _, i := range importers {
if i.HasDepMetadata(dir) {
a.ctx.Loggers.Err.Printf("Importing configuration from %s. These are only initial constraints, and are further refined during the solve process.", i.Name())
a.ctx.Err.Printf("Importing configuration from %s. These are only initial constraints, and are further refined during the solve process.", i.Name())
m, l, err := i.Import(dir, pr)
a.removeTransitiveDependencies(m)
return m, l, err
Expand Down
22 changes: 11 additions & 11 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,22 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error {
}
}

digestMismatch, hasMissingPkgs, err := runStatusAll(ctx.Loggers, out, p, sm)
digestMismatch, hasMissingPkgs, err := runStatusAll(ctx, out, p, sm)
if err != nil {
return err
}

if digestMismatch {
if hasMissingPkgs {
ctx.Loggers.Err.Printf("Lock inputs-digest mismatch due to the following packages missing from the lock:\n\n")
ctx.Loggers.Out.Print(buf.String())
ctx.Loggers.Err.Printf("\nThis happens when a new import is added. Run `dep ensure` to install the missing packages.\n")
ctx.Err.Printf("Lock inputs-digest mismatch due to the following packages missing from the lock:\n\n")
ctx.Out.Print(buf.String())
ctx.Err.Printf("\nThis happens when a new import is added. Run `dep ensure` to install the missing packages.\n")
} else {
ctx.Loggers.Err.Printf("Lock inputs-digest mismatch. This happens when Gopkg.toml is modified.\n" +
ctx.Err.Printf("Lock inputs-digest mismatch. This happens when Gopkg.toml is modified.\n" +
"Run `dep ensure` to regenerate the inputs-digest.")
}
} else {
ctx.Loggers.Out.Print(buf.String())
ctx.Out.Print(buf.String())
}

return nil
Expand All @@ -253,7 +253,7 @@ type MissingStatus struct {
MissingPackages []string
}

func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.SourceManager) (bool, bool, error) {
func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceManager) (bool, bool, error) {
var digestMismatch, hasMissingPkgs bool

if p.Lock == nil {
Expand All @@ -276,8 +276,8 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So
Manifest: p.Manifest,
// Locks aren't a part of the input hash check, so we can omit it.
}
if loggers.Verbose {
params.TraceLogger = loggers.Err
if ctx.Verbose {
params.TraceLogger = ctx.Err
}

s, err := gps.Prepare(params, sm)
Expand Down Expand Up @@ -412,9 +412,9 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So
// TODO this is just a fix quick so staticcheck doesn't complain.
// Visually reconciling failure to deduce project roots with the rest of
// the mismatch output is a larger problem.
loggers.Err.Printf("Failed to deduce project roots for import paths:\n")
ctx.Err.Printf("Failed to deduce project roots for import paths:\n")
for _, fail := range errs {
loggers.Err.Printf("\t%s: %s\n", fail.ex, fail.err.Error())
ctx.Err.Printf("\t%s: %s\n", fail.ex, fail.err.Error())
}

return digestMismatch, hasMissingPkgs, errors.New("address issues with undeducible import paths to get more status information")
Expand Down
Loading

0 comments on commit f4027ef

Please sign in to comment.