Skip to content

Commit 8a113aa

Browse files
authored
Prettify run report with pterm.sh tables / styles (#84)
1 parent 3d8ca75 commit 8a113aa

File tree

4 files changed

+63
-73
lines changed

4 files changed

+63
-73
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ require (
77
github.com/golang/protobuf v1.4.3 // indirect
88
github.com/google/go-github/v43 v43.0.0
99
github.com/gruntwork-io/go-commons v0.8.2
10-
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23
11-
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1
1210
github.com/pterm/pterm v0.12.42
1311
github.com/sirupsen/logrus v1.7.0
1412
github.com/stretchr/testify v1.7.0

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZ
166166
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
167167
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
168168
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
169-
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
170169
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
171170
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
172171
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
@@ -354,8 +353,6 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/
354353
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
355354
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
356355
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
357-
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 h1:M8exrBzuhWcU6aoHJlHWPe4qFjVKzkMGRal78f5jRRU=
358-
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23/go.mod h1:kBSna6b0/RzsOcOZf515vAXwSsXYusl2U7SA0XP09yI=
359356
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
360357
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
361358
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -379,8 +376,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
379376
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
380377
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
381378
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
382-
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1 h1:xUwSaTDYl+Ib5OoFxWJnqYFG9N31++qfeNXzTZ1cc8o=
383-
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1/go.mod h1:f0X1c0za3TbET/rl5ThtCSel0+G3/yZ8iuU9BxnyVK0=
384379
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
385380
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
386381
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=

printer/printer.go

Lines changed: 59 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,35 @@ package printer
22

33
import (
44
"fmt"
5-
"os"
6-
"strings"
75
"time"
86

97
"github.com/gruntwork-io/git-xargs/types"
10-
"github.com/kataras/tablewriter"
11-
"github.com/landoop/tableprinter"
8+
"github.com/pterm/pterm"
129
)
1310

14-
// configurePrinterStyling accepts a pointer to a table printer and sets up the styles commonly used across them
15-
// resulting in uniform tabular output to STDOUT following each run of the CLI
16-
func configurePrinterStyling(printer *tableprinter.Printer) {
17-
printer.BorderTop, printer.BorderBottom, printer.BorderLeft, printer.BorderRight = false, false, true, true
18-
printer.CenterSeparator = "|"
19-
printer.ColumnSeparator = "|"
20-
printer.RowSeparator = "-"
21-
printer.HeaderBgColor = tablewriter.BgBlackColor
22-
printer.HeaderFgColor = tablewriter.FgGreenColor
23-
}
24-
2511
func PrintRepoReport(allEvents []types.AnnotatedEvent, runReport *types.RunReport) {
26-
fmt.Print("\n\n")
27-
fmt.Println("*****************************************************************")
28-
fmt.Printf(" GIT-XARGS RUN SUMMARY @ %v\n", time.Now().UTC())
29-
fmt.Printf(" Runtime in seconds: %v\n", runReport.RuntimeSeconds)
30-
fmt.Println("*****************************************************************")
31-
32-
// If there were any allowed repos provided via file, print out the list of them
33-
fileProvidedReposPrinter := tableprinter.New(os.Stdout)
34-
configurePrinterStyling(fileProvidedReposPrinter)
35-
36-
fmt.Print("\n\n")
37-
38-
fmt.Println("COMMAND SUPPLIED")
39-
fmt.Println()
40-
fmt.Println(runReport.Command)
41-
fmt.Println()
42-
fmt.Println("REPO SELECTION METHOD USED FOR THIS RUN - (see README.md for more information)")
43-
fmt.Println()
44-
fmt.Println(runReport.SelectionMode)
45-
46-
// If the user selected repos via a flat file, print a table showing which repos they were
12+
renderSection(fmt.Sprintf("Git-xargs run summary @ %s", time.Now().UTC()))
13+
14+
pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
15+
{Level: 0, Text: fmt.Sprintf("Runtime in seconds: %d", runReport.RuntimeSeconds)},
16+
{Level: 0, Text: fmt.Sprintf("Command supplied: %s", runReport.Command)},
17+
{Level: 0, Text: fmt.Sprintf("Repo selection method: %s", runReport.SelectionMode)},
18+
}).Render()
19+
4720
if len(runReport.FileProvidedRepos) > 0 {
48-
fmt.Println(" REPOS SUPPLIED VIA --repos FILE FLAG")
49-
fileProvidedReposPrinter.Print(runReport.FileProvidedRepos)
21+
renderSection("Repos supplied via --repos file flag")
22+
data := make([][]string, len(runReport.FileProvidedRepos))
23+
for idx, fileProvidedRepo := range runReport.FileProvidedRepos {
24+
data[idx] = []string{fmt.Sprintf("%s/%s", fileProvidedRepo.Organization, fileProvidedRepo.Name)}
25+
}
26+
renderTableWithHeader([]string{"Repo name"}, data)
5027
}
51-
// For each event type, print a summary of the repos in that category
28+
29+
// For each event type, print a summary table of the repos in that category
5230
for _, ae := range allEvents {
5331

5432
var reducedRepos []types.ReducedRepo
5533

56-
printer := tableprinter.New(os.Stdout)
57-
configurePrinterStyling(printer)
58-
5934
for _, repo := range runReport.Repos[ae.Event] {
6035
rr := types.ReducedRepo{
6136
Name: repo.GetName(),
@@ -65,10 +40,14 @@ func PrintRepoReport(allEvents []types.AnnotatedEvent, runReport *types.RunRepor
6540
}
6641

6742
if len(reducedRepos) > 0 {
68-
fmt.Println()
69-
fmt.Printf(" %s\n", strings.ToUpper(ae.Description))
70-
printer.Print(reducedRepos)
71-
fmt.Println()
43+
44+
renderSection(ae.Description)
45+
data := make([][]string, len(reducedRepos))
46+
for idx, repo := range reducedRepos {
47+
data[idx] = []string{repo.Name, repo.URL}
48+
}
49+
50+
renderTableWithHeader([]string{"Repo name", "Repo URL"}, data)
7251
}
7352
}
7453

@@ -93,26 +72,44 @@ func PrintRepoReport(allEvents []types.AnnotatedEvent, runReport *types.RunRepor
9372
}
9473

9574
if len(pullRequests) > 0 {
96-
fmt.Println()
97-
fmt.Println("*****************************************************")
98-
fmt.Println(" PULL REQUESTS OPENED")
99-
fmt.Println("*****************************************************")
100-
pullRequestPrinter := tableprinter.New(os.Stdout)
101-
configurePrinterStyling(pullRequestPrinter)
102-
pullRequestPrinter.Print(pullRequests)
103-
fmt.Println()
75+
renderSection("Pull requests opened")
10476

77+
data := make([][]string, len(pullRequests))
78+
for idx, pullRequest := range pullRequests {
79+
data[idx] = []string{pullRequest.Repo, pullRequest.URL}
80+
}
81+
82+
renderTableWithHeader([]string{"Repo name", "Pull request URL"}, data)
10583
}
10684

10785
if len(draftPullRequests) > 0 {
108-
fmt.Println()
109-
fmt.Println("*****************************************************")
110-
fmt.Println(" DRAFT PULL REQUESTS OPENED")
111-
fmt.Println("*****************************************************")
112-
pullRequestPrinter := tableprinter.New(os.Stdout)
113-
configurePrinterStyling(pullRequestPrinter)
114-
pullRequestPrinter.Print(draftPullRequests)
115-
fmt.Println()
86+
renderSection("Draft Pull requests opened")
87+
88+
data := make([][]string, len(draftPullRequests))
89+
for idx, draftPullRequest := range draftPullRequests {
90+
data[idx] = []string{draftPullRequest.Repo, draftPullRequest.URL}
91+
}
92+
93+
renderTableWithHeader([]string{"Repo name", "Draft Pull request URL"}, data)
94+
}
95+
}
11696

97+
func renderSection(sectionTitle string) {
98+
pterm.DefaultSection.Style = pterm.NewStyle(pterm.FgLightCyan)
99+
pterm.DefaultSection.WithLevel(0).Println(sectionTitle)
100+
}
101+
102+
func renderTableWithHeader(headers []string, data [][]string) {
103+
tableData := pterm.TableData{
104+
headers,
105+
}
106+
for idx := range data {
107+
tableData = append(tableData, data[idx])
117108
}
109+
pterm.DefaultTable.
110+
WithHasHeader().
111+
WithBoxed(true).
112+
WithRowSeparator("-").
113+
WithData(tableData).
114+
Render()
118115
}

stats/stats.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const (
6060
PullRequestAlreadyExists types.Event = "pull-request-already-exists"
6161
// CommitsMadeDirectlyToBranch denotes a repo whose local worktree changes were committed directly to the specified branch because the --skip-pull-requests flag was passed
6262
CommitsMadeDirectlyToBranch types.Event = "commits-made-directly-to-branch"
63-
//DirectCommitsPushedToRemoteBranch denotes a repo whose changes were pushed to the remote specified branch because the --skip-pull-requests flag was passed
63+
// DirectCommitsPushedToRemoteBranch denotes a repo whose changes were pushed to the remote specified branch because the --skip-pull-requests flag was passed
6464
DirectCommitsPushedToRemoteBranch types.Event = "direct-commits-pushed-to-remote"
6565
// BranchRemotePullFailed denotes a repo whose remote branch could not be fetched successfully
6666
BranchRemotePullFailed types.Event = "branch-remote-pull-failed"
@@ -74,14 +74,14 @@ const (
7474
BaseBranchTargetInvalidErr types.Event = "base-branch-target-invalid"
7575
// PRFailedDueToRateLimits denotes a repo whose initial pull request failed as a result of being rate limited by GitHub
7676
PRFailedDueToRateLimitsErr types.Event = "pr-failed-due-to-rate-limits"
77-
//PRFailedAfterMaximumRetriesErr denotes a repo whose pull requests all failed to be created via GitHub following the maximum number of retries
77+
// PRFailedAfterMaximumRetriesErr denotes a repo whose pull requests all failed to be created via GitHub following the maximum number of retries
7878
PRFailedAfterMaximumRetriesErr types.Event = "pr-failed-after-maximum-retries"
7979
)
8080

8181
var allEvents = []types.AnnotatedEvent{
8282
{Event: FetchedViaGithubAPI, Description: "Repos successfully fetched via Github API"},
8383
{Event: DryRunSet, Description: "Repos that were not modified in any way because this was a dry-run"},
84-
{Event: ReposSelected, Description: "All repos that were targeted for processing AFTER filtering missing / malformed repos"},
84+
{Event: ReposSelected, Description: "All repos that were targeted for processing after filtering missing / malformed repos"},
8585
{Event: ReposArchivedSkipped, Description: "All repos that were filtered out with the --skip-archived-repos flag"},
8686
{Event: TargetBranchNotFound, Description: "Repos whose target branch was not found"},
8787
{Event: TargetBranchAlreadyExists, Description: "Repos whose target branch already existed"},
@@ -93,7 +93,7 @@ var allEvents = []types.AnnotatedEvent{
9393
{Event: CommandErrorOccurredDuringExecution, Description: "Repos for which the supplied command raised an error during execution"},
9494
{Event: WorktreeStatusCheckFailed, Description: "Repos for which the git status command failed following command execution"},
9595
{Event: WorktreeStatusDirty, Description: "Repos that showed file changes to their working directory following command execution"},
96-
{Event: WorktreeStatusClean, Description: "Repos that showed NO file changes to their working directory following command execution"},
96+
{Event: WorktreeStatusClean, Description: "Repos that showed no file changes to their working directory following command execution"},
9797
{Event: CommitChangesFailed, Description: "Repos whose file changes failed to be committed for some reason"},
9898
{Event: PushBranchFailed, Description: "Repos whose tool-specific branch containing changes failed to push to remote origin"},
9999
{Event: PushBranchSkipped, Description: "Repos whose local branch was not pushed because the --dry-run flag was set"},

0 commit comments

Comments
 (0)