From b9f36ee948d3b1ff71c0099f579db308fe3eba4b Mon Sep 17 00:00:00 2001 From: Brian McGee Date: Wed, 3 Jul 2024 19:07:36 +0100 Subject: [PATCH] feat: add batch size global config value Signed-off-by: Brian McGee --- cli/format.go | 12 ++++++++++++ config/config.go | 2 ++ config/config_test.go | 1 + test/examples/treefmt.toml | 1 + 4 files changed, 16 insertions(+) diff --git a/cli/format.go b/cli/format.go index da9552ea..8ceac98c 100644 --- a/cli/format.go +++ b/cli/format.go @@ -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) diff --git a/config/config.go b/config/config.go index 553aee62..32114388 100644 --- a/config/config.go +++ b/config/config.go @@ -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"` diff --git a/config/config_test.go b/config/config_test.go index abb9344c..3a44bcf8 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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 diff --git a/test/examples/treefmt.toml b/test/examples/treefmt.toml index 2291ca82..ab2c735d 100644 --- a/test/examples/treefmt.toml +++ b/test/examples/treefmt.toml @@ -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]