-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor some config init logic into config package
Signed-off-by: Brian McGee <[email protected]>
- Loading branch information
1 parent
fb94938
commit 2eaf999
Showing
4 changed files
with
53 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,51 @@ | ||
package config | ||
|
||
import "github.com/BurntSushi/toml" | ||
import ( | ||
"fmt" | ||
"sort" | ||
|
||
"github.com/BurntSushi/toml" | ||
) | ||
|
||
// Config is used to represent the list of configured Formatters. | ||
type Config struct { | ||
Global struct { | ||
// Excludes is an optional list of glob patterns used to exclude certain files from all formatters. | ||
Excludes []string | ||
} | ||
Names []string `toml:"-"` | ||
Formatters map[string]*Formatter `toml:"formatter"` | ||
} | ||
|
||
// ReadFile reads from path and unmarshals toml into a Config instance. | ||
func ReadFile(path string) (cfg *Config, err error) { | ||
_, err = toml.DecodeFile(path, &cfg) | ||
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 | ||
} | ||
|
||
// sort the formatter names so that, as we construct pipelines, we add formatters in a determinstic fashion. This | ||
// ensures a deterministic order even when all priority values are the same e.g. 0 | ||
for name := range cfg.Formatters { | ||
cfg.Names = append(cfg.Names, name) | ||
} | ||
sort.Strings(cfg.Names) | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters