Skip to content

Commit 47ba9db

Browse files
authored
feat: allow overriding config path using LEFTHOOK_CONFIG env (#1072)
1 parent ae8c42a commit 47ba9db

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

internal/config/load.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,36 @@ 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 {
64+
return err
65+
}
66+
67+
return nil
68+
}
69+
6070
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+
6182
for _, extension := range extensions {
6283
for _, name := range names {
6384
config := filepath.Join(root, name+extension)
6485
if ok, _ := afero.Exists(filesystem, config); !ok {
6586
continue
6687
}
6788

68-
log.Debug("loading config: ", config)
69-
if err := k.Load(kfs.Provider(newIOFS(filesystem), config), parsers[extension], mergeJobsOption); err != nil {
70-
return err
71-
}
72-
73-
return nil
89+
return loadConfig(k, filesystem, config)
7490
}
7591
}
7692

internal/config/load_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestLoad(t *testing.T) {
2121
files map[string]string
2222
remote string
2323
remoteConfigPath string
24+
pathOverride string
2425
result *Config
2526
}{
2627
"with .lefthook.yml": {
@@ -841,7 +842,7 @@ pre-commit:
841842
},
842843
},
843844
},
844-
"with .cofig/lefthook.yml": {
845+
"with .config/lefthook.yml": {
845846
files: map[string]string{
846847
filepath.Join(".config", "lefthook.yml"): `
847848
pre-commit:
@@ -933,6 +934,32 @@ pre-commit:
933934
},
934935
},
935936
},
937+
"custom config path": {
938+
files: map[string]string{
939+
"lefthook_custom.yml": `
940+
pre-commit:
941+
commands:
942+
tests:
943+
run: yarn test
944+
`,
945+
},
946+
pathOverride: "lefthook_custom.yml",
947+
result: &Config{
948+
SourceDir: DefaultSourceDir,
949+
SourceDirLocal: DefaultSourceDirLocal,
950+
Colors: nil,
951+
Hooks: map[string]*Hook{
952+
"pre-commit": {
953+
Parallel: false,
954+
Commands: map[string]*Command{
955+
"tests": {
956+
Run: "yarn test",
957+
},
958+
},
959+
},
960+
},
961+
},
962+
},
936963
} {
937964
fs := afero.Afero{Fs: afero.NewMemMapFs()}
938965
repo := &git.Repository{
@@ -960,6 +987,8 @@ pre-commit:
960987
assert.NoError(fs.WriteFile(tt.remoteConfigPath, []byte(tt.remote), 0o644))
961988
}
962989

990+
t.Setenv("LEFTHOOK_CONFIG", tt.pathOverride)
991+
963992
result, err := Load(fs.Fs, repo)
964993
assert.NoError(err)
965994
assert.Equal(tt.result, result)

0 commit comments

Comments
 (0)