Merge pull request #33 from asdf-vm/tb/default-config-refactor

chore(golang-rewrite): default settings refactor
This commit is contained in:
Trevor Brown 2024-03-25 20:52:44 -04:00 committed by Trevor Brown
commit 3ffeec2ea0

View File

@ -10,9 +10,6 @@ import (
"gopkg.in/ini.v1" "gopkg.in/ini.v1"
) )
const LegacyVersionFileDefault = false
const AlwaysKeepDownloadDefault = false
const DisablePluginShortNameRepositoryDefault = false
const ForcePrependDefault = false const ForcePrependDefault = false
const DataDirDefault = "~/.asdf" const DataDirDefault = "~/.asdf"
const ConfigFileDefault = "~/.asdfrc" const ConfigFileDefault = "~/.asdfrc"
@ -50,6 +47,16 @@ type Config struct {
Settings Settings Settings Settings
} }
func defaultSettings() *Settings {
return &Settings{
Loaded: false,
LegacyVersionFile: false,
AlwaysKeepDownload: false,
PluginRepositoryLastCheckDuration: PluginRepoCheckDurationDefault,
DisablePluginShortNameRepository: false,
}
}
func NewPluginRepoCheckDuration(checkDuration string) PluginRepoCheckDuration { func NewPluginRepoCheckDuration(checkDuration string) PluginRepoCheckDuration {
if strings.ToLower(checkDuration) == "never" { if strings.ToLower(checkDuration) == "never" {
return PluginRepoCheckDuration{Never: true} return PluginRepoCheckDuration{Never: true}
@ -174,26 +181,26 @@ func loadSettings(asdfrcPath string) (Settings, error) {
} }
mainConf := config.Section("") mainConf := config.Section("")
checkDuration := NewPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String())
return Settings{ settings := defaultSettings()
Loaded: true,
LegacyVersionFile: yesNoToBool(mainConf, "legacy_version_file", LegacyVersionFileDefault), settings.Loaded = true
AlwaysKeepDownload: yesNoToBool(mainConf, "use_release_candidates", AlwaysKeepDownloadDefault), settings.PluginRepositoryLastCheckDuration = NewPluginRepoCheckDuration(mainConf.Key("plugin_repository_last_check_duration").String())
PluginRepositoryLastCheckDuration: checkDuration,
DisablePluginShortNameRepository: yesNoToBool(mainConf, "disable_plugin_short_name_repository", DisablePluginShortNameRepositoryDefault), boolOverride(&settings.LegacyVersionFile, mainConf, "legacy_version_file")
}, nil 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 { func boolOverride(field *bool, section *ini.Section, key string) {
yesOrNo := section.Key(key).String() lcYesOrNo := strings.ToLower(section.Key(key).String())
lcYesOrNo := strings.ToLower(yesOrNo)
if lcYesOrNo == "yes" { if lcYesOrNo == "yes" {
return true *field = true
} }
if lcYesOrNo == "no" { if lcYesOrNo == "no" {
return false *field = false
} }
return defaultValue
} }