From 03bda4bcb2e741bc3e540ac171ba229bcfb47b9c Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Mon, 5 Oct 2015 18:29:24 -0400 Subject: [PATCH] main: add filtering of rules to run Signed-off-by: Vincent Batts --- main.go | 7 ++++++- validate/rules.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 5442dfb..5bfa236 100644 --- a/main.go +++ b/main.go @@ -28,6 +28,11 @@ func main() { return } + rules := validate.RegisteredRules + if *flRun != "" { + rules = validate.FilterRules(rules, validate.SanitizeFilters(*flRun)) + } + var commitrange string if *flCommitRange != "" { commitrange = *flCommitRange @@ -47,7 +52,7 @@ func main() { results := validate.Results{} for _, commit := range c { fmt.Printf(" * %s %s ... ", commit["abbreviated_commit"], commit["subject"]) - vr := validate.Commit(commit, validate.RegisteredRules) + vr := validate.Commit(commit, rules) results = append(results, vr...) if _, fail := vr.PassFail(); fail == 0 { fmt.Println("PASS") diff --git a/validate/rules.go b/validate/rules.go index 21ad613..b88e0e9 100644 --- a/validate/rules.go +++ b/validate/rules.go @@ -1,6 +1,10 @@ package validate -import "github.com/vbatts/git-validation/git" +import ( + "strings" + + "github.com/vbatts/git-validation/git" +) var ( // RegisteredRules are the standard validation to perform on git commits @@ -49,3 +53,29 @@ func (vr Results) PassFail() (pass int, fail int) { } return pass, fail } + +// SanitizeFilters takes a comma delimited list and returns the cleaned items in the list +func SanitizeFilters(filt string) (excludes []string) { + + for _, item := range strings.Split(filt, ",") { + excludes = append(excludes, strings.TrimSpace(item)) + } + + return +} + +// FilterRules takes a set of rules and a list of short names to exclude, and returns the reduced set. +// The comparison is case insensitive. +func FilterRules(rules []Rule, excludes []string) []Rule { + ret := []Rule{} + + for _, r := range rules { + for _, e := range excludes { + if strings.ToLower(r.Name) == strings.ToLower(e) { + ret = append(ret, r) + } + } + } + + return ret +}