feat: add batch size global config value

Signed-off-by: Brian McGee <brian@bmcgee.ie>
This commit is contained in:
Brian McGee 2024-07-03 19:07:36 +01:00
parent 976d156689
commit 445a873bd1
No known key found for this signature in database
GPG Key ID: D49016E76AD1E8C0
4 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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"`

View File

@ -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

View File

@ -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]