feat: support global excludes

Fixes #12
This commit is contained in:
Jonas Chevalier 2024-01-01 16:02:12 +01:00
parent c9665041cc
commit 9b9d8dc8b3
4 changed files with 35 additions and 20 deletions

View File

@ -40,6 +40,8 @@ func (f *Format) Run() error {
return fmt.Errorf("%w: failed to read config file", err)
}
globalExcludes, err := format.CompileGlobs(cfg.Global.Excludes)
// create optional formatter filter set
formatterSet := make(map[string]bool)
for _, name := range Cli.Formatters {
@ -68,7 +70,7 @@ func (f *Format) Run() error {
continue
}
err = formatter.Init(name)
err = formatter.Init(name, globalExcludes)
if err == format.ErrFormatterNotFound && Cli.AllowMissingFormatter {
l.Debugf("formatter not found: %v", name)
// remove this formatter

View File

@ -4,6 +4,10 @@ import "github.com/BurntSushi/toml"
// Config is used to represent the list of configured Formatters.
type Config struct {
Global struct {
// Excludes is an optional list of glob patterns used to exclude certain files from all formatters.
Excludes []string
}
Formatters map[string]*Formatter `toml:"formatter"`
}

View File

@ -41,12 +41,14 @@ type Formatter struct {
batchSize int
}
func (f *Formatter) Init(name string) error {
func (f *Formatter) Init(name string, globalExcludes []glob.Glob) error {
var err error
// capture the name from the config file
f.name = name
// test if the formatter is available
if err := exec.Command(f.Command, "--help").Run(); err != nil {
if err = exec.Command(f.Command, "--help").Run(); err != nil {
return ErrFormatterNotFound
}
@ -57,26 +59,16 @@ func (f *Formatter) Init(name string) error {
f.batch = make([]string, f.batchSize)
f.batch = f.batch[:0]
// todo refactor common code below
if len(f.Includes) > 0 {
for _, pattern := range f.Includes {
g, err := glob.Compile("**/" + pattern)
if err != nil {
return fmt.Errorf("%w: failed to compile include pattern '%v' for formatter '%v'", err, pattern, f.name)
}
f.includes = append(f.includes, g)
}
f.includes, err = CompileGlobs(f.Includes)
if err != nil {
return fmt.Errorf("%w: formatter '%v' includes", err, f.name)
}
if len(f.Excludes) > 0 {
for _, pattern := range f.Excludes {
g, err := glob.Compile("**/" + pattern)
if err != nil {
return fmt.Errorf("%w: failed to compile exclude pattern '%v' for formatter '%v'", err, pattern, f.name)
}
f.excludes = append(f.excludes, g)
}
f.excludes, err = CompileGlobs(f.Excludes)
if err != nil {
return fmt.Errorf("%w: formatter '%v' excludes", err, f.name)
}
f.excludes = append(f.excludes, globalExcludes...)
return nil
}

View File

@ -1,9 +1,26 @@
package format
import (
"fmt"
"github.com/gobwas/glob"
)
// CompileGlobs prepares the globs, where the patterns are all right-matching.
func CompileGlobs(patterns []string) ([]glob.Glob, error) {
globs := make([]glob.Glob, len(patterns))
for i, pattern := range patterns {
g, err := glob.Compile("**/" + pattern)
if err != nil {
return nil, fmt.Errorf("%w: failed to compile include pattern '%v'", err, pattern)
}
globs[i] = g
}
return globs, nil
}
func PathMatches(path string, globs []glob.Glob) bool {
for idx := range globs {
if globs[idx].Match(path) {