@@ -57,28 +57,19 @@ func (err ConfigNotFoundError) Error() string {
57
57
return err .message
58
58
}
59
59
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 {
64
65
return err
65
66
}
66
67
67
68
return nil
68
69
}
69
70
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 {
82
73
for _ , extension := range extensions {
83
74
for _ , name := range names {
84
75
config := filepath .Join (root , name + extension )
@@ -93,26 +84,51 @@ func loadOne(k *koanf.Koanf, filesystem afero.Fs, root string, names []string) e
93
84
return ConfigNotFoundError {fmt .Sprintf ("No config files with names %q have been found in \" %s\" " , names , root )}
94
85
}
95
86
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 )
101
90
if ok := errors .As (err , & ConfigNotFoundError {}); ok {
102
91
var hasLocalConfig bool
103
92
OUT:
104
93
for _ , extension := range extensions {
105
94
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 {
107
96
hasLocalConfig = true
108
97
break OUT
109
98
}
110
99
}
111
100
}
112
101
if ! hasLocalConfig {
113
- return nil , nil , err
102
+ return err
114
103
}
115
104
} 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 {
116
132
return nil , nil , err
117
133
}
118
134
@@ -140,7 +156,7 @@ func LoadKoanf(filesystem afero.Fs, repo *git.Repository) (*koanf.Koanf, *koanf.
140
156
141
157
// Load optional local config (e.g. lefthook-local.yml)
142
158
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 {
144
160
if ok := errors .As (err , & ConfigNotFoundError {}); ! ok {
145
161
return nil , nil , err
146
162
}
0 commit comments