From 48be3dae5f83fc244db5fc30c13bf00c1c66cce1 Mon Sep 17 00:00:00 2001 From: Sunny Date: Mon, 11 Sep 2017 21:36:32 +0530 Subject: [PATCH] status: refactor return statements Return explicit values instead of variables in runStatusAll(). --- cmd/dep/status.go | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index 5ed824df04..42a5a21a25 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -231,22 +231,22 @@ func (cmd *statusCommand) Run(ctx *dep.Ctx, args []string) error { } } - digestMismatch, hasMissingPkgs, errStatus := runStatusAll(ctx, out, p, sm) - if errStatus.err != nil { + digestMismatch, hasMissingPkgs, errCount, errStatus := runStatusAll(ctx, out, p, sm) + if errStatus != nil { // If it's only update errors - if errStatus.err == errFailedUpdate { + if errStatus == errFailedUpdate { // Print the results with unknown data ctx.Out.Println(buf.String()) // Print the help when in non-verbose mode if !ctx.Verbose { - ctx.Out.Printf("The status of %d projects are unknown due to errors. Rerun with `-v` flag to see details.\n", errStatus.count) + ctx.Out.Printf("The status of %d projects are unknown due to errors. Rerun with `-v` flag to see details.\n", errCount) } } else { // List package failure or multiple failures ctx.Out.Println("Failed to get status. Rerun with `-v` flag to see details.") } - return errStatus.err + return errStatus } if digestMismatch { @@ -348,26 +348,16 @@ type MissingStatus struct { MissingPackages []string } -// errorStatus contains information about error and number of status failures. -type errorStatus struct { - err error - // count is for counting errors due to which we don't fail completely, but - // return partial results with missing/unknown data. - count int -} - -func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceManager) (digestMismatch bool, hasMissingPkgs bool, errStatus errorStatus) { +func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceManager) (digestMismatch bool, hasMissingPkgs bool, errCount int, errStatus error) { if p.Lock == nil { - errStatus.err = errors.Errorf("no Gopkg.lock found. Run `dep ensure` to generate lock file") - return digestMismatch, hasMissingPkgs, errStatus + return false, false, 0, errors.Errorf("no Gopkg.lock found. Run `dep ensure` to generate lock file") } // While the network churns on ListVersions() requests, statically analyze // code from the current project. ptree, err := pkgtree.ListPackages(p.ResolvedAbsRoot, string(p.ImportRoot)) if err != nil { - errStatus.err = errors.Wrapf(err, "analysis of local packages failed") - return digestMismatch, hasMissingPkgs, errStatus + return false, false, 0, errors.Wrapf(err, "analysis of local packages failed") } // Set up a solver in order to check the InputHash. @@ -387,13 +377,12 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana } if err := ctx.ValidateParams(sm, params); err != nil { - return digestMismatch, hasMissingPkgs, errorStatus{err: err} + return false, false, 0, err } s, err := gps.Prepare(params, sm) if err != nil { - errStatus.err = errors.Wrapf(err, "could not set up solver for input hashing") - return digestMismatch, hasMissingPkgs, errStatus + return false, false, 0, errors.Wrapf(err, "could not set up solver for input hashing") } cm := collectConstraints(ptree, p, sm) @@ -522,7 +511,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana // List Packages errors. This would happen only for dot output. if len(errListPkgCh) > 0 { - errStatus.err = errFailedListPkg + errStatus = errFailedListPkg if ctx.Verbose { for err := range errListPkgCh { ctx.Err.Println(err.Error()) @@ -533,15 +522,15 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana // List Version errors if len(errListVerCh) > 0 { - if errStatus.err == nil { - errStatus.err = errFailedUpdate + if errStatus == nil { + errStatus = errFailedUpdate } else { - errStatus.err = errMultipleFailures + errStatus = errMultipleFailures } // Count ListVersions error because we get partial results when // this happens. - errStatus.count = len(errListVerCh) + errCount = len(errListVerCh) if ctx.Verbose { for err := range errListVerCh { ctx.Err.Println(err.Error()) @@ -565,7 +554,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana out.BasicFooter() - return digestMismatch, hasMissingPkgs, errStatus + return false, false, errCount, errStatus } // Hash digest mismatch may indicate that some deps are no longer @@ -607,7 +596,7 @@ func runStatusAll(ctx *dep.Ctx, out outputter, p *dep.Project, sm gps.SourceMana ctx.Err.Printf("\t%s: %s\n", fail.ex, fail.err.Error()) } - return digestMismatch, hasMissingPkgs, errorStatus{err: errors.New("address issues with undeducible import paths to get more status information")} + return digestMismatch, hasMissingPkgs, 0, errors.New("address issues with undeducible import paths to get more status information") } out.MissingHeader() @@ -627,7 +616,7 @@ outer: } out.MissingFooter() - return digestMismatch, hasMissingPkgs, errorStatus{} + return digestMismatch, hasMissingPkgs, 0, nil } func formatVersion(v gps.Version) string {