From 583fce1ace57ad7372449cf5f1e4e8e131c51b20 Mon Sep 17 00:00:00 2001 From: Robert Sese <734194+rsese@users.noreply.github.com> Date: Sat, 7 Jan 2023 20:14:22 -0600 Subject: [PATCH 1/2] plain output if no terminal --- main.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index b20e237..e9f90ee 100644 --- a/main.go +++ b/main.go @@ -43,22 +43,15 @@ func (w *workflow) RenderHealth() string { successStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#32cd32")) neutralStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#808080")) failedStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#dc143c")) - var results string - for i, r := range w.Runs { - if i > defaultMaxRuns { - break - } - - if r.Status != "completed" { - results += neutralStyle.Render("-") - continue - } + var results string + health := workflowHealth(*w) - switch r.Conclusion { - case "success": + for _, r := range health { + switch r { + case '✓': results += successStyle.Render("✓") - case "skipped", "cancelled", "neutral": + case '-': results += neutralStyle.Render("-") default: results += failedStyle.Render("x") @@ -159,6 +152,82 @@ type options struct { Selector string } +func workflowHealth(w workflow) string { + health := "" + + for i, r := range w.Runs { + if i > defaultMaxRuns { + break + } + + if r.Status != "completed" { + health += "-" + continue + } + + switch r.Conclusion { + case "success": + health += "✓" + case "skipped", "cancelled", "neutral": + health += "-" + default: + health += "x" + } + } + + return health +} + +func noTerminalRender(repos []*repositoryData, selector string, opts *options) error { + totalBillableMs := 0 + + for _, r := range repos { + workflows, err := getWorkflows(*r, opts.Last) + if err != nil { + return err + } + + r.Workflows = workflows + + for _, w := range workflows { + totalBillableMs += w.BillableMs + } + } + + fmt.Printf("GitHub Actions dashboard for %s for the past %s\n", selector, util.FuzzyAgo(opts.Last)) + fmt.Printf("Total billable time: %s\n", util.PrettyMS(totalBillableMs)) + + for _, r := range repos { + if len(r.Workflows) == 0 { + continue + } + fmt.Println() + fmt.Println(r.Name) + // TODO leverage go-gh to determine what host to use + // (NB: go-gh needs a PR in order to help with this) + fmt.Printf("https://github.com/%s/actions\n", r.Name) + fmt.Println() + + for _, w := range r.Workflows { + fmt.Println() + fmt.Printf("%s:\n", w.Name) + if len(w.Runs) == 0 { + fmt.Printf(" No runs\n") + } else { + health := workflowHealth(*w) + + fmt.Printf(" %-15s %v\n", "Health: ", health) + fmt.Printf(" %-15s %v\n", "Avg elapsed: ", w.AverageElapsed()) + fmt.Printf(" %-15s %v\n", "Billable time: ", util.PrettyMS(w.BillableMs)) + } + } + + fmt.Println() + } + + return nil +} + func _main(opts *options) error { selector := opts.Selector last := opts.Last @@ -169,6 +238,12 @@ func _main(opts *options) error { } columnWidth := defaultWorkflowNameLength + 5 // account for ellipsis and padding/border + + if !term.IsTerminal(int(os.Stdout.Fd())) { + noTerminalRender(repos, selector, opts) + return nil + } + cardsPerRow := (getTerminalWidth() / columnWidth) - 1 cardStyle := lipgloss.NewStyle(). From 0651252cf232f00af11a963af18f6f79a18167a0 Mon Sep 17 00:00:00 2001 From: Robert Sese <734194+rsese@users.noreply.github.com> Date: Sun, 8 Jan 2023 19:04:48 -0600 Subject: [PATCH 2/2] move things around --- main.go | 96 +++++++++++++++++++++++++-------------------------------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git a/main.go b/main.go index e9f90ee..ac65e04 100644 --- a/main.go +++ b/main.go @@ -178,25 +178,7 @@ func workflowHealth(w workflow) string { return health } -func noTerminalRender(repos []*repositoryData, selector string, opts *options) error { - totalBillableMs := 0 - - for _, r := range repos { - workflows, err := getWorkflows(*r, opts.Last) - if err != nil { - return err - } - - r.Workflows = workflows - - for _, w := range workflows { - totalBillableMs += w.BillableMs - } - } - - fmt.Printf("GitHub Actions dashboard for %s for the past %s\n", selector, util.FuzzyAgo(opts.Last)) - fmt.Printf("Total billable time: %s\n", util.PrettyMS(totalBillableMs)) - +func noTerminalRender(repos []*repositoryData) error { for _, r := range repos { if len(r.Workflows) == 0 { continue @@ -228,22 +210,8 @@ func noTerminalRender(repos []*repositoryData, selector string, opts *options) e return nil } -func _main(opts *options) error { - selector := opts.Selector - last := opts.Last - - repos, err := populateRepos(opts) - if err != nil { - return fmt.Errorf("could not fetch repository data: %w", err) - } - +func terminalRender(repos []*repositoryData) error { columnWidth := defaultWorkflowNameLength + 5 // account for ellipsis and padding/border - - if !term.IsTerminal(int(os.Stdout.Fd())) { - noTerminalRender(repos, selector, opts) - return nil - } - cardsPerRow := (getTerminalWidth() / columnWidth) - 1 cardStyle := lipgloss.NewStyle(). @@ -253,29 +221,9 @@ func _main(opts *options) error { BorderStyle(lipgloss.DoubleBorder()). BorderForeground(lipgloss.Color("63")) - titleStyle := lipgloss.NewStyle().Bold(true).Align(lipgloss.Center).Width(getTerminalWidth()) - subTitleStyle := lipgloss.NewStyle().Align(lipgloss.Center).Width(getTerminalWidth()) repoNameStyle := lipgloss.NewStyle().Bold(true) repoHintStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#808080")).Italic(true) - totalBillableMs := 0 - - for _, r := range repos { - workflows, err := getWorkflows(*r, last) - if err != nil { - return err - } - - r.Workflows = workflows - - for _, w := range workflows { - totalBillableMs += w.BillableMs - } - } - - fmt.Println(titleStyle.Render(fmt.Sprintf("GitHub Actions dashboard for %s for the past %s", selector, util.FuzzyAgo(opts.Last)))) - fmt.Println(subTitleStyle.Render(fmt.Sprintf("Total billable time: %s", util.PrettyMS(totalBillableMs)))) - for _, r := range repos { if len(r.Workflows) == 0 { continue @@ -307,6 +255,46 @@ func _main(opts *options) error { return nil } +func _main(opts *options) error { + selector := opts.Selector + last := opts.Last + + repos, err := populateRepos(opts) + if err != nil { + return fmt.Errorf("could not fetch repository data: %w", err) + } + + totalBillableMs := 0 + + for _, r := range repos { + workflows, err := getWorkflows(*r, last) + if err != nil { + return err + } + + r.Workflows = workflows + + for _, w := range workflows { + totalBillableMs += w.BillableMs + } + } + + if term.IsTerminal(int(os.Stdout.Fd())) { + titleStyle := lipgloss.NewStyle().Bold(true).Align(lipgloss.Center).Width(getTerminalWidth()) + subTitleStyle := lipgloss.NewStyle().Align(lipgloss.Center).Width(getTerminalWidth()) + + fmt.Println(titleStyle.Render(fmt.Sprintf("GitHub Actions dashboard for %s for the past %s", selector, util.FuzzyAgo(opts.Last)))) + fmt.Println(subTitleStyle.Render(fmt.Sprintf("Total billable time: %s", util.PrettyMS(totalBillableMs)))) + terminalRender(repos) + } else { + fmt.Printf("GitHub Actions dashboard for %s for the past %s\n", selector, util.FuzzyAgo(opts.Last)) + fmt.Printf("Total billable time: %s\n", util.PrettyMS(totalBillableMs)) + noTerminalRender(repos) + } + + return nil +} + func populateRepos(opts *options) ([]*repositoryData, error) { result := []*repositoryData{} if len(opts.Repositories) > 0 {