Skip to content

Commit bd847e2

Browse files
committed
refactor: loading configs logic
1 parent 47ba9db commit bd847e2

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

internal/config/load.go

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,19 @@ func (err ConfigNotFoundError) Error() string {
5757
return err.message
5858
}
5959

60-
func loadConfig(k *koanf.Koanf, filesystem afero.Fs, config string) error {
61-
extension := filepath.Ext(config)
62-
log.Debug("loading config: ", config)
63-
if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeJobsOption); err != nil {
60+
// loadConfig loads the config at the given path.
61+
func loadConfig(k *koanf.Koanf, filesystem afero.Fs, path string) error {
62+
extension := filepath.Ext(path)
63+
log.Debug("loading config: ", path)
64+
if err := k.Load(kfs.Provider(newIOFS(filesystem), path), parsers[extension], mergeJobsOption); err != nil {
6465
return err
6566
}
6667

6768
return nil
6869
}
6970

70-
func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) error {
71-
configPathOverride := os.Getenv("LEFTHOOK_CONFIG")
72-
if configPathOverride != "" {
73-
if !filepath.IsAbs(configPathOverride) {
74-
configPathOverride = filepath.Join(root, configPathOverride)
75-
}
76-
if ok, _ := afero.Exists(filesystem, configPathOverride); !ok {
77-
return ConfigNotFoundError{fmt.Sprintf("Config file \"%s\" not found!", configPathOverride)}
78-
}
79-
return loadConfig(k, filesystem, configPathOverride)
80-
}
81-
71+
// loadFirst loads the first existing config from given names and supported extensions.
72+
func loadFirst(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) error {
8273
for _, extension := range extensions {
8374
for _, name := range names {
8475
config := filepath.Join(root, name+extension)
@@ -93,26 +84,51 @@ func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) e
9384
return ConfigNotFoundError{fmt.Sprintf("No config files with names %q have been found in \"%s\"", names, root)}
9485
}
9586

96-
func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.Koanf, error) {
97-
main := koanf.New(".")
98-
99-
// Load main (e.g. lefthook.yml) or fallback to local config (e.g. lefthook-local.yml)
100-
err := loadOne(main, filesystem, repo.RootPath, MainConfigNames)
87+
// loadFirstMain loads the main config (e.g. lefthook.yml) or fallbacks to local config (e.g. lefthook-local.yml).
88+
func loadFirstMain(k *koanf.Koanf, filesystem afero.Fs, root string) error {
89+
err := loadFirst(k, filesystem, root, MainConfigNames)
10190
if ok := errors.As(err, &ConfigNotFoundError{}); ok {
10291
var hasLocalConfig bool
10392
OUT:
10493
for _, extension := range extensions {
10594
for _, name := range LocalConfigNames {
106-
if ok, _ := afero.Exists(filesystem, filepath.Join(repo.RootPath, name+extension)); ok {
95+
if ok, _ := afero.Exists(filesystem, filepath.Join(root, name+extension)); ok {
10796
hasLocalConfig = true
10897
break OUT
10998
}
11099
}
111100
}
112101
if !hasLocalConfig {
113-
return nil, nil, err
102+
return err
114103
}
115104
} else if err != nil {
105+
return err
106+
}
107+
108+
return nil
109+
}
110+
111+
func loadMain(k *koanf.Koanf, filesystem afero.Fs, root string) error {
112+
configOverride := os.Getenv("LEFTHOOK_CONFIG")
113+
if len(configOverride) == 0 {
114+
return loadFirstMain(k, filesystem, root)
115+
}
116+
117+
if !filepath.IsAbs(configOverride) {
118+
configOverride = filepath.Join(root, configOverride)
119+
}
120+
if ok, _ := afero.Exists(filesystem, configOverride); !ok {
121+
return ConfigNotFoundError{fmt.Sprintf("Config file \"%s\" not found!", configOverride)}
122+
}
123+
124+
return loadConfig(k, filesystem, configOverride)
125+
}
126+
127+
func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.Koanf, error) {
128+
main := koanf.New(".")
129+
130+
// Load main config
131+
if err := loadMain(main, filesystem, repo.RootPath); err != nil {
116132
return nil, nil, err
117133
}
118134

@@ -140,7 +156,7 @@ func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.
140156

141157
// Load optional local config (e.g. lefthook-local.yml)
142158
var noLocal bool
143-
if err := loadOne(secondary, filesystem, repo.RootPath, LocalConfigNames); err != nil {
159+
if err := loadFirst(secondary, filesystem, repo.RootPath, LocalConfigNames); err != nil {
144160
if ok := errors.As(err, &ConfigNotFoundError{}); !ok {
145161
return nil, nil, err
146162
}

0 commit comments

Comments
 (0)