-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: distinction between lint and compile source files
In order to support differential linting against Git repository branches, linters need to be able to determine which files should be compared by looking at both the "from" and "to" file sources. This change introduces support for this, while also making the necessary distinction between collections of files that need to be compared for linting, and files that need to be compiled into aggregate OpenAPI specs.
- Loading branch information
Showing
12 changed files
with
324 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package files | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
|
||
"github.com/snyk/vervet" | ||
"github.com/snyk/vervet/config" | ||
) | ||
|
||
// FileSource defines a source of spec files to lint. This abstraction allows | ||
// linters to operate seamlessly over version control systems and local files. | ||
type FileSource interface { | ||
// Match returns a slice of logical paths to spec files that should be | ||
// linted from the given resource set configuration. | ||
Match(*config.ResourceSet) ([]string, error) | ||
|
||
// Fetch retrieves the contents of the requested logical path as a local | ||
// file and returns the absolute path where it may be found. An empty | ||
// string, rather than an error, is returned if the file does not exist. | ||
Fetch(path string) (string, error) | ||
|
||
// Close releases any resources consumed in content retrieval. Any files | ||
// returned by Fetch will no longer be available after calling Close, and | ||
// any further calls to Fetch will error. | ||
Close() error | ||
} | ||
|
||
// NilSource is a FileSource that does not have any files in it. | ||
type NilSource struct{} | ||
|
||
// Match implements FileSource. | ||
func (NilSource) Match(*config.ResourceSet) ([]string, error) { return nil, nil } | ||
|
||
// Fetch implements FileSource. | ||
func (NilSource) Fetch(path string) (string, error) { | ||
return "", nil | ||
} | ||
|
||
// Close implements FileSource. | ||
func (NilSource) Close() error { return nil } | ||
|
||
// LocalFSSource is a FileSource that resolves files from the local filesystem | ||
// relative to the current working directory. | ||
type LocalFSSource struct{} | ||
|
||
// Match implements FileSource. | ||
func (LocalFSSource) Match(rcConfig *config.ResourceSet) ([]string, error) { | ||
var result []string | ||
err := doublestar.GlobWalk(os.DirFS(rcConfig.Path), | ||
vervet.SpecGlobPattern, | ||
func(path string, d fs.DirEntry) error { | ||
rcPath := filepath.Join(rcConfig.Path, path) | ||
for i := range rcConfig.Excludes { | ||
if ok, err := doublestar.Match(rcConfig.Excludes[i], rcPath); ok { | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
result = append(result, rcPath) | ||
return nil | ||
}) | ||
return result, err | ||
} | ||
|
||
// Fetch implements FileSource. | ||
func (LocalFSSource) Fetch(path string) (string, error) { | ||
if _, err := os.Stat(path); err == nil { | ||
return filepath.Abs(path) | ||
} else if os.IsNotExist(err) { | ||
return "", nil | ||
} else { | ||
return "", err | ||
} | ||
} | ||
|
||
// Close implements FileSource. | ||
func (LocalFSSource) Close() error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.