diff options
author | Robin Jarry <robin@jarry.cc> | 2022-11-15 00:22:55 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-16 16:12:01 +0100 |
commit | eb10c328fd8953f4a2cc7d22126f4214a57c9261 (patch) | |
tree | 76381ba24bdb33c7cb0cba4edfa81cf6bfa11996 /config | |
parent | 21f82053075b320015ac622c683aafeac2b0a041 (diff) | |
download | aerc-eb10c328fd8953f4a2cc7d22126f4214a57c9261.tar.gz |
config: move [general] parsing in separate file
The config.go file is getting too big. Move the aerc.conf [general]
section parsing logic into a dedicated general.go file.
No functional change.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
Diffstat (limited to 'config')
-rw-r--r-- | config/config.go | 38 | ||||
-rw-r--r-- | config/general.go | 48 |
2 files changed, 51 insertions, 35 deletions
diff --git a/config/config.go b/config/config.go index 16d07e3f..d889393a 100644 --- a/config/config.go +++ b/config/config.go @@ -18,12 +18,6 @@ import ( "git.sr.ht/~rjarry/aerc/logging" ) -type GeneralConfig struct { - DefaultSavePath string `ini:"default-save-path"` - PgpProvider string `ini:"pgp-provider"` - UnsafeAccountsConf bool `ini:"unsafe-accounts-conf"` -} - const ( FILTER_MIMETYPE = iota FILTER_HEADER @@ -255,21 +249,6 @@ func (config *AercConfig) LoadConfig(file *ini.File) error { return nil } -func validatePgpProvider(section *ini.Section) error { - m := map[string]bool{ - "gpg": true, - "internal": true, - } - for key, val := range section.KeysHash() { - if key == "pgp-provider" { - if !m[strings.ToLower(val)] { - return fmt.Errorf("%v must be either 'gpg' or 'internal'", key) - } - } - } - return nil -} - func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { if root == nil { _root := path.Join(xdg.ConfigHome(), "aerc") @@ -299,11 +278,7 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { ContextualBinds: []BindingConfigContext{}, - General: GeneralConfig{ - PgpProvider: "internal", - UnsafeAccountsConf: false, - }, - + General: defaultGeneralConfig(), Ui: defaultUiConfig(), ContextualUis: []UIConfigContext{}, @@ -344,17 +319,10 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { if err := config.parseUi(file); err != nil { return nil, err } - - if ui, err := file.GetSection("general"); err == nil { - if err := ui.MapTo(&config.General); err != nil { - return nil, err - } - if err := validatePgpProvider(ui); err != nil { - return nil, err - } + if err := config.parseGeneral(file); err != nil { + return nil, err } - logging.Debugf("aerc.conf: [general] %#v", config.General) logging.Debugf("aerc.conf: [statusline] %#v", config.Statusline) logging.Debugf("aerc.conf: [viewer] %#v", config.Viewer) logging.Debugf("aerc.conf: [compose] %#v", config.Compose) diff --git a/config/general.go b/config/general.go new file mode 100644 index 00000000..7f8ed2ef --- /dev/null +++ b/config/general.go @@ -0,0 +1,48 @@ +package config + +import ( + "fmt" + + "git.sr.ht/~rjarry/aerc/logging" + "github.com/go-ini/ini" +) + +type GeneralConfig struct { + DefaultSavePath string `ini:"default-save-path"` + PgpProvider string `ini:"pgp-provider"` + UnsafeAccountsConf bool `ini:"unsafe-accounts-conf"` +} + +func defaultGeneralConfig() GeneralConfig { + return GeneralConfig{ + PgpProvider: "internal", + UnsafeAccountsConf: false, + } +} + +func (config *AercConfig) parseGeneral(file *ini.File) error { + gen, err := file.GetSection("general") + if err != nil { + goto end + } + + if err := gen.MapTo(&config.General); err != nil { + return err + } + if err := config.General.validatePgpProvider(); err != nil { + return err + } + +end: + logging.Debugf("aerc.conf: [general] %#v", config.General) + return nil +} + +func (gen *GeneralConfig) validatePgpProvider() error { + switch gen.PgpProvider { + case "gpg", "internal": + return nil + default: + return fmt.Errorf("pgp-provider must be either gpg or internal") + } +} |