Skip to content

Commit

Permalink
add -terraform command line option to use a built-in rule set for Ter…
Browse files Browse the repository at this point in the history
…raform, generate Asset for the rule set using go-bindata
  • Loading branch information
Larry Hitchon committed Apr 14, 2018
1 parent 5a0ccf8 commit a7d2a25
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage.out
.release/
vendor/
dist/
cli/assets.go
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ $(BUILD_DIR)/config-lint: $(CLI_FILES)
mkdir -p $(BUILD_DIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) go build -ldflags=$(GOLDFLAGS) -o $(BUILD_DIR)/config-lint cli/*.go

build: $(BUILD_DIR)/config-lint
build: gen $(BUILD_DIR)/config-lint

all: clean deps test build

Expand Down
238 changes: 0 additions & 238 deletions builtin-rules/terraform-set-2.yml

This file was deleted.

29 changes: 28 additions & 1 deletion cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ type (
}
)

//go:generate go-bindata -pkg $GOPACKAGE -o assets.go assets/
func main() {
var rulesFilenames arrayFlags
terraformBuiltInRules := flag.Bool("terraform", false, "Use built-in rules for Terraform")
verboseLogging := flag.Bool("verbose", false, "Verbose logging")
flag.Var(&rulesFilenames, "rules", "Rules file, can be specified multiple times")
tags := flag.String("tags", "", "Run only tests with tags in this comma separated list")
Expand Down Expand Up @@ -58,9 +60,17 @@ func main() {
}
ruleSets, err := loadRuleSets(rulesFilenames)
if err != nil {
fmt.Printf("Failed to load rules: %v", err)
fmt.Printf("Failed to load rules: %v\n", err)
return
}
if *terraformBuiltInRules {
builtInRuleSet, err := loadBuiltInRuleSet("assets/terraform.yml")
if err != nil {
fmt.Printf("Failed to load built-in rules for Terraform: %v\n", err)
return
}
ruleSets = append(ruleSets, builtInRuleSet)
}
applyRules(ruleSets, flag.Args(), applyOptions)
}

Expand Down Expand Up @@ -98,6 +108,23 @@ func loadRuleSets(rulesFilenames arrayFlags) ([]assertion.RuleSet, error) {
return ruleSets, nil
}

func loadBuiltInRuleSet(name string) (assertion.RuleSet, error) {
emptyRuleSet := assertion.RuleSet{}
rulesContent, err := Asset(name)
if err != nil {
fmt.Println("Unable to find built-in rules:", name)
fmt.Println(err.Error())
return emptyRuleSet, err
}
ruleSet, err := assertion.ParseRules(string(rulesContent))
if err != nil {
fmt.Println("Unable to parse built-in rules:" + name)
fmt.Println(err.Error())
return emptyRuleSet, err
}
return ruleSet, nil
}

func applyRules(ruleSets []assertion.RuleSet, args arrayFlags, options ApplyOptions) {

report := assertion.ValidationReport{
Expand Down
Loading

0 comments on commit a7d2a25

Please sign in to comment.