mirror of
https://github.com/numtide/treefmt.git
synced 2024-10-03 20:18:11 +03:00
feat: add batch size global config value
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
parent
976d156689
commit
445a873bd1
@ -23,6 +23,10 @@ import (
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultBatchSize = 1024
|
||||
)
|
||||
|
||||
var (
|
||||
ErrFailOnChange = errors.New("unexpected changes detected, --fail-on-change is enabled")
|
||||
ErrInvalidBatchSize = errors.New("batch size must be >= 1")
|
||||
@ -32,6 +36,8 @@ func (f *Format) Run() (err error) {
|
||||
// set log level and other options
|
||||
f.configureLogging()
|
||||
|
||||
// validate the cli batch size
|
||||
// todo move this into kong validation
|
||||
if f.BatchSize < 1 {
|
||||
return ErrInvalidBatchSize
|
||||
}
|
||||
@ -99,6 +105,12 @@ func (f *Format) Run() (err error) {
|
||||
return fmt.Errorf("failed to read config file %v: %w", f.ConfigFile, err)
|
||||
}
|
||||
|
||||
// update the batch size only if it has not already been set by the cli arg
|
||||
// cli arg takes precedence over config
|
||||
if f.BatchSize == DefaultBatchSize && cfg.Global.BatchSize != 0 {
|
||||
f.BatchSize = cfg.Global.BatchSize
|
||||
}
|
||||
|
||||
// compile global exclude globs
|
||||
if f.globalExcludes, err = format.CompileGlobs(cfg.Global.Excludes); err != nil {
|
||||
return fmt.Errorf("failed to compile global excludes: %w", err)
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
// Config is used to represent the list of configured Formatters.
|
||||
type Config struct {
|
||||
Global struct {
|
||||
// BatchSize controls the maximum number of paths to batch before applying them to a sequence of formatters.
|
||||
BatchSize int `toml:"batch_size"`
|
||||
// Excludes is an optional list of glob patterns used to exclude certain files from all formatters.
|
||||
Excludes []string `toml:"excludes"`
|
||||
} `toml:"global"`
|
||||
|
@ -14,6 +14,7 @@ func TestReadConfigFile(t *testing.T) {
|
||||
|
||||
as.NotNil(cfg)
|
||||
|
||||
as.Equal(10, cfg.Global.BatchSize)
|
||||
as.Equal([]string{"*.toml"}, cfg.Global.Excludes)
|
||||
|
||||
// python
|
||||
|
@ -1,6 +1,7 @@
|
||||
# One CLI to format the code tree - https://git.numtide.com/numtide/treefmt
|
||||
|
||||
[global]
|
||||
batch_size = 10
|
||||
excludes = ["*.toml"]
|
||||
|
||||
[formatter.python]
|
||||
|
Loading…
Reference in New Issue
Block a user