Skip to content

Commit

Permalink
fix bug with empty type flag #2135
Browse files Browse the repository at this point in the history
  • Loading branch information
cx-joao-reigota committed Feb 25, 2021
1 parent 12baa85 commit e7eadf8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
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

0 comments on commit e7eadf8

Please sign in to comment.