Skip to content

Commit

Permalink
feat: support for stdin and stdout on reports #220
Browse files Browse the repository at this point in the history
stdin and stdout can be used for machine readable reports instead of reading and writing files now. Docs have been updated:

Also updated to latest `libopenapi`

- https://quobix.com/vacuum/commands/spectral-report/
- https://quobix.com/vacuum/commands/report/
  • Loading branch information
daveshanley committed Dec 30, 2022
1 parent e3edfc0 commit b53a368
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func GetSpectralReportCommand() *cobra.Command {
Use: "spectral-report",
Short: "Generate a Spectral compatible JSON report",
Long: "Generate a JSON report using the same model as Spectral. Default output " +
"filename is 'vacuum-spectral-report.json' located in the working directory.",
"filename is 'vacuum-spectral-report.json' located in the working directory. " +
"Use the -i flag for using stdin instead of reading a file, and -o for stdout, instead of writing to a file.",
Example: "vacuum spectral-report my-awesome-spec.yaml <vacuum-spectral-report.json>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
switch len(args) {
Expand Down Expand Up @@ -123,6 +124,7 @@ func GetSpectralReportCommand() *cobra.Command {
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
})

resultSet := model.NewRuleResultSet(ruleset.Results)
Expand Down
6 changes: 4 additions & 2 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func GetVacuumReportCommand() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
Use: "report",
Short: "Generate a vacuum sealed, replayable report",
Short: "Generate a vacuum sealed, re-playable report",
Long: "Generate a full report of a linting run. This can be used as a result set, or can be used to replay a linting run. " +
"the default filename is 'vacuum-report-MM-DD-YY-HH_MM_SS.json' located in the working directory.",
"the default filename is 'vacuum-report-MM-DD-YY-HH_MM_SS.json' located in the working directory. " +
"Use the -i flag for using stdin instead of reading a file, and -o for stdout, instead of writing to a file.",
Example: "vacuum report <my-awesome-spec.yaml> <report-prefix>",
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) == 0 {
Expand Down Expand Up @@ -124,6 +125,7 @@ func GetVacuumReportCommand() *cobra.Command {
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
})

resultSet := model.NewRuleResultSet(ruleset.Results)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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.4.6
github.com/pb33f/libopenapi v0.4.10
github.com/pterm/pterm v0.12.50
github.com/santhosh-tekuri/jsonschema/v5 v5.1.1
github.com/spf13/cobra v1.6.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/pb33f/libopenapi v0.4.5 h1:99h9A7ojryM74GFnt4npT0Z6lLcDrlBOgZ2ZFBbu/3
github.com/pb33f/libopenapi v0.4.5/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pb33f/libopenapi v0.4.6 h1:DSgP3ifXouTgTgEAwMIIaH99tCcex4n3z9VuRNvMNv4=
github.com/pb33f/libopenapi v0.4.6/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pb33f/libopenapi v0.4.10 h1:tP1sOW3TVAimr+mzSbgf01QgTCc2oWnLHqE54/fVcaw=
github.com/pb33f/libopenapi v0.4.10/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
7 changes: 6 additions & 1 deletion motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ruleContext struct {
specInfo *datamodel.SpecInfo
customFunctions map[string]model.RuleFunction
panicFunc func(p any)
silenceLogs bool
}

// RuleSetExecution is an instruction set for executing a ruleset. It's a convenience structure to allow the signature
Expand All @@ -38,6 +39,7 @@ type RuleSetExecution struct {
SpecInfo *datamodel.SpecInfo // Pre-parsed spec-info.
CustomFunctions map[string]model.RuleFunction // custom functions loaded from plugin.
PanicFunction func(p any) // In case of emergency, do this thing here.
SilenceLogs bool // Prevent any warnings about rules/rule-sets being printed.
}

// RuleSetExecutionResult returns the results of running the ruleset against the supplied spec.
Expand Down Expand Up @@ -155,6 +157,7 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
specInfo: specInfo,
index: ruleIndex,
customFunctions: execution.CustomFunctions,
silenceLogs: execution.SilenceLogs,
}
if execution.PanicFunction != nil {
ctx.panicFunc = execution.PanicFunction
Expand Down Expand Up @@ -367,7 +370,9 @@ func buildResults(ctx ruleContext, ruleAction model.RuleAction, nodes []*yaml.No
}

if ctx.specInfo.SpecFormat == "" && ctx.specInfo.Version == "" {
pterm.Warning.Printf("Specification version not detected, cannot apply rule `%s`\n", ctx.rule.Id)
if !ctx.silenceLogs {
pterm.Warning.Printf("Specification version not detected, cannot apply rule `%s`\n", ctx.rule.Id)
}
return ctx.ruleResults
}

Expand Down

0 comments on commit b53a368

Please sign in to comment.