-
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.
Merge pull request #79 from cmars/fix/optic-git-filesets
fix: proper support for optic git file sources
- Loading branch information
Showing
14 changed files
with
359 additions
and
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package linter | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/snyk/vervet/config" | ||
) | ||
|
||
// A Linter checks that a set of spec files conform to some set of rules and | ||
// standards. | ||
type Linter 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) | ||
|
||
// WithOverride returns a new instance of a Linter with the given configuration. | ||
WithOverride(ctx context.Context, cfg *config.Linter) (Linter, error) | ||
|
||
// Run executes the linter checks on the given spec files. | ||
Run(ctx context.Context, files ...string) error | ||
} |
Oops, something went wrong.