Skip to content

Commit 2eaf999

Browse files
committed
feat: refactor some config init logic into config package
Signed-off-by: Brian McGee <[email protected]>
1 parent fb94938 commit 2eaf999

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

cli/format.go

+9-33
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"os/signal"
1010
"path/filepath"
1111
"runtime"
12-
"slices"
13-
"sort"
1412
"strings"
1513
"syscall"
1614

@@ -41,57 +39,32 @@ var (
4139
)
4240

4341
func (f *Format) Run() (err error) {
44-
stats.Init()
45-
42+
// create a prefixed logger
4643
l := log.WithPrefix("format")
4744

45+
// ensure cache is closed on return
4846
defer func() {
4947
if err := cache.Close(); err != nil {
5048
l.Errorf("failed to close cache: %v", err)
5149
}
5250
}()
5351

5452
// read config
55-
cfg, err := config.ReadFile(Cli.ConfigFile)
53+
cfg, err := config.ReadFile(Cli.ConfigFile, Cli.Formatters)
5654
if err != nil {
5755
return fmt.Errorf("%w: failed to read config file", err)
5856
}
5957

58+
// compile global exclude globs
6059
if globalExcludes, err = format.CompileGlobs(cfg.Global.Excludes); err != nil {
6160
return fmt.Errorf("%w: failed to compile global globs", err)
6261
}
6362

63+
// initialise pipelines
6464
pipelines = make(map[string]*format.Pipeline)
6565
formatters = make(map[string]*format.Formatter)
6666

67-
// filter formatters
68-
if len(Cli.Formatters) > 0 {
69-
// first check the cli formatter list is valid
70-
for _, name := range Cli.Formatters {
71-
_, ok := cfg.Formatters[name]
72-
if !ok {
73-
return fmt.Errorf("formatter not found in config: %v", name)
74-
}
75-
}
76-
// next we remove any formatter configs that were not specified
77-
for name := range cfg.Formatters {
78-
if !slices.Contains(Cli.Formatters, name) {
79-
delete(cfg.Formatters, name)
80-
}
81-
}
82-
}
83-
84-
// sort the formatter names so that, as we construct pipelines, we add formatters in a determinstic fashion. This
85-
// ensures a deterministic order even when all priority values are the same e.g. 0
86-
87-
names := make([]string, 0, len(cfg.Formatters))
88-
for name := range cfg.Formatters {
89-
names = append(names, name)
90-
}
91-
sort.Strings(names)
92-
93-
// init formatters
94-
for _, name := range names {
67+
for _, name := range cfg.Names {
9568
formatterCfg := cfg.Formatters[name]
9669
formatter, err := format.NewFormatter(name, Cli.TreeRoot, formatterCfg, globalExcludes)
9770
if errors.Is(err, format.ErrCommandNotFound) && Cli.AllowMissingFormatter {
@@ -134,6 +107,9 @@ func (f *Format) Run() (err error) {
134107
cancel()
135108
}()
136109

110+
// initialise stats collection
111+
stats.Init()
112+
137113
// create some groups for concurrent processing and control flow
138114
eg, ctx := errgroup.WithContext(ctx)
139115

config/config.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
package config
22

3-
import "github.com/BurntSushi/toml"
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/BurntSushi/toml"
8+
)
49

510
// Config is used to represent the list of configured Formatters.
611
type Config struct {
712
Global struct {
813
// Excludes is an optional list of glob patterns used to exclude certain files from all formatters.
914
Excludes []string
1015
}
16+
Names []string `toml:"-"`
1117
Formatters map[string]*Formatter `toml:"formatter"`
1218
}
1319

1420
// ReadFile reads from path and unmarshals toml into a Config instance.
15-
func ReadFile(path string) (cfg *Config, err error) {
16-
_, err = toml.DecodeFile(path, &cfg)
21+
func ReadFile(path string, names []string) (cfg *Config, err error) {
22+
if _, err = toml.DecodeFile(path, &cfg); err != nil {
23+
return nil, fmt.Errorf("failed to decode config file: %w", err)
24+
}
25+
26+
// filter formatters based on provided names
27+
if len(names) > 0 {
28+
filtered := make(map[string]*Formatter)
29+
30+
// check if the provided names exist in the config
31+
for _, name := range names {
32+
formatterCfg, ok := cfg.Formatters[name]
33+
if !ok {
34+
return nil, fmt.Errorf("formatter %v not found in config", name)
35+
}
36+
filtered[name] = formatterCfg
37+
}
38+
39+
// updated formatters
40+
cfg.Formatters = filtered
41+
}
42+
43+
// sort the formatter names so that, as we construct pipelines, we add formatters in a determinstic fashion. This
44+
// ensures a deterministic order even when all priority values are the same e.g. 0
45+
for name := range cfg.Formatters {
46+
cfg.Names = append(cfg.Names, name)
47+
}
48+
sort.Strings(cfg.Names)
49+
1750
return
1851
}

config/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestReadConfigFile(t *testing.T) {
1010
as := require.New(t)
1111

12-
cfg, err := ReadFile("../test/examples/treefmt.toml")
12+
cfg, err := ReadFile("../test/examples/treefmt.toml", nil)
1313
as.NoError(err, "failed to read config file")
1414

1515
as.NotNil(cfg)

format/formatter.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func (f *Formatter) Wants(file *walk.File) bool {
111111
func NewFormatter(
112112
name string,
113113
treeRoot string,
114-
config *config.Formatter,
114+
cfg *config.Formatter,
115115
globalExcludes []glob.Glob,
116116
) (*Formatter, error) {
117117
var err error
@@ -120,11 +120,11 @@ func NewFormatter(
120120

121121
// capture config and the formatter's name
122122
f.name = name
123-
f.config = config
123+
f.config = cfg
124124
f.workingDir = treeRoot
125125

126126
// test if the formatter is available
127-
executable, err := exec.LookPath(config.Command)
127+
executable, err := exec.LookPath(cfg.Command)
128128
if errors.Is(err, exec.ErrNotFound) {
129129
return nil, ErrCommandNotFound
130130
} else if err != nil {
@@ -133,18 +133,18 @@ func NewFormatter(
133133
f.executable = executable
134134

135135
// initialise internal state
136-
if config.Pipeline == "" {
136+
if cfg.Pipeline == "" {
137137
f.log = log.WithPrefix(fmt.Sprintf("format | %s", name))
138138
} else {
139-
f.log = log.WithPrefix(fmt.Sprintf("format | %s[%s]", config.Pipeline, name))
139+
f.log = log.WithPrefix(fmt.Sprintf("format | %s[%s]", cfg.Pipeline, name))
140140
}
141141

142-
f.includes, err = CompileGlobs(config.Includes)
142+
f.includes, err = CompileGlobs(cfg.Includes)
143143
if err != nil {
144144
return nil, fmt.Errorf("%w: formatter '%v' includes", err, f.name)
145145
}
146146

147-
f.excludes, err = CompileGlobs(config.Excludes)
147+
f.excludes, err = CompileGlobs(cfg.Excludes)
148148
if err != nil {
149149
return nil, fmt.Errorf("%w: formatter '%v' excludes", err, f.name)
150150
}

0 commit comments

Comments
 (0)