parent
c9665041cc
commit
9b9d8dc8b3
|
@ -40,6 +40,8 @@ func (f *Format) Run() error {
|
||||||
return fmt.Errorf("%w: failed to read config file", err)
|
return fmt.Errorf("%w: failed to read config file", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
globalExcludes, err := format.CompileGlobs(cfg.Global.Excludes)
|
||||||
|
|
||||||
// create optional formatter filter set
|
// create optional formatter filter set
|
||||||
formatterSet := make(map[string]bool)
|
formatterSet := make(map[string]bool)
|
||||||
for _, name := range Cli.Formatters {
|
for _, name := range Cli.Formatters {
|
||||||
|
@ -68,7 +70,7 @@ func (f *Format) Run() error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
err = formatter.Init(name)
|
err = formatter.Init(name, globalExcludes)
|
||||||
if err == format.ErrFormatterNotFound && Cli.AllowMissingFormatter {
|
if err == format.ErrFormatterNotFound && Cli.AllowMissingFormatter {
|
||||||
l.Debugf("formatter not found: %v", name)
|
l.Debugf("formatter not found: %v", name)
|
||||||
// remove this formatter
|
// remove this formatter
|
||||||
|
|
|
@ -4,6 +4,10 @@ import "github.com/BurntSushi/toml"
|
||||||
|
|
||||||
// Config is used to represent the list of configured Formatters.
|
// Config is used to represent the list of configured Formatters.
|
||||||
type Config struct {
|
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"`
|
Formatters map[string]*Formatter `toml:"formatter"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,12 +41,14 @@ type Formatter struct {
|
||||||
batchSize int
|
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
|
// capture the name from the config file
|
||||||
f.name = name
|
f.name = name
|
||||||
|
|
||||||
// test if the formatter is available
|
// 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
|
return ErrFormatterNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,26 +59,16 @@ func (f *Formatter) Init(name string) error {
|
||||||
f.batch = make([]string, f.batchSize)
|
f.batch = make([]string, f.batchSize)
|
||||||
f.batch = f.batch[:0]
|
f.batch = f.batch[:0]
|
||||||
|
|
||||||
// todo refactor common code below
|
f.includes, err = CompileGlobs(f.Includes)
|
||||||
if len(f.Includes) > 0 {
|
if err != nil {
|
||||||
for _, pattern := range f.Includes {
|
return fmt.Errorf("%w: formatter '%v' includes", err, f.name)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(f.Excludes) > 0 {
|
f.excludes, err = CompileGlobs(f.Excludes)
|
||||||
for _, pattern := range f.Excludes {
|
if err != nil {
|
||||||
g, err := glob.Compile("**/" + pattern)
|
return fmt.Errorf("%w: formatter '%v' excludes", err, f.name)
|
||||||
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 = append(f.excludes, globalExcludes...)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,26 @@
|
||||||
package format
|
package format
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
"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 {
|
func PathMatches(path string, globs []glob.Glob) bool {
|
||||||
for idx := range globs {
|
for idx := range globs {
|
||||||
if globs[idx].Match(path) {
|
if globs[idx].Match(path) {
|
||||||
|
|
Reference in New Issue
Block a user