Skip to content

Commit

Permalink
Adding in support for remote file systems and a master remote switch
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Nov 3, 2023
1 parent 77411f7 commit ae2b2cc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func GetLintCommand() *cobra.Command {
failSeverityFlag, _ := cmd.Flags().GetString("fail-severity")
noStyleFlag, _ := cmd.Flags().GetBool("no-style")
baseFlag, _ := cmd.Flags().GetString("base")
remoteFlag, _ := cmd.Flags().GetBool("remote")
skipCheckFlag, _ := cmd.Flags().GetBool("skip-check")

// disable color and styling, for CI/CD use.
Expand Down Expand Up @@ -109,6 +110,7 @@ func GetLintCommand() *cobra.Command {
Spec: specBytes,
CustomFunctions: customFunctions,
Base: baseFlag,
AllowLookup: remoteFlag,
SkipDocumentCheck: skipCheckFlag,
})

Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func GetRootCommand() *cobra.Command {
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")
rootCmd.PersistentFlags().BoolP("remote", "u", false, "Allow local files and remote (http) references to be looked up")
rootCmd.PersistentFlags().BoolP("skip-check", "k", false, "Skip checking for a valid OpenAPI document, useful for linting fragments or non-OpenAPI documents")

regErr := rootCmd.RegisterFlagCompletionFunc("functions", cobra.FixedCompletions(
Expand Down
19 changes: 16 additions & 3 deletions motor/build_rolodex.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func BuildRolodexFromIndexConfig(indexConfig *index.SpecIndexConfig) (*index.Rol
rolodex := index.NewRolodex(indexConfig)

// we need to create a local filesystem for the rolodex.
if indexConfig.BasePath != "" {
if indexConfig.AllowFileLookup {
cwd, absErr := filepath.Abs(indexConfig.BasePath)
if absErr != nil {
return nil, absErr
Expand All @@ -31,8 +31,21 @@ func BuildRolodexFromIndexConfig(indexConfig *index.SpecIndexConfig) (*index.Rol
rolodex.AddLocalFS(cwd, fileFS)
}

// TODO: Remote filesystem
if indexConfig.AllowRemoteLookup {

return rolodex, nil
// create a remote filesystem
remoteFS, err := index.NewRemoteFSWithConfig(indexConfig)
if err != nil {
return nil, err
}

// add the filesystem to the rolodex
if indexConfig.BaseURL == nil {
rolodex.AddRemoteFS("root", remoteFS)
} else {
rolodex.AddRemoteFS(indexConfig.BaseURL.String(), remoteFS)
}
}

return rolodex, nil
}
8 changes: 8 additions & 0 deletions motor/rule_applicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type RuleSetExecution struct {
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.
AllowLookup bool // Allow remote lookup of files or links
Document libopenapi.Document // a ready to render model.
SkipDocumentCheck bool // Skip the document check, useful for fragments and non openapi specs.
}
Expand Down Expand Up @@ -101,12 +102,19 @@ func ApplyRulesToRuleSet(execution *RuleSetExecution) *RuleSetExecutionResult {
indexConfig.BasePath = ""
docConfig.BaseURL = u
docConfig.BasePath = ""
indexConfig.AllowRemoteLookup = true
} else {
indexConfig.AllowFileLookup = true
indexConfig.BasePath = execution.Base
docConfig.BasePath = execution.Base
}
}

if execution.AllowLookup {
indexConfig.AllowFileLookup = true
indexConfig.AllowRemoteLookup = true
}

if execution.SkipDocumentCheck {
docConfig.BypassDocumentCheck = true
}
Expand Down

0 comments on commit ae2b2cc

Please sign in to comment.