diff --git a/README.md b/README.md index 659f319..7d1bd91 100644 --- a/README.md +++ b/README.md @@ -42,15 +42,14 @@ Some SSH arguments have default values - for example, the default value for given Host/keyword pair exists in the config, we'll return a default for the keyword if one exists. -### Clearing cached SSH config files +### Reloading SSH config files Once the first call to `Get()`, `GetStrict()`, `GetAll()`, or `GetAllStrict()` has been made, the contents of the config files will be cached for all future -calls to any of those functions. This cache can manually be cleared with the -`ClearCachedConfigs()` function. Once this is done, it will be cached again by -the next call to `Get()`, `GetStrict()`, `GetAll()`, or `GetAllStrict()`. +calls to any of those functions. The `ReloadConfigs()` function will reset +this cache and replace it with the current config file contents. ```go -ssh_config.ClearCachedConfigs() +ssh_config.ReloadConfigs() ``` ### Manipulating SSH config files diff --git a/config.go b/config.go index 3d56bea..a26e4d3 100644 --- a/config.go +++ b/config.go @@ -167,13 +167,12 @@ func GetAllStrict(alias, key string) ([]string, error) { return DefaultUserSettings.GetAllStrict(alias, key) } -// ClearCachedConfigs resets the loaded config so it can be reloaded on the -// next retrieval. +// ReloadConfigs clears the cached config data and freshly loads the config +// files again. // -// ClearCachedConfigs is a wrapper around -// DefaultUserSettings.ClearCachedConfigs. -func ClearCachedConfigs() { - DefaultUserSettings.ClearCachedConfigs() +// ReloadConfigs is a wrapper around DefaultUserSettings.ReloadConfigs. +func ReloadConfigs() { + DefaultUserSettings.ReloadConfigs() } // Get finds the first value for key within a declaration that matches the @@ -281,6 +280,9 @@ func (u *UserSettings) ConfigFinder(f func() string) { } func (u *UserSettings) doLoadConfigs() { + if u.loadConfigs == nil { + u.loadConfigs = new(sync.Once) + } u.loadConfigs.Do(func() { var filename string var err error @@ -319,10 +321,11 @@ func (u *UserSettings) doLoadConfigs() { }) } -// ClearCachedConfigs resets the loaded config so it can be reloaded on the -// next retrieval. -func (u *UserSettings) ClearCachedConfigs() { +// ReloadConfigs clears the cached config data and freshly loads the config +// files again. +func (u *UserSettings) ReloadConfigs() { u.loadConfigs = new(sync.Once) + u.doLoadConfigs() } func parseFile(filename string) (*Config, error) {