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

Commit fd32123

Browse files
committed
projectStatus: Prettify output
1 parent 6a55fc1 commit fd32123

File tree

1 file changed

+142
-24
lines changed

1 file changed

+142
-24
lines changed

cmd/dep/status.go

+142-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package main
66

77
import (
8+
"bufio"
89
"bytes"
910
"context"
1011
"encoding/json"
@@ -650,45 +651,162 @@ func collectConstraints(ptree pkgtree.PackageTree, p *dep.Project, sm gps.Source
650651
return map[string][]gps.Constraint{}
651652
}
652653

654+
// pubVersion type to store Public Version data of a project.
655+
type pubVersions map[string][]string
656+
657+
// TabString returns a tabwriter compatible string of pubVersion.
658+
func (pv pubVersions) TabString() string {
659+
var buf bytes.Buffer
660+
w := bufio.NewWriter(&buf)
661+
662+
// Create a list of version categories and sort it for consistent results.
663+
var catgs []string
664+
665+
for catg := range pv {
666+
catgs = append(catgs, catg)
667+
}
668+
669+
// Sort the list of categories.
670+
sort.Strings(catgs)
671+
672+
// Count the number of different version categories. Use this count to add
673+
// a newline("\n") and tab("\t") in all the version string list except the
674+
// first one. This is required to maintain the indentation of the strings
675+
// when used with tabwriter.
676+
// semver:...\n \tbranches:...\n \tnonsemvers:...
677+
count := 0
678+
for _, catg := range catgs {
679+
count++
680+
if count > 1 {
681+
fmt.Fprintf(w, "\n \t")
682+
}
683+
684+
vers := pv[catg]
685+
686+
// Sort the versions list for consistent result.
687+
sort.Strings(vers)
688+
689+
fmt.Fprintf(w, "%s: %s", catg, strings.Join(vers, ", "))
690+
}
691+
w.Flush()
692+
693+
return buf.String()
694+
}
695+
696+
type projectImporters map[string]bool
697+
698+
func (pi projectImporters) String() string {
699+
var projects []string
700+
701+
for p := range pi {
702+
projects = append(projects, p)
703+
}
704+
705+
// Sort the projects for a consistent result.
706+
sort.Strings(projects)
707+
708+
return strings.Join(projects, ", ")
709+
}
710+
711+
type packageImporters map[string][]string
712+
713+
func (pi packageImporters) TabString() string {
714+
var buf bytes.Buffer
715+
w := bufio.NewWriter(&buf)
716+
717+
// Create a list of packages in the map and sort it for consistent results.
718+
var pkgs []string
719+
720+
for pkg := range pi {
721+
pkgs = append(pkgs, pkg)
722+
}
723+
724+
// Sort the list of packages.
725+
sort.Strings(pkgs)
726+
727+
// Count the number of different packages. Use this count to add
728+
// a newline("\n") and tab("\t") in all the package string header except the
729+
// first one. This is required to maintain the indentation of the strings
730+
// when used with tabwriter.
731+
// github.com/x/y\n \t github.com/a/b/foo\n \t github.com/a/b/bar
732+
count := 0
733+
for _, pkg := range pkgs {
734+
count++
735+
if count > 1 {
736+
fmt.Fprintf(w, "\n \t")
737+
}
738+
739+
fmt.Fprintf(w, "%s", pkg)
740+
741+
importers := pi[pkg]
742+
743+
// Sort the importers list for consistent result.
744+
sort.Strings(importers)
745+
746+
for _, p := range importers {
747+
fmt.Fprintf(w, "\n \t %s", p)
748+
}
749+
}
750+
w.Flush()
751+
752+
return buf.String()
753+
}
754+
653755
type projectStatus struct {
654756
Project string
655757
Version string
656758
Constraints []string
657759
Source string
658760
AltSource string
659-
PubVersions map[string][]string
761+
PubVersions pubVersions
660762
Revision string
661763
LatestAllowed string
662764
SourceType string
663765
Packages []string
664-
ProjectImporters map[string]bool
665-
PackageImporters map[string][]string
766+
ProjectImporters projectImporters
767+
PackageImporters packageImporters
666768
UpstreamExists bool
667769
UpstreamVersionExists bool
668770
}
669771

670772
func (ps projectStatus) String() string {
671-
return fmt.Sprintf(`
672-
PROJECT: %s
673-
VERSION: %s
674-
CONSTRAINTS: %s
675-
SOURCE: %s
676-
ALT SOURCE: %s
677-
PUB VERSION: %s
678-
REVISION: %s
679-
LATEST ALLOWED: %s
680-
SOURCE TYPE: %s
681-
PACKAGES: %s
682-
PROJECT IMPORTERS: %v
683-
PACKAGE IMPORTERS: %s
684-
UPSTREAM EXISTS: %t
685-
UPSTREAM VERSION EXISTS: %t
686-
`,
773+
var buf bytes.Buffer
774+
775+
w := tabwriter.NewWriter(&buf, 0, 4, 2, ' ', 0)
776+
777+
upstreamExists := "no"
778+
if ps.UpstreamExists {
779+
upstreamExists = "yes"
780+
}
781+
782+
upstreamVersionExists := "no"
783+
if ps.UpstreamVersionExists {
784+
upstreamVersionExists = "yes"
785+
}
786+
787+
fmt.Fprintf(w, "\n"+
788+
"PROJECT:\t%s\n"+
789+
"VERSION:\t%s\n"+
790+
"CONSTRAINTS:\t%s\n"+
791+
"SOURCE:\t%s\n"+
792+
"ALT SOURCE:\t%s\n"+
793+
"PUB VERSION:\t%s\n"+
794+
"REVISION:\t%s\n"+
795+
"LATEST ALLOWED:\t%s\n"+
796+
"SOURCE TYPE:\t%s\n"+
797+
"PACKAGES:\t%s\n"+
798+
"PROJECT IMPORTERS:\t%s\n"+
799+
"PACKAGE IMPORTERS:\t%s\n"+
800+
"UPSTREAM EXISTS:\t%s\n"+
801+
"UPSTREAM VERSION EXISTS:\t%s",
687802
ps.Project, ps.Version, ps.Constraints, ps.Source, ps.AltSource,
688-
ps.PubVersions, ps.Revision, ps.LatestAllowed, ps.SourceType,
689-
strings.Join(ps.Packages, ", "),
690-
ps.ProjectImporters, ps.PackageImporters, ps.UpstreamExists,
691-
ps.UpstreamVersionExists)
803+
ps.PubVersions.TabString(), ps.Revision, ps.LatestAllowed, ps.SourceType,
804+
strings.Join(ps.Packages, ", "), ps.ProjectImporters,
805+
ps.PackageImporters.TabString(), upstreamExists, upstreamVersionExists,
806+
)
807+
w.Flush()
808+
809+
return buf.String()
692810
}
693811

694812
func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.SourceManager) error {
@@ -840,7 +958,7 @@ func runProjectStatus(ctx *dep.Ctx, args []string, p *dep.Project, sm gps.Source
840958
}
841959
}
842960
projStatus.PubVersions = make(map[string][]string)
843-
projStatus.PubVersions["semver"] = semvers
961+
projStatus.PubVersions["semvers"] = semvers
844962
projStatus.PubVersions["branches"] = branches
845963
projStatus.PubVersions["nonsemvers"] = nonsemvers
846964

0 commit comments

Comments
 (0)