2024-01-15 13:46:49 +03:00
|
|
|
package config
|
2023-12-23 15:50:47 +03:00
|
|
|
|
2024-05-02 12:56:32 +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 {
|
2024-01-02 15:12:47 +03:00
|
|
|
Global struct {
|
|
|
|
// Excludes is an optional list of glob patterns used to exclude certain files from all formatters.
|
|
|
|
Excludes []string
|
|
|
|
}
|
2024-01-15 13:46:49 +03:00
|
|
|
Formatters map[string]*Formatter `toml:"formatter"`
|
2023-12-23 15:50:47 +03:00
|
|
|
}
|
|
|
|
|
2024-01-15 13:46:49 +03:00
|
|
|
// ReadFile reads from path and unmarshals toml into a Config instance.
|
2024-05-02 12:56:32 +03:00
|
|
|
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
|
|
|
|
}
|