Skip to content

Commit

Permalink
Adding formatted logging, cleaned some bugs
Browse files Browse the repository at this point in the history
bumped latest deps

Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Nov 18, 2023
1 parent 90e4c88 commit e83f435
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 70 deletions.
2 changes: 1 addition & 1 deletion cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func GetHTMLReportCommand() *cobra.Command {
pterm.Println()

fi, _ := os.Stat(args[0])
RenderTime(timeFlag, duration, fi)
RenderTime(timeFlag, duration, fi.Size())

return nil
},
Expand Down
34 changes: 25 additions & 9 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dustin/go-humanize"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"log/slog"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -73,7 +74,12 @@ func GetLintCommand() *cobra.Command {
mf = true
}

defaultRuleSets := rulesets.BuildDefaultRuleSets()
// setup logging
handler := pterm.NewSlogHandler(&pterm.DefaultLogger)
pterm.DefaultLogger.Level = pterm.LogLevelError
logger := slog.New(handler)

defaultRuleSets := rulesets.BuildDefaultRuleSetsWithLogger(logger)
selectedRS := defaultRuleSets.GenerateOpenAPIRecommendedRuleSet()
customFunctions, _ := LoadCustomFunctions(functionsFlag)

Expand Down Expand Up @@ -110,10 +116,18 @@ func GetLintCommand() *cobra.Command {
pterm.Println()
}

start := time.Now()
var size int64
for i, arg := range args {

go func(c chan bool, i int, arg string) {

// get size
s, _ := os.Stat(arg)
if s != nil {
size = size + s.Size()
}

lfr := lintFileRequest{
fileName: arg,
baseFlag: baseFlag,
Expand All @@ -132,6 +146,7 @@ func GetLintCommand() *cobra.Command {
selectedRS: selectedRS,
functions: customFunctions,
lock: &printLock,
logger: logger,
}
errs = append(errs, lintFile(lfr))
doneChan <- true
Expand All @@ -150,6 +165,10 @@ func GetLintCommand() *cobra.Command {
pterm.Println()
}

duration := time.Since(start)

RenderTime(timeFlag, duration, size)

if len(errs) > 0 {
return errors.Join(errs...)
}
Expand Down Expand Up @@ -210,6 +229,7 @@ type lintFileRequest struct {
selectedRS *rulesets.RuleSet
functions map[string]model.RuleFunction
lock *sync.Mutex
logger *slog.Logger
}

func lintFile(req lintFileRequest) error {
Expand All @@ -227,39 +247,35 @@ func lintFile(req lintFileRequest) error {

}

start := time.Now()
result := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{
RuleSet: req.selectedRS,
Spec: specBytes,
CustomFunctions: req.functions,
Base: req.baseFlag,
AllowLookup: true,
SkipDocumentCheck: req.skipCheckFlag,
Logger: req.logger,
})

results := result.Results

if len(result.Errors) > 0 {
for _, err := range result.Errors {
pterm.Error.Printf("linting error: %s", err.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))
}

resultSet := model.NewRuleResultSet(results)
resultSet.SortResultsByLineNumber()
fi, _ := os.Stat(req.fileName)
duration := time.Since(start)

warnings := resultSet.GetWarnCount()
errs := resultSet.GetErrorCount()
informs := resultSet.GetInfoCount()
req.lock.Lock()
defer req.lock.Unlock()
if !req.detailsFlag {
RenderSummary(resultSet, req.silent, req.totalFiles, req.fileIndex, req.fileName, req.failSeverityFlag)
RenderTime(req.timeFlag, duration, fi)
return CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs)
}

Expand Down Expand Up @@ -302,13 +318,13 @@ func lintFile(req lintFileRequest) error {
}

RenderSummary(resultSet, req.silent, req.totalFiles, req.fileIndex, req.fileName, req.failSeverityFlag)
RenderTime(req.timeFlag, duration, fi)

return CheckFailureSeverity(req.failSeverityFlag, errs, warnings, informs)
}

func processResults(results []*model.RuleFunctionResult, specData []string, snippets, errors bool, silent bool, abs, filename string) {

pterm.Println(pterm.LightMagenta(fmt.Sprintf("%s", abs)))
pterm.Println(pterm.LightMagenta(fmt.Sprintf("\n%s", abs)))
underline := make([]string, len(abs))
for x, _ := range abs {
underline[x] = "-"
Expand Down
9 changes: 6 additions & 3 deletions cmd/shared_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/daveshanley/vacuum/plugin"
"github.com/daveshanley/vacuum/rulesets"
"github.com/pterm/pterm"
"os"
"time"
)

Expand All @@ -29,10 +28,14 @@ func BuildRuleSetFromUserSuppliedSet(rsBytes []byte, rs rulesets.RuleSets) (*rul
}

// 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 os.FileInfo) {
func RenderTime(timeFlag bool, duration time.Duration, fi int64) {
if timeFlag {
pterm.Println()
pterm.Info.Println(fmt.Sprintf("vacuum took %d milliseconds to lint %dkb", duration.Milliseconds(), fi.Size()/1000))
if (fi / 1000) <= 1024 {
pterm.Info.Println(fmt.Sprintf("vacuum took %d milliseconds to lint %dkb", duration.Milliseconds(), fi/1000))
} else {
pterm.Info.Println(fmt.Sprintf("vacuum took %d milliseconds to lint %dmb", duration.Milliseconds(), fi/1000000))
}
pterm.Println()
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/shared_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ func TestRenderTime(t *testing.T) {
start := time.Now()
time.Sleep(1 * time.Millisecond)
fi, _ := os.Stat("shared_functions.go")
RenderTime(true, time.Since(start), fi)
RenderTime(true, time.Since(start), fi.Size())
}
2 changes: 1 addition & 1 deletion cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func GetSpectralReportCommand() *cobra.Command {
pterm.Println()

fi, _ := os.Stat(args[0])
RenderTime(timeFlag, duration, fi)
RenderTime(timeFlag, duration, fi.Size())

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func GetVacuumReportCommand() *cobra.Command {
pterm.Println()

fi, _ := os.Stat(args[0])
RenderTime(timeFlag, duration, fi)
RenderTime(timeFlag, duration, fi.Size())

return nil
},
Expand Down
4 changes: 3 additions & 1 deletion functions/core/casing.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func (c Casing) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext) [
rx := regexp.MustCompile(fmt.Sprintf("^%s$", pattern))
node := nodes[0]
if utils.IsNodeMap(nodes[0]) || utils.IsNodeArray(nodes[0]) {
node = nodes[0].Content[0]
if len(nodes[0].Content) > 0 {
node = nodes[0].Content[0]
}
}

if !rx.MatchString(node.Value) {
Expand Down
8 changes: 4 additions & 4 deletions functions/core/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (sch Schema) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
forceValidationOnCurrentNode := utils.ExtractValueFromInterfaceMap("forceValidationOnCurrentNode", context.Options)
if _, ok := forceValidationOnCurrentNode.(bool); ok && len(nodes) > 0 {
schema.GoLow().Index = context.Index
results = append(results, validateNodeAgainstSchema(schema, nodes[0], context, 0)...)
results = append(results, validateNodeAgainstSchema(&context, schema, nodes[0], context, 0)...)
return results
}

Expand All @@ -114,7 +114,7 @@ func (sch Schema) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)
_, field := utils.FindKeyNodeTop(context.RuleAction.Field, no)
if field != nil {
schema.GoLow().Index = context.Index
results = append(results, validateNodeAgainstSchema(schema, field, context, x)...)
results = append(results, validateNodeAgainstSchema(&context, schema, field, context, x)...)

} else {
// If the field is not found, and we're being strict, it's invalid.
Expand All @@ -138,7 +138,7 @@ func (sch Schema) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext)

var bannedErrors = []string{"if-then failed", "if-else failed", "allOf failed", "oneOf failed"}

func validateNodeAgainstSchema(schema *highBase.Schema, field *yaml.Node,
func validateNodeAgainstSchema(ctx *model.RuleFunctionContext, schema *highBase.Schema, field *yaml.Node,
context model.RuleFunctionContext, x int) []model.RuleFunctionResult {

ruleMessage := context.Rule.Description
Expand All @@ -149,7 +149,7 @@ func validateNodeAgainstSchema(schema *highBase.Schema, field *yaml.Node,
var results []model.RuleFunctionResult

// validate using schema provided.
res, resErrors := parser.ValidateNodeAgainstSchema(schema, field, false)
res, resErrors := parser.ValidateNodeAgainstSchema(ctx, schema, field, false)

if res {
return results
Expand Down
16 changes: 8 additions & 8 deletions functions/openapi/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (ex Examples) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext
}
}

ctxTimeout, cancel := ctx.WithTimeout(ctx.Background(), time.Second*2)
ctxTimeout, cancel := ctx.WithTimeout(ctx.Background(), time.Second*1)
defer cancel()
f := make(chan bool)
go func() {
Expand All @@ -192,8 +192,8 @@ func (ex Examples) RunRule(nodes []*yaml.Node, context model.RuleFunctionContext

select {
case <-ctxTimeout.Done():
pterm.Warning.Println("examples rule timed-out after two seconds, trying to scan examples for " +
"response bodies, request bodies, and parameters")
pterm.Warning.Println("bug: examples function timed-out after a second, trying to scan examples for " +
"response bodies, request bodies, and parameters. Disable rules that use the example function checking for this spec. Please report this!")
return *results
case <-f:
// ok
Expand Down Expand Up @@ -325,10 +325,10 @@ func checkDefinitionForExample(componentNode *yaml.Node, compName string,
}

if schema != nil && schema.Type != nil && isArr && exValue != nil {
res, errs = parser.ValidateNodeAgainstSchema(schema, exValue, true)
res, errs = parser.ValidateNodeAgainstSchema(&context, schema, exValue, true)
}
if schema != nil && schema.Type != nil && !isArr && exValue != nil {
res, errs = parser.ValidateNodeAgainstSchema(schema, exValue, false)
res, errs = parser.ValidateNodeAgainstSchema(&context, schema, exValue, false)
}

// TODO: handle enums in here.
Expand Down Expand Up @@ -375,7 +375,7 @@ func checkDefinitionForExample(componentNode *yaml.Node, compName string,

var errorResults []*validationErrors.ValidationError
if topExValue != nil {
_, errorResults = parser.ValidateNodeAgainstSchema(schema, topExValue, false)
_, errorResults = parser.ValidateNodeAgainstSchema(&context, schema, topExValue, false)
}

// extract all validation errors.
Expand Down Expand Up @@ -524,7 +524,7 @@ func analyzeExample(nameNodeValue string, mediaTypeNode *yaml.Node, basePath str

}

res, errs := parser.ValidateNodeAgainstSchema(convertedSchema, valueNode, false)
res, errs := parser.ValidateNodeAgainstSchema(&context, convertedSchema, valueNode, false)

if !res {
// extract all validation errors.
Expand Down Expand Up @@ -606,7 +606,7 @@ func analyzeExample(nameNodeValue string, mediaTypeNode *yaml.Node, basePath str

//return results

res, validateError := parser.ValidateNodeAgainstSchema(schema, eValue, false)
res, validateError := parser.ValidateNodeAgainstSchema(&context, schema, eValue, false)

var schemaErrors []*validationErrors.SchemaValidationFailure
for i := range validateError {
Expand Down
15 changes: 7 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ require (
github.com/gizak/termui/v3 v3.1.0
github.com/json-iterator/go v1.1.12
github.com/mitchellh/mapstructure v1.5.0
github.com/pb33f/libopenapi v0.13.9
github.com/pb33f/libopenapi-validator v0.0.27
github.com/pterm/pterm v0.12.69
github.com/pb33f/libopenapi v0.13.11
github.com/pb33f/libopenapi-validator v0.0.28
github.com/pterm/pterm v0.12.70
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/vmware-labs/yaml-jsonpath v0.3.2
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa
gopkg.in/yaml.v3 v3.0.1
)

Expand Down Expand Up @@ -58,8 +57,8 @@ require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.11.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit e83f435

Please sign in to comment.