Skip to content

Commit

Permalink
added file details extracted from rolodex.
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Nov 26, 2023
1 parent 39a3aa1 commit 5b9771f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 20 deletions.
41 changes: 31 additions & 10 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {

Expand All @@ -135,6 +149,7 @@ func GetLintCommand() *cobra.Command {
lfr := lintFileRequest{
fileName: arg,
baseFlag: baseFlag,
remote: remoteFlag,
multiFile: mf,
skipCheckFlag: skipCheckFlag,
silent: silent,
Expand All @@ -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)
}
Expand All @@ -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...)
Expand Down Expand Up @@ -219,6 +239,7 @@ type lintFileRequest struct {
fileName string
baseFlag string
multiFile bool
remote bool
skipCheckFlag bool
silent bool
detailsFlag bool
Expand All @@ -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)

Expand All @@ -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

}

Expand All @@ -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,
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions cmd/shared_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 {
Expand Down
40 changes: 30 additions & 10 deletions motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package motor
import (
"errors"
"fmt"
"io"
"log/slog"
"net/url"
"os"
"path/filepath"
"sync"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()

Expand All @@ -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()

Expand Down Expand Up @@ -402,6 +420,8 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
Index: indexResolved,
SpecInfo: specInfo,
Errors: errs,
FilesProcessed: rolodexResolved.RolodexTotalFiles(),
FileSize: rolodexResolved.RolodexFileSize(),
}
}

Expand Down

0 comments on commit 5b9771f

Please sign in to comment.