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

Commit

Permalink
status: refactor return statements
Browse files Browse the repository at this point in the history
Return explicit values instead of variables in runStatusAll().
  • Loading branch information
darkowlzz committed Sep 11, 2017
1 parent 2d25bff commit 48be3da
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -627,7 +616,7 @@ outer:
}
out.MissingFooter()

return digestMismatch, hasMissingPkgs, errorStatus{}
return digestMismatch, hasMissingPkgs, 0, nil
}

func formatVersion(v gps.Version) string {
Expand Down

0 comments on commit 48be3da

Please sign in to comment.