Skip to content

Commit

Permalink
feat: add batch size global config value
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Jul 3, 2024
1 parent 188012b commit b9f36ee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cli/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/examples/treefmt.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit b9f36ee

Please sign in to comment.