From be2ee83fad1656b5a6d60c3075466ff828c0ae15 Mon Sep 17 00:00:00 2001 From: quobix Date: Tue, 20 Feb 2024 19:29:01 -0500 Subject: [PATCH] added limits for git functions and flattened reports The report command is now flat, and much easier to consume. Designed to meet the needs of extracto. Signed-off-by: quobix --- cmd/console.go | 6 +++++- cmd/html_report.go | 6 +++--- cmd/report.go | 20 ++++++++++++-------- cmd/summary.go | 31 +++++++++++++++++-------------- 4 files changed, 37 insertions(+), 26 deletions(-) diff --git a/cmd/console.go b/cmd/console.go index 8bf88e9..103cf20 100644 --- a/cmd/console.go +++ b/cmd/console.go @@ -216,6 +216,9 @@ func GetConsoleCommand() *cobra.Command { // boot. app := tui.BuildApplication(commits, Version) + if app == nil { + return errors.New("console is unable to start") + } if err := app.Run(); err != nil { pterm.Error.Println("console is unable to start, are you running this inside a container?") pterm.Error.Println("the console requires a terminal to run, it cannot run on a headless system.") @@ -223,6 +226,7 @@ func GetConsoleCommand() *cobra.Command { fmt.Println() return err } + return nil } else { go listenForUpdates(updateChan, errorChan) @@ -310,7 +314,7 @@ func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int, filePath, gitPath), false, updateChan) // build commit history. - commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan) + commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit) if err != nil { close(updateChan) model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(err)), errorChan) diff --git a/cmd/html_report.go b/cmd/html_report.go index e5dcef7..32bfaf7 100644 --- a/cmd/html_report.go +++ b/cmd/html_report.go @@ -225,7 +225,7 @@ func GetHTMLReportCommand() *cobra.Command { go listenForUpdates(updateChan, errorChan) report, _, er := RunGitHistoryHTMLReport(args[0], args[1], latestFlag, cdnFlag, - updateChan, errorChan, baseFlag, remoteFlag) + updateChan, errorChan, baseFlag, remoteFlag, limitFlag) <-doneChan if er != nil { for x := range er { @@ -302,7 +302,7 @@ func ExtractGithubDetailsFromURL(url *url.URL) (string, string, string, error) { } func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool, - progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]byte, []*model.Report, []error) { + progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) ([]byte, []*model.Report, []error) { if gitPath == "" || filePath == "" { err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f") model.SendProgressError("reading paths", @@ -312,7 +312,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool, } // build commit history. - commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan) + commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, limit) if err != nil { model.SendFatalError("extraction", fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan) diff --git a/cmd/report.go b/cmd/report.go index 8acfa47..b53a5b2 100644 --- a/cmd/report.go +++ b/cmd/report.go @@ -109,8 +109,8 @@ func GetReportCommand() *cobra.Command { } return er[0] } - - jsonBytes, _ := json.MarshalIndent(report, "", " ") + flat := FlattenHistoricalReport(report) + jsonBytes, _ := json.MarshalIndent(flat, "", " ") fmt.Println(string(jsonBytes)) return nil } @@ -152,7 +152,8 @@ func GetReportCommand() *cobra.Command { go listenForUpdates(updateChan, errorChan) - report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag) + report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, + remoteFlag, limitFlag) <-doneChan @@ -164,13 +165,14 @@ func GetReportCommand() *cobra.Command { } if er != nil { + pterm.Error.Println("errors occurred while processing the git history") for x := range er { pterm.Error.Println(er[x].Error()) } return er[0] } - - jsonBytes, _ := json.MarshalIndent(report, "", " ") + flat := FlattenHistoricalReport(report) + jsonBytes, _ := json.MarshalIndent(flat, "", " ") fmt.Println(string(jsonBytes)) return nil @@ -204,7 +206,9 @@ func GetReportCommand() *cobra.Command { return nil } - jsonBytes, _ := json.MarshalIndent(report, "", " ") + // flatten report + flat := FlattenReport(report) + jsonBytes, _ := json.MarshalIndent(flat, "", " ") fmt.Println(string(jsonBytes)) return nil } @@ -218,7 +222,7 @@ func GetReportCommand() *cobra.Command { } func runGitHistoryReport(gitPath, filePath string, latest bool, - updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) (*model.HistoricalReport, []error) { + updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) (*model.HistoricalReport, []error) { if gitPath == "" || filePath == "" { err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f") @@ -232,7 +236,7 @@ func runGitHistoryReport(gitPath, filePath string, latest bool, filePath, gitPath), false, updateChan) // build commit history. - commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan) + commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit) if err != nil { model.SendProgressError("git", fmt.Sprintf("%d errors found building history", len(err)), errorChan) close(updateChan) diff --git a/cmd/summary.go b/cmd/summary.go index c8542c5..dd180d7 100644 --- a/cmd/summary.go +++ b/cmd/summary.go @@ -96,9 +96,7 @@ func GetSummaryCommand() *cobra.Command { } else { if !failed { spinner.Success("completed") - spinner.Stop() - pterm.Println() - pterm.Println() + fmt.Println() } else { spinner.Fail("failed to complete. sorry!") } @@ -200,7 +198,7 @@ func GetSummaryCommand() *cobra.Command { go listenForUpdates(updateChan, errorChan) - err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag) + err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag, limitFlag) <-doneChan @@ -208,6 +206,8 @@ func GetSummaryCommand() *cobra.Command { pterm.Error.Println(err.Error()) return err } + + return nil } else { go listenForUpdates(updateChan, errorChan) @@ -321,11 +321,11 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda model.SendProgressUpdate("extraction", fmt.Sprintf("extracted %d commits from history", len(commits)), true, updateChan) - close(updateChan) e := printSummaryDetails(commits) if e != nil { return []error{e} } + close(updateChan) return nil } @@ -347,7 +347,7 @@ func runGithubHistorySummary(username, repo, filePath string, latest bool, limit } func runGitHistorySummary(gitPath, filePath string, latest bool, - updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) error { + updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) error { if gitPath == "" || filePath == "" { err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f") model.SendProgressError("git", err.Error(), errorChan) @@ -359,7 +359,7 @@ func runGitHistorySummary(gitPath, filePath string, latest bool, filePath, gitPath), false, updateChan) // build commit history. - commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan) + commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit) if errs != nil { model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(errs)), errorChan) close(updateChan) @@ -372,12 +372,13 @@ func runGitHistorySummary(gitPath, filePath string, latest bool, if latest { commitHistory = commitHistory[:1] } - model.SendProgressUpdate("extraction", - fmt.Sprintf("extracted %d commits from history", len(commitHistory)), true, updateChan) - close(updateChan) + err := printSummaryDetails(commitHistory) - return printSummaryDetails(commitHistory) + model.SendProgressUpdate("extraction", + fmt.Sprintf("extracted %d commits from history\n", len(commitHistory)), true, updateChan) + close(updateChan) + return err } func printSummaryDetails(commitHistory []*model.Commit) error { @@ -439,12 +440,14 @@ func printSummaryDetails(commitHistory []*model.Commit) error { } if c < len(commitHistory) { - pterm.Println() + //pterm.Println() } } else { if tt <= 0 && tb <= 0 { - pterm.Success.Println("No changes detected") - pterm.Println() + if c+1 < len(commitHistory) { + pterm.Print(pterm.Green(fmt.Sprintf("No changes detected between %s and %s\n", + commitHistory[c].Hash, commitHistory[c+1].Hash))) + } } } }