From ea2c71d55214c5e412609cdae11f39ceb566fc7b Mon Sep 17 00:00:00 2001 From: Sylvia Crowe Date: Mon, 8 Jan 2024 23:04:00 -0800 Subject: [PATCH] change ClearCachedConfigs to ReloadConfigs Clearing cached data is somewhat vague and could be more confusing to users who are not familiar with this library. But Reloading the config data is very straightforward. For this reason, the ability to clear the cache has been replaced with the ability to reload. The reload simply clears the cache and then loads the contents again. Additionally, this contains a bug fix that ensure the loadConfigs pointer is set to a non-null value before it is used. --- README.md | 9 ++++----- config.go | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) 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) {