@@ -53,6 +53,8 @@ type configFinder func() string
53
53
// files are parsed and cached the first time Get() or GetStrict() is called.
54
54
type UserSettings struct {
55
55
IgnoreErrors bool
56
+ customConfig * Config
57
+ customConfigFinder configFinder
56
58
systemConfig * Config
57
59
systemConfigFinder configFinder
58
60
userConfig * Config
@@ -203,6 +205,13 @@ func (u *UserSettings) GetStrict(alias, key string) (string, error) {
203
205
if u .onceErr != nil && u .IgnoreErrors == false {
204
206
return "" , u .onceErr
205
207
}
208
+ // TODO this is getting repetitive
209
+ if u .customConfig != nil {
210
+ val , err := findVal (u .customConfig , alias , key )
211
+ if err != nil || val != "" {
212
+ return val , err
213
+ }
214
+ }
206
215
val , err := findVal (u .userConfig , alias , key )
207
216
if err != nil || val != "" {
208
217
return val , err
@@ -228,6 +237,12 @@ func (u *UserSettings) GetAllStrict(alias, key string) ([]string, error) {
228
237
if u .onceErr != nil && u .IgnoreErrors == false {
229
238
return nil , u .onceErr
230
239
}
240
+ if u .customConfig != nil {
241
+ val , err := findAll (u .customConfig , alias , key )
242
+ if err != nil || val != nil {
243
+ return val , err
244
+ }
245
+ }
231
246
val , err := findAll (u .userConfig , alias , key )
232
247
if err != nil || val != nil {
233
248
return val , err
@@ -243,16 +258,38 @@ func (u *UserSettings) GetAllStrict(alias, key string) ([]string, error) {
243
258
return []string {}, nil
244
259
}
245
260
261
+ // ConfigFinder will invoke f to try to find a ssh config file in a custom
262
+ // location on disk, instead of in /etc/ssh or $HOME/.ssh. f should return the
263
+ // name of a file containing SSH configuration.
264
+ //
265
+ // ConfigFinder must be invoked before any calls to Get or GetStrict and panics
266
+ // if f is nil. Most users should not need to use this function.
267
+ func (u * UserSettings ) ConfigFinder (f func () string ) {
268
+ if f == nil {
269
+ panic ("cannot call ConfigFinder with nil function" )
270
+ }
271
+ u .customConfigFinder = f
272
+ }
273
+
246
274
func (u * UserSettings ) doLoadConfigs () {
247
275
u .loadConfigs .Do (func () {
248
- // can't parse user file, that's ok.
249
276
var filename string
277
+ var err error
278
+ if u .customConfigFinder != nil {
279
+ filename = u .customConfigFinder ()
280
+ u .customConfig , err = parseFile (filename )
281
+ // IsNotExist should be returned because a user specified this
282
+ // function - not existing likely means they made an error
283
+ if err != nil {
284
+ u .onceErr = err
285
+ }
286
+ return
287
+ }
250
288
if u .userConfigFinder == nil {
251
289
filename = userConfigFinder ()
252
290
} else {
253
291
filename = u .userConfigFinder ()
254
292
}
255
- var err error
256
293
u .userConfig , err = parseFile (filename )
257
294
//lint:ignore S1002 I prefer it this way
258
295
if err != nil && os .IsNotExist (err ) == false {
0 commit comments