From 5b9771f143493e200ccc5941414bfeed17c97fe3 Mon Sep 17 00:00:00 2001 From: quobix Date: Sat, 25 Nov 2023 19:06:25 -0500 Subject: [PATCH] added file details extracted from rolodex. Signed-off-by: quobix --- cmd/lint.go | 41 ++++++++++++++++++++++++++++++---------- cmd/shared_functions.go | 19 +++++++++++++++++++ motor/rule_applicator.go | 40 +++++++++++++++++++++++++++++---------- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/cmd/lint.go b/cmd/lint.go index 36a05d18..1f1f2024 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -48,6 +48,7 @@ func GetLintCommand() *cobra.Command { noStyleFlag, _ := cmd.Flags().GetBool("no-style") baseFlag, _ := cmd.Flags().GetString("base") skipCheckFlag, _ := cmd.Flags().GetBool("skip-check") + remoteFlag, _ := cmd.Flags().GetBool("remote") // disable color and styling, for CI/CD use. // https://github.com/daveshanley/vacuum/issues/234 @@ -75,8 +76,18 @@ func GetLintCommand() *cobra.Command { } // setup logging - handler := pterm.NewSlogHandler(&pterm.DefaultLogger) - pterm.DefaultLogger.Level = pterm.LogLevelError + handler := pterm.NewSlogHandler(&pterm.Logger{ + Formatter: pterm.LogFormatterColorful, + Writer: os.Stdout, + Level: pterm.LogLevelError, + ShowTime: false, + MaxWidth: 280, + KeyStyles: map[string]pterm.Style{ + "error": *pterm.NewStyle(pterm.FgRed, pterm.Bold), + "err": *pterm.NewStyle(pterm.FgRed, pterm.Bold), + "caller": *pterm.NewStyle(pterm.FgGray, pterm.Bold), + }, + }) logger := slog.New(handler) defaultRuleSets := rulesets.BuildDefaultRuleSetsWithLogger(logger) @@ -121,6 +132,9 @@ func GetLintCommand() *cobra.Command { } start := time.Now() + + var filesProcessedSize int64 + var filesProcessed int var size int64 for i, arg := range args { @@ -135,6 +149,7 @@ func GetLintCommand() *cobra.Command { lfr := lintFileRequest{ fileName: arg, baseFlag: baseFlag, + remote: remoteFlag, multiFile: mf, skipCheckFlag: skipCheckFlag, silent: silent, @@ -152,7 +167,12 @@ func GetLintCommand() *cobra.Command { lock: &printLock, logger: logger, } - errs = append(errs, lintFile(lfr)) + fs, fp, err := lintFile(lfr) + + filesProcessedSize = filesProcessedSize + fs + size + filesProcessed = filesProcessed + fp + 1 + + errs = append(errs, err) doneChan <- true }(doneChan, i, arg) } @@ -171,7 +191,7 @@ func GetLintCommand() *cobra.Command { duration := time.Since(start) - RenderTime(timeFlag, duration, size) + RenderTimeAndFiles(timeFlag, duration, filesProcessedSize, filesProcessed) if len(errs) > 0 { return errors.Join(errs...) @@ -219,6 +239,7 @@ type lintFileRequest struct { fileName string baseFlag string multiFile bool + remote bool skipCheckFlag bool silent bool detailsFlag bool @@ -236,7 +257,7 @@ type lintFileRequest struct { logger *slog.Logger } -func lintFile(req lintFileRequest) error { +func lintFile(req lintFileRequest) (int64, int, error) { // read file. specBytes, ferr := os.ReadFile(req.fileName) @@ -247,7 +268,7 @@ func lintFile(req lintFileRequest) error { pterm.Error.Printf("Unable to read file '%s': %s\n", req.fileName, ferr.Error()) pterm.Println() - return ferr + return 0, 0, ferr } @@ -257,7 +278,7 @@ func lintFile(req lintFileRequest) error { SpecFileName: req.fileName, CustomFunctions: req.functions, Base: req.baseFlag, - AllowLookup: true, + AllowLookup: req.remote, SkipDocumentCheck: req.skipCheckFlag, Logger: req.logger, }) @@ -269,7 +290,7 @@ func lintFile(req lintFileRequest) error { pterm.Error.Printf("unable to process spec '%s', error: %s", req.fileName, err.Error()) pterm.Println() } - return fmt.Errorf("linting failed due to %d issues", len(result.Errors)) + return result.FileSize, result.FilesProcessed, fmt.Errorf("linting failed due to %d issues", len(result.Errors)) } resultSet := model.NewRuleResultSet(results) @@ -281,7 +302,7 @@ func lintFile(req lintFileRequest) error { defer req.lock.Unlock() if !req.detailsFlag { RenderSummary(resultSet, req.silent, req.totalFiles, req.fileIndex, req.fileName, req.failSeverityFlag) - return CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs) + return result.FileSize, result.FilesProcessed, CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs) } abs, _ := filepath.Abs(req.fileName) @@ -292,7 +313,7 @@ func lintFile(req lintFileRequest) error { RenderSummary(resultSet, req.silent, req.totalFiles, req.fileIndex, req.fileName, req.failSeverityFlag) - return CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs) + return result.FileSize, result.FilesProcessed, CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs) } func processResults(results []*model.RuleFunctionResult, specData []string, snippets, errors bool, silent bool, abs, filename string) { diff --git a/cmd/shared_functions.go b/cmd/shared_functions.go index 34022ee7..0b6bb203 100644 --- a/cmd/shared_functions.go +++ b/cmd/shared_functions.go @@ -8,6 +8,8 @@ import ( "github.com/daveshanley/vacuum/model" "github.com/daveshanley/vacuum/plugin" "github.com/daveshanley/vacuum/rulesets" + "github.com/dustin/go-humanize" + "github.com/pb33f/libopenapi/index" "github.com/pterm/pterm" "time" ) @@ -27,6 +29,23 @@ func BuildRuleSetFromUserSuppliedSet(rsBytes []byte, rs rulesets.RuleSets) (*rul return rs.GenerateRuleSetFromSuppliedRuleSet(userRS), nil } +// RenderTimeAndFiles will render out the time taken to process a specification, and the size of the file in kb. +// it will also render out how many files were processed. +func RenderTimeAndFiles(timeFlag bool, duration time.Duration, fileSize int64, totalFiles int) { + if timeFlag { + pterm.Println() + l := "milliseconds" + d := fmt.Sprintf("%d", duration.Milliseconds()) + if duration.Milliseconds() > 1000 { + l = "seconds" + d = humanize.FormatFloat("##.##", duration.Seconds()) + } + pterm.Info.Println(fmt.Sprintf("vacuum took %s %s to lint %s across %d files", d, l, + index.HumanFileSize(float64(fileSize)), totalFiles)) + pterm.Println() + } +} + // RenderTime will render out the time taken to process a specification, and the size of the file in kb. func RenderTime(timeFlag bool, duration time.Duration, fi int64) { if timeFlag { diff --git a/motor/rule_applicator.go b/motor/rule_applicator.go index 494d0f86..1022d5a6 100644 --- a/motor/rule_applicator.go +++ b/motor/rule_applicator.go @@ -6,8 +6,10 @@ package motor import ( "errors" "fmt" + "io" "log/slog" "net/url" + "os" "path/filepath" "sync" @@ -64,7 +66,8 @@ type RuleSetExecutionResult struct { Index *index.SpecIndex // The index that was created from the specification, used by the rules. SpecInfo *datamodel.SpecInfo // A reference to the SpecInfo object, used by all the rules. Errors []error // Any errors that were returned. - + FilesProcessed int // number of files extracted by the rolodex + FileSize int64 // total filesize loaded by the rolodex } // todo: move copy into virtual file system or some kind of map. @@ -98,9 +101,30 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult { // add new pretty logger. if execution.Logger == nil { - handler := pterm.NewSlogHandler(&pterm.DefaultLogger) - docConfig.Logger = slog.New(handler) - pterm.DefaultLogger.Level = pterm.LogLevelError + var logger *slog.Logger + if execution.SilenceLogs { + // logger that goes to no-where + logger = slog.New(slog.NewJSONHandler(io.Discard, &slog.HandlerOptions{ + Level: slog.LevelError, + })) + } else { + handler := pterm.NewSlogHandler(&pterm.Logger{ + Formatter: pterm.LogFormatterColorful, + Writer: os.Stdout, + Level: pterm.LogLevelError, + ShowTime: false, + MaxWidth: 280, + KeyStyles: map[string]pterm.Style{ + "error": *pterm.NewStyle(pterm.FgRed, pterm.Bold), + "err": *pterm.NewStyle(pterm.FgRed, pterm.Bold), + "caller": *pterm.NewStyle(pterm.FgGray, pterm.Bold), + }, + }) + logger = slog.New(handler) + pterm.DefaultLogger.Level = pterm.LogLevelError + } + docConfig.Logger = logger + } else { docConfig.Logger = execution.Logger } @@ -231,9 +255,6 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult { indexResolved = rolodexResolved.GetRootIndex() indexUnresolved = rolodexUnresolved.GetRootIndex() - rolodexResolved.BuildIndexes() - rolodexUnresolved.BuildIndexes() - // we only resolve one. rolodexResolved.Resolve() @@ -250,9 +271,6 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult { indexResolved = rolodexResolved.GetRootIndex() indexUnresolved = rolodexUnresolved.GetRootIndex() - rolodexResolved.BuildIndexes() - rolodexUnresolved.BuildIndexes() - // we only resolve one. rolodexResolved.Resolve() @@ -402,6 +420,8 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult { Index: indexResolved, SpecInfo: specInfo, Errors: errs, + FilesProcessed: rolodexResolved.RolodexTotalFiles(), + FileSize: rolodexResolved.RolodexFileSize(), } }