Skip to content

Commit

Permalink
fix: remote and relative refs are configutrable #251 #231
Browse files Browse the repository at this point in the history
Using the new `-p` or `--base` globals flags, a base path or base URL can be provided, to tell vacuum where to look for references when trying to resolve them.
  • Loading branch information
daveshanley committed Mar 27, 2023
1 parent 85ded3b commit adc885a
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 11 deletions.
7 changes: 6 additions & 1 deletion cmd/build_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"os"
)

func BuildResults(rulesetFlag string, specBytes []byte, customFunctions map[string]model.RuleFunction) (*model.RuleResultSet, *motor.RuleSetExecutionResult, error) {
func BuildResults(
rulesetFlag string,
specBytes []byte,
customFunctions map[string]model.RuleFunction,
base string) (*model.RuleResultSet, *motor.RuleSetExecutionResult, error) {

// read spec and parse
defaultRuleSets := rulesets.BuildDefaultRuleSets()
Expand Down Expand Up @@ -36,6 +40,7 @@ func BuildResults(rulesetFlag string, specBytes []byte, customFunctions map[stri
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
Base: base,
})

resultSet := model.NewRuleResultSet(ruleset.Results)
Expand Down
2 changes: 1 addition & 1 deletion cmd/build_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
)

func TestBuildResults(t *testing.T) {
_, _, err := BuildResults("nuggets", nil, nil)
_, _, err := BuildResults("nuggets", nil, nil, "")
assert.Error(t, err)
}
18 changes: 16 additions & 2 deletions cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"net/url"
"time"
)

Expand All @@ -39,6 +40,7 @@ func GetDashboardCommand() *cobra.Command {
pterm.Println()
return errors.New(errText)
}
baseFlag, _ := cmd.Flags().GetString("base")

var err error
vacuumReport, specBytes, _ := vacuum_report.BuildVacuumReportFromFile(args[0])
Expand All @@ -59,7 +61,7 @@ func GetDashboardCommand() *cobra.Command {
customFunctions, _ := LoadCustomFunctions(functionsFlag)

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions)
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions, baseFlag)
if err != nil {
pterm.Error.Printf("Failed to render dashboard: %v\n\n", err)
return err
Expand All @@ -82,7 +84,19 @@ func GetDashboardCommand() *cobra.Command {
return err
}

config := index.CreateOpenAPIIndexConfig()
config := index.CreateClosedAPIIndexConfig()
if baseFlag != "" {
u, e := url.Parse(baseFlag)
if e == nil && u.Scheme != "" && u.Host != "" {
config.BaseURL = u
config.BasePath = ""
} else {
config.BasePath = baseFlag
}
config.AllowFileLookup = true
config.AllowRemoteLookup = true
}

specIndex = index.NewSpecIndexWithConfig(&rootNode, config)

specInfo = vacuumReport.SpecInfo
Expand Down
3 changes: 2 additions & 1 deletion cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func GetHTMLReportCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

noStyleFlag, _ := cmd.Flags().GetBool("no-style")
baseFlag, _ := cmd.Flags().GetString("base")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -91,7 +92,7 @@ func GetHTMLReportCommand() *cobra.Command {
customFunctions, _ := LoadCustomFunctions(functionsFlag)

rulesetFlag, _ := cmd.Flags().GetString("ruleset")
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions)
resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions, baseFlag)
if err != nil {
pterm.Error.Printf("Failed to generate report: %v\n\n", err)
return err
Expand Down
2 changes: 2 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func GetLintCommand() *cobra.Command {
functionsFlag, _ := cmd.Flags().GetString("functions")
failSeverityFlag, _ := cmd.Flags().GetString("fail-severity")
noStyleFlag, _ := cmd.Flags().GetBool("no-style")
baseFlag, _ := cmd.Flags().GetString("base")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -106,6 +107,7 @@ func GetLintCommand() *cobra.Command {
RuleSet: selectedRS,
Spec: specBytes,
CustomFunctions: customFunctions,
Base: baseFlag,
})

results := result.Results
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func init() {
rootCmd.PersistentFlags().BoolP("time", "t", false, "Show how long vacuum took to run")
rootCmd.PersistentFlags().StringP("ruleset", "r", "", "Path to a spectral ruleset configuration")
rootCmd.PersistentFlags().StringP("functions", "f", "", "Path to custom functions")
rootCmd.PersistentFlags().StringP("base", "p", "", "Base URL or path to use for resolving relative or remote references")

regErr := rootCmd.RegisterFlagCompletionFunc("functions", cobra.FixedCompletions(
[]string{"so"}, cobra.ShellCompDirectiveFilterFileExt,
Expand Down
2 changes: 2 additions & 0 deletions cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func GetSpectralReportCommand() *cobra.Command {
stdIn, _ := cmd.Flags().GetBool("stdin")
stdOut, _ := cmd.Flags().GetBool("stdout")
noStyleFlag, _ := cmd.Flags().GetBool("no-style")
baseFlag, _ := cmd.Flags().GetString("base")

// disable color and styling, for CI/CD use.
// https://github.com/daveshanley/vacuum/issues/234
Expand Down Expand Up @@ -133,6 +134,7 @@ func GetSpectralReportCommand() *cobra.Command {
Spec: specBytes,
CustomFunctions: customFunctions,
SilenceLogs: true,
Base: baseFlag,
})

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.6.1
github.com/pb33f/libopenapi v0.7.0
github.com/pterm/pterm v0.12.54
github.com/santhosh-tekuri/jsonschema/v5 v5.2.0
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 @@ -124,6 +124,8 @@ github.com/pb33f/libopenapi v0.6.0 h1:omj3g89XbT/RaafrzdRQFEDsyLRZsnkV7Q+1zCEhFd
github.com/pb33f/libopenapi v0.6.0/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
github.com/pb33f/libopenapi v0.6.1 h1:AXnfXFVVK+bXgUeLYPJqLk8y/8Ow4Nercx1dabT2xog=
github.com/pb33f/libopenapi v0.6.1/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
github.com/pb33f/libopenapi v0.7.0 h1:2mQgDa0UQs8/5Hd2z+KbgWvoGx0++N3ihMKihcAS5Mw=
github.com/pb33f/libopenapi v0.7.0/go.mod h1:lvUmCtjgHUGVj6WzN3I5/CS9wkXtyN3Ykjh6ZZP5lrI=
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
8 changes: 4 additions & 4 deletions model/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ type RuleFunctionResult struct {
// RuleResultSet contains all the results found during a linting run, and all the methods required to
// filter, sort and calculate counts.
type RuleResultSet struct {
Results []*RuleFunctionResult `json:"results" yaml:"results"` // All the results!
WarnCount int `json:"warningCount" yaml:"warningCount"` // Total warnings
ErrorCount int `json:"errorCount" yaml:"errorCount"` // Total errors
InfoCount int `json:"infoCount" yaml:"infoCount"` // Total info
Results []*RuleFunctionResult `json:"results,omitempty" yaml:"results,omitempty"` // All the results!
WarnCount int `json:"warningCount" yaml:"warningCount"` // Total warnings
ErrorCount int `json:"errorCount" yaml:"errorCount"` // Total errors
InfoCount int `json:"infoCount" yaml:"infoCount"` // Total info
categoryMap map[*RuleCategory][]*RuleFunctionResult `json:"-" yaml:"-"`
}

Expand Down
17 changes: 16 additions & 1 deletion motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pb33f/libopenapi/utils"
"github.com/pterm/pterm"
"gopkg.in/yaml.v3"
"net/url"
"sync"
)

Expand All @@ -40,6 +41,7 @@ type RuleSetExecution struct {
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.
Base string // The base path or URL of the specification, used for resolving relative or remote paths.
}

// RuleSetExecutionResult returns the results of running the ruleset against the supplied spec.
Expand Down Expand Up @@ -92,7 +94,20 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
specUnresolved = specInfoUnresolved.RootNode
specResolved = specInfo.RootNode

config := index.CreateOpenAPIIndexConfig()
config := index.CreateClosedAPIIndexConfig()

if execution.Base != "" {
// check if this is a URL or not
u, e := url.Parse(execution.Base)
if e == nil && u.Scheme != "" && u.Host != "" {
config.BaseURL = u
config.BasePath = ""
} else {
config.BasePath = execution.Base
}
config.AllowFileLookup = true
config.AllowRemoteLookup = true
}

// create resolved and un-resolved indexes.
indexResolved := index.NewSpecIndexWithConfig(specResolved, config)
Expand Down

0 comments on commit adc885a

Please sign in to comment.