Skip to content

Commit

Permalink
Prettify run report with pterm.sh tables / styles (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackproser authored Jul 20, 2022
1 parent 3d8ca75 commit 8a113aa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 73 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ require (
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-github/v43 v43.0.0
github.com/gruntwork-io/go-commons v0.8.2
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1
github.com/pterm/pterm v0.12.42
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.7.0
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZ
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
Expand Down Expand Up @@ -354,8 +353,6 @@ github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23 h1:M8exrBzuhWcU6aoHJlHWPe4qFjVKzkMGRal78f5jRRU=
github.com/kataras/tablewriter v0.0.0-20180708051242-e063d29b7c23/go.mod h1:kBSna6b0/RzsOcOZf515vAXwSsXYusl2U7SA0XP09yI=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 h1:DowS9hvgyYSX4TO5NpyC606/Z4SxnNYbT+WX27or6Ck=
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand All @@ -379,8 +376,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1 h1:xUwSaTDYl+Ib5OoFxWJnqYFG9N31++qfeNXzTZ1cc8o=
github.com/landoop/tableprinter v0.0.0-20200805134727-ea32388e35c1/go.mod h1:f0X1c0za3TbET/rl5ThtCSel0+G3/yZ8iuU9BxnyVK0=
github.com/lithammer/fuzzysearch v1.1.5 h1:Ag7aKU08wp0R9QCfF4GoGST9HbmAIeLP7xwMrOBEp1c=
github.com/lithammer/fuzzysearch v1.1.5/go.mod h1:1R1LRNk7yKid1BaQkmuLQaHruxcC4HmAH30Dh61Ih1Q=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down
121 changes: 59 additions & 62 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,35 @@ package printer

import (
"fmt"
"os"
"strings"
"time"

"github.com/gruntwork-io/git-xargs/types"
"github.com/kataras/tablewriter"
"github.com/landoop/tableprinter"
"github.com/pterm/pterm"
)

// configurePrinterStyling accepts a pointer to a table printer and sets up the styles commonly used across them
// resulting in uniform tabular output to STDOUT following each run of the CLI
func configurePrinterStyling(printer *tableprinter.Printer) {
printer.BorderTop, printer.BorderBottom, printer.BorderLeft, printer.BorderRight = false, false, true, true
printer.CenterSeparator = "|"
printer.ColumnSeparator = "|"
printer.RowSeparator = "-"
printer.HeaderBgColor = tablewriter.BgBlackColor
printer.HeaderFgColor = tablewriter.FgGreenColor
}

func PrintRepoReport(allEvents []types.AnnotatedEvent, runReport *types.RunReport) {
fmt.Print("\n\n")
fmt.Println("*****************************************************************")
fmt.Printf(" GIT-XARGS RUN SUMMARY @ %v\n", time.Now().UTC())
fmt.Printf(" Runtime in seconds: %v\n", runReport.RuntimeSeconds)
fmt.Println("*****************************************************************")

// If there were any allowed repos provided via file, print out the list of them
fileProvidedReposPrinter := tableprinter.New(os.Stdout)
configurePrinterStyling(fileProvidedReposPrinter)

fmt.Print("\n\n")

fmt.Println("COMMAND SUPPLIED")
fmt.Println()
fmt.Println(runReport.Command)
fmt.Println()
fmt.Println("REPO SELECTION METHOD USED FOR THIS RUN - (see README.md for more information)")
fmt.Println()
fmt.Println(runReport.SelectionMode)

// If the user selected repos via a flat file, print a table showing which repos they were
renderSection(fmt.Sprintf("Git-xargs run summary @ %s", time.Now().UTC()))

pterm.DefaultBulletList.WithItems([]pterm.BulletListItem{
{Level: 0, Text: fmt.Sprintf("Runtime in seconds: %d", runReport.RuntimeSeconds)},
{Level: 0, Text: fmt.Sprintf("Command supplied: %s", runReport.Command)},
{Level: 0, Text: fmt.Sprintf("Repo selection method: %s", runReport.SelectionMode)},
}).Render()

if len(runReport.FileProvidedRepos) > 0 {
fmt.Println(" REPOS SUPPLIED VIA --repos FILE FLAG")
fileProvidedReposPrinter.Print(runReport.FileProvidedRepos)
renderSection("Repos supplied via --repos file flag")
data := make([][]string, len(runReport.FileProvidedRepos))
for idx, fileProvidedRepo := range runReport.FileProvidedRepos {
data[idx] = []string{fmt.Sprintf("%s/%s", fileProvidedRepo.Organization, fileProvidedRepo.Name)}
}
renderTableWithHeader([]string{"Repo name"}, data)
}
// For each event type, print a summary of the repos in that category

// For each event type, print a summary table of the repos in that category
for _, ae := range allEvents {

var reducedRepos []types.ReducedRepo

printer := tableprinter.New(os.Stdout)
configurePrinterStyling(printer)

for _, repo := range runReport.Repos[ae.Event] {
rr := types.ReducedRepo{
Name: repo.GetName(),
Expand All @@ -65,10 +40,14 @@ func PrintRepoReport(allEvents []types.AnnotatedEvent, runReport *types.RunRepor
}

if len(reducedRepos) > 0 {
fmt.Println()
fmt.Printf(" %s\n", strings.ToUpper(ae.Description))
printer.Print(reducedRepos)
fmt.Println()

renderSection(ae.Description)
data := make([][]string, len(reducedRepos))
for idx, repo := range reducedRepos {
data[idx] = []string{repo.Name, repo.URL}
}

renderTableWithHeader([]string{"Repo name", "Repo URL"}, data)
}
}

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

if len(pullRequests) > 0 {
fmt.Println()
fmt.Println("*****************************************************")
fmt.Println(" PULL REQUESTS OPENED")
fmt.Println("*****************************************************")
pullRequestPrinter := tableprinter.New(os.Stdout)
configurePrinterStyling(pullRequestPrinter)
pullRequestPrinter.Print(pullRequests)
fmt.Println()
renderSection("Pull requests opened")

data := make([][]string, len(pullRequests))
for idx, pullRequest := range pullRequests {
data[idx] = []string{pullRequest.Repo, pullRequest.URL}
}

renderTableWithHeader([]string{"Repo name", "Pull request URL"}, data)
}

if len(draftPullRequests) > 0 {
fmt.Println()
fmt.Println("*****************************************************")
fmt.Println(" DRAFT PULL REQUESTS OPENED")
fmt.Println("*****************************************************")
pullRequestPrinter := tableprinter.New(os.Stdout)
configurePrinterStyling(pullRequestPrinter)
pullRequestPrinter.Print(draftPullRequests)
fmt.Println()
renderSection("Draft Pull requests opened")

data := make([][]string, len(draftPullRequests))
for idx, draftPullRequest := range draftPullRequests {
data[idx] = []string{draftPullRequest.Repo, draftPullRequest.URL}
}

renderTableWithHeader([]string{"Repo name", "Draft Pull request URL"}, data)
}
}

func renderSection(sectionTitle string) {
pterm.DefaultSection.Style = pterm.NewStyle(pterm.FgLightCyan)
pterm.DefaultSection.WithLevel(0).Println(sectionTitle)
}

func renderTableWithHeader(headers []string, data [][]string) {
tableData := pterm.TableData{
headers,
}
for idx := range data {
tableData = append(tableData, data[idx])
}
pterm.DefaultTable.
WithHasHeader().
WithBoxed(true).
WithRowSeparator("-").
WithData(tableData).
Render()
}
8 changes: 4 additions & 4 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const (
PullRequestAlreadyExists types.Event = "pull-request-already-exists"
// CommitsMadeDirectlyToBranch denotes a repo whose local worktree changes were committed directly to the specified branch because the --skip-pull-requests flag was passed
CommitsMadeDirectlyToBranch types.Event = "commits-made-directly-to-branch"
//DirectCommitsPushedToRemoteBranch denotes a repo whose changes were pushed to the remote specified branch because the --skip-pull-requests flag was passed
// DirectCommitsPushedToRemoteBranch denotes a repo whose changes were pushed to the remote specified branch because the --skip-pull-requests flag was passed
DirectCommitsPushedToRemoteBranch types.Event = "direct-commits-pushed-to-remote"
// BranchRemotePullFailed denotes a repo whose remote branch could not be fetched successfully
BranchRemotePullFailed types.Event = "branch-remote-pull-failed"
Expand All @@ -74,14 +74,14 @@ const (
BaseBranchTargetInvalidErr types.Event = "base-branch-target-invalid"
// PRFailedDueToRateLimits denotes a repo whose initial pull request failed as a result of being rate limited by GitHub
PRFailedDueToRateLimitsErr types.Event = "pr-failed-due-to-rate-limits"
//PRFailedAfterMaximumRetriesErr denotes a repo whose pull requests all failed to be created via GitHub following the maximum number of retries
// PRFailedAfterMaximumRetriesErr denotes a repo whose pull requests all failed to be created via GitHub following the maximum number of retries
PRFailedAfterMaximumRetriesErr types.Event = "pr-failed-after-maximum-retries"
)

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

0 comments on commit 8a113aa

Please sign in to comment.