treefmt/config/config.go

43 lines
1.0 KiB
Go
Raw Normal View History

package config
2023-12-23 15:50:47 +03:00
import (
"fmt"
"github.com/BurntSushi/toml"
)
2023-12-23 15:50:47 +03:00
2023-12-24 14:59:05 +03:00
// Config is used to represent the list of configured Formatters.
2023-12-23 15:50:47 +03:00
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"`
2023-12-23 15:50:47 +03:00
}
// ReadFile reads from path and unmarshals toml into a Config instance.
func ReadFile(path string, names []string) (cfg *Config, err error) {
if _, err = toml.DecodeFile(path, &cfg); err != nil {
return nil, fmt.Errorf("failed to decode config file: %w", err)
}
// filter formatters based on provided names
if len(names) > 0 {
filtered := make(map[string]*Formatter)
// check if the provided names exist in the config
for _, name := range names {
formatterCfg, ok := cfg.Formatters[name]
if !ok {
return nil, fmt.Errorf("formatter %v not found in config", name)
}
filtered[name] = formatterCfg
}
// updated formatters
cfg.Formatters = filtered
}
2023-12-23 15:50:47 +03:00
return
}