Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug with empty type flag #2135 #2137

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/console/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func scan() error {
Add(&yamlParser.Parser{}).
Add(terraformParser.NewDefault()).
Add(&dockerParser.Parser{}).
Build(types)
Build(querySource.Types)
if err != nil {
return err
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/engine/query/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
)

// FilesystemSource this type defines a struct with a path to a filesystem source of queries
// Source is the path to the queries
// Types are the types given by the flag --type for query selection mechanism
type FilesystemSource struct {
source string
types []string
Source string
Types []string
}

const (
Expand All @@ -33,9 +35,12 @@ const (

// NewFilesystemSource initializes a NewFilesystemSource with source to queries and types of queries to load
func NewFilesystemSource(source string, types []string) *FilesystemSource {
if len(types) == 0 {
types = []string{""}
}
return &FilesystemSource{
source: filepath.FromSlash(source),
types: types,
Source: filepath.FromSlash(source),
Types: types,
}
}

Expand Down Expand Up @@ -69,7 +74,7 @@ func GetPathToLibrary(platform, relativeBasePath string) string {

// GetGenericQuery returns the library.rego for the platform passed in the argument
func (s *FilesystemSource) GetGenericQuery(platform string) (string, error) {
pathToLib := GetPathToLibrary(platform, s.source)
pathToLib := GetPathToLibrary(platform, s.Source)

content, err := ioutil.ReadFile(filepath.Clean(pathToLib))
if err != nil {
Expand All @@ -84,8 +89,8 @@ func (s *FilesystemSource) CheckType(queryPlatform interface{}) bool {
if queryPlatform.(string) == "Common" {
return true
}
if s.types[0] != "" {
return strings.Contains(strings.ToUpper(strings.Join(s.types, ",")), strings.ToUpper(queryPlatform.(string)))
if s.Types[0] != "" {
return strings.Contains(strings.ToUpper(strings.Join(s.Types, ",")), strings.ToUpper(queryPlatform.(string)))
}
return true
}
Expand All @@ -94,7 +99,7 @@ func (s *FilesystemSource) CheckType(queryPlatform interface{}) bool {
// QueryMetadata struct
func (s *FilesystemSource) GetQueries() ([]model.QueryMetadata, error) {
queryDirs := make([]string, 0)
err := filepath.Walk(s.source,
err := filepath.Walk(s.Source,
func(p string, f os.FileInfo, err error) error {
if err != nil {
return err
Expand Down