From 0ce1de38348b997976acfe0cdb8d8b39e5da67fb Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Fri, 22 Mar 2024 22:22:14 -0400 Subject: [PATCH] chore(golang-rewrite): default settings refactor --- config/config.go | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/config/config.go b/config/config.go index dff32610..9ba955e8 100644 --- a/config/config.go +++ b/config/config.go @@ -10,9 +10,6 @@ import ( "gopkg.in/ini.v1" ) -const LegacyVersionFileDefault = false -const AlwaysKeepDownloadDefault = false -const DisablePluginShortNameRepositoryDefault = false const ForcePrependDefault = false const DataDirDefault = "~/.asdf" const ConfigFileDefault = "~/.asdfrc" @@ -50,6 +47,16 @@ type Config struct { Settings Settings } +func defaultSettings() *Settings { + return &Settings{ + Loaded: false, + LegacyVersionFile: false, + AlwaysKeepDownload: false, + PluginRepositoryLastCheckDuration: PluginRepoCheckDurationDefault, + DisablePluginShortNameRepository: false, + } +} + func NewPluginRepoCheckDuration(checkDuration string) PluginRepoCheckDuration { if strings.ToLower(checkDuration) == "never" { return PluginRepoCheckDuration{Never: true} @@ -174,26 +181,26 @@ func loadSettings(asdfrcPath string) (Settings, error) { } mainConf := config.Section("") - checkDuration := NewPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String()) - return Settings{ - Loaded: true, - LegacyVersionFile: yesNoToBool(mainConf, "legacy_version_file", LegacyVersionFileDefault), - AlwaysKeepDownload: yesNoToBool(mainConf, "use_release_candidates", AlwaysKeepDownloadDefault), - PluginRepositoryLastCheckDuration: checkDuration, - DisablePluginShortNameRepository: yesNoToBool(mainConf, "disable_plugin_short_name_repository", DisablePluginShortNameRepositoryDefault), - }, nil + settings := defaultSettings() + + settings.Loaded = true + settings.PluginRepositoryLastCheckDuration = NewPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String()) + + boolOverride(&settings.LegacyVersionFile, mainConf, "legacy_version_file") + boolOverride(&settings.AlwaysKeepDownload, mainConf, "always_keep_download") + boolOverride(&settings.DisablePluginShortNameRepository, mainConf, "disable_plugin_short_name_repository") + + return *settings, nil } -func yesNoToBool(section *ini.Section, key string, defaultValue bool) bool { - yesOrNo := section.Key(key).String() - lcYesOrNo := strings.ToLower(yesOrNo) +func boolOverride(field *bool, section *ini.Section, key string) { + lcYesOrNo := strings.ToLower(section.Key(key).String()) + if lcYesOrNo == "yes" { - return true + *field = true } if lcYesOrNo == "no" { - return false + *field = false } - - return defaultValue }