Skip to content

Commit

Permalink
change Search to take an io.Writer, add first test
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Hitchon committed Apr 21, 2018
1 parent fe5f08e commit dd33dcb
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options ApplyOpti
}
if l != nil {
if options.SearchExpression != "" {
l.Search(ruleSet, options.SearchExpression)
l.Search(ruleSet, options.SearchExpression, os.Stdout)
} else {
options := linter.Options{
Tags: options.Tags,
Expand Down
9 changes: 5 additions & 4 deletions linter/aws_resource_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linter
import (
"fmt"
"github.com/stelligent/config-lint/assertion"
"io"
)

type (
Expand Down Expand Up @@ -30,18 +31,18 @@ func (l AWSResourceLinter) Validate(ruleSet assertion.RuleSet, options Options)
}

// Search applies a JMESPath to all SecurityGroups
func (l AWSResourceLinter) Search(ruleSet assertion.RuleSet, searchExpression string) {
func (l AWSResourceLinter) Search(ruleSet assertion.RuleSet, searchExpression string, w io.Writer) {
resources, _ := l.Loader.Load()
for _, resource := range resources {
v, err := assertion.SearchData(searchExpression, resource.Properties)
if err != nil {
fmt.Println(err)
fmt.Fprintln(w, err)
} else {
s, err := assertion.JSONStringify(v)
if err != nil {
fmt.Println(err)
fmt.Fprintln(w, err)
} else {
fmt.Printf("%s: %s\n", resource.ID, s)
fmt.Fprintf(w, "%s: %s\n", resource.ID, s)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions linter/aws_resource_linter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package linter

import (
"bytes"
"github.com/stelligent/config-lint/assertion"
"strings"
"testing"
)

Expand All @@ -25,6 +27,17 @@ func TestAWSResourceLinterValidate(t *testing.T) {
}
}

func TestAWSResourceLinterSearch(t *testing.T) {
ruleSet := loadRulesForTest("./testdata/rules/aws_resource.yml", t)
mockLoader := AWSMockLoader{}
linter := AWSResourceLinter{Loader: mockLoader, ValueSource: TestingValueSource{}}
var b bytes.Buffer
linter.Search(ruleSet, "@", &b)
if !strings.Contains(b.String(), "Name") {
t.Errorf("Expecting Search to find Name in %s", b.String())
}
}

type AWSMockLoader struct{}

func (l AWSMockLoader) Load() ([]assertion.Resource, error) {
Expand Down
15 changes: 8 additions & 7 deletions linter/file_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package linter
import (
"fmt"
"github.com/stelligent/config-lint/assertion"
"io"
)

type (
Expand Down Expand Up @@ -65,26 +66,26 @@ func (fl FileLinter) Validate(ruleSet assertion.RuleSet, options Options) (asser
}

// Search evaluates a JMESPath expression against resources in a collection of filenames
func (fl FileLinter) Search(ruleSet assertion.RuleSet, searchExpression string) {
func (fl FileLinter) Search(ruleSet assertion.RuleSet, searchExpression string, w io.Writer) {
for _, filename := range fl.Filenames {
include, _ := assertion.ShouldIncludeFile(ruleSet.Files, filename) // FIXME what about error?
if include {
fmt.Printf("Searching %s:\n", filename)
fmt.Fprintf(w, "Searching %s:\n", filename)
loaded, err := fl.Loader.Load(filename)
if err != nil {
fmt.Println("Error for file:", filename)
fmt.Println(err.Error())
fmt.Fprintln(w, "Error for file:", filename)
fmt.Fprintln(w, err.Error())
}
for _, resource := range loaded.Resources {
v, err := assertion.SearchData(searchExpression, resource.Properties)
if err != nil {
fmt.Println(err)
fmt.Fprintln(w, err)
} else {
s, err := assertion.JSONStringify(v)
if err != nil {
fmt.Println(err)
fmt.Fprintln(w, err)
} else {
fmt.Printf("%s (%s): %s\n", resource.ID, resource.Type, s)
fmt.Fprintf(w, "%s (%s): %s\n", resource.ID, resource.Type, s)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package linter
import (
"fmt"
"github.com/stelligent/config-lint/assertion"
"io"
)

type (
// Linter provides the interface for all supported linters
Linter interface {
Validate(ruleSet assertion.RuleSet, options Options) (assertion.ValidationReport, error)
Search(ruleSet assertion.RuleSet, searchExpression string)
Search(ruleSet assertion.RuleSet, searchExpression string, w io.Writer)
}

// Options configures what resources will be linted
Expand Down

0 comments on commit dd33dcb

Please sign in to comment.