parent
d8d666a132
commit
256c6774c6
|
@ -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
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,9 @@ func (f *Formatter) Executable() string {
|
|||
}
|
||||
|
||||
// Init is used to initialise internal state before this Formatter is ready to accept paths.
|
||||
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
|
||||
|
||||
|
@ -68,26 +70,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
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Reference in New Issue
Block a user