-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(GH-33): Support rule loading from config file
- Loading branch information
Showing
18 changed files
with
171 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
rules: | ||
- pathPrefix: vendor/ | ||
exclude: true | ||
- pathPrefix: fixtures/ | ||
exclude: true | ||
- nameMatch: '^.*\.go$' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mode: dockerfile | ||
rules: | ||
- mode: dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mode: bogus | ||
rules: | ||
- mode: bogus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mode: jenkins | ||
rules: | ||
- mode: jenkins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mode: terraform | ||
rules: | ||
- mode: terraform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
mode: terraform_or_terragrunt | ||
rules: | ||
- mode: terraform | ||
- mode: terragrunt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mode: terragrunt | ||
rules: | ||
- mode: terragrunt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ivanilves/travelgrunt/pkg/config/include" | ||
) | ||
|
||
func getIncludeFn(mode string) (fn func(os.DirEntry) bool, err error) { | ||
err = nil | ||
|
||
switch mode { | ||
case "": | ||
fn = nil | ||
case "terragrunt": | ||
fn = include.IsTerragrunt | ||
case "terraform": | ||
fn = include.IsTerraform | ||
case "dockerfile": | ||
fn = include.IsDockerfile | ||
case "jenkins": | ||
fn = include.IsJenkins | ||
default: | ||
fn = nil | ||
err = fmt.Errorf("illegal mode: %s", mode) | ||
} | ||
|
||
return fn, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/ivanilves/travelgrunt/pkg/config/include" | ||
) | ||
|
||
// Rule represents a single configuration rule entity | ||
type Rule struct { | ||
// Mode is a behaviour backed by a function from the `include` package | ||
Mode string `yaml:"mode"` | ||
// Prefix is a literal path prefix to be matched against the passed file path | ||
Prefix string `yaml:"prefix"` | ||
// PathEx is a regex to be matched against the passed file full path | ||
PathEx string `yaml:"path"` | ||
// NameEx is a regex to be matched against the passed file base name | ||
NameEx string `yaml:"name"` | ||
// Exclude reverses the rule match effect | ||
Exclude bool `yaml:"exclude"` | ||
|
||
// IncludeFn is an `include` package function to invoke on passed file / dir entry | ||
IncludeFn func(os.DirEntry) bool | ||
} | ||
|
||
// Matched matches passed file / dir entry name against supplied expressions (if any) | ||
func (r Rule) Matched(d os.DirEntry, rel string) bool { | ||
return r.pathPrefixed(rel) && r.pathMatched(rel) && r.nameMatched(d) | ||
} | ||
|
||
func (r Rule) pathPrefixed(path string) bool { | ||
if r.Prefix != "" { | ||
return strings.HasPrefix(path, r.Prefix) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (r Rule) pathMatched(path string) bool { | ||
if r.PathEx != "" { | ||
rx := regexp.MustCompile(r.PathEx) | ||
|
||
return rx.MatchString(path) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (r Rule) nameMatched(d os.DirEntry) bool { | ||
if r.NameEx != "" { | ||
if !include.FileOrSymlink(d) { | ||
return false | ||
} | ||
|
||
rx := regexp.MustCompile(r.NameEx) | ||
|
||
return rx.MatchString(d.Name()) | ||
} | ||
|
||
return true | ||
} | ||
|
||
// Include is a "decider" function that includes/excludes the path given [on the single rule level] | ||
func (r Rule) Include(d os.DirEntry, rel string) bool { | ||
if r.IncludeFn == nil { | ||
return r.Matched(d, rel) | ||
} | ||
|
||
return r.IncludeFn(d) && r.Matched(d, rel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters