diff options
author | Robin Jarry <robin@jarry.cc> | 2022-11-15 00:14:19 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-16 16:11:59 +0100 |
commit | 21f82053075b320015ac622c683aafeac2b0a041 (patch) | |
tree | 5b9c6d548a1b8bcc5dbd13cd38061db1e0c84142 | |
parent | b9e5346d6a136244212c913c80a2661d69de692c (diff) | |
download | aerc-21f82053075b320015ac622c683aafeac2b0a041.tar.gz |
config: move [templates] parsing in separate file
The config.go file is getting too big. Move the aerc.conf [templates]
section parsing logic into a dedicated templates.go file.
No functional change.
Signed-off-by: Robin Jarry <robin@jarry.cc>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
-rw-r--r-- | config/config.go | 51 | ||||
-rw-r--r-- | config/templates.go | 62 |
2 files changed, 67 insertions, 46 deletions
diff --git a/config/config.go b/config/config.go index 91fd99a2..16d07e3f 100644 --- a/config/config.go +++ b/config/config.go @@ -15,7 +15,6 @@ import ( "github.com/kyoh86/xdg" "github.com/mitchellh/go-homedir" - "git.sr.ht/~rjarry/aerc/lib/templates" "git.sr.ht/~rjarry/aerc/logging" ) @@ -67,13 +66,6 @@ type TriggersConfig struct { ExecuteCommand func(command []string) error } -type TemplateConfig struct { - TemplateDirs []string `ini:"template-dirs" delim:":"` - NewMessage string `ini:"new-message"` - QuotedReply string `ini:"quoted-reply"` - Forwards string `ini:"forwards"` -} - type AercConfig struct { Bindings BindingConfig ContextualBinds []BindingConfigContext @@ -259,35 +251,6 @@ func (config *AercConfig) LoadConfig(file *ini.File) error { return err } } - if templatesSec, err := file.GetSection("templates"); err == nil { - if err := templatesSec.MapTo(&config.Templates); err != nil { - return err - } - templateDirs := templatesSec.Key("template-dirs").String() - if templateDirs != "" { - config.Templates.TemplateDirs = strings.Split(templateDirs, ":") - } - } - - // append default paths to template-dirs - for _, dir := range SearchDirs { - config.Templates.TemplateDirs = append( - config.Templates.TemplateDirs, path.Join(dir, "templates"), - ) - } - - // we want to fail during startup if the templates are not ok - // hence we do dummy executes here - t := config.Templates - if err := templates.CheckTemplate(t.NewMessage, t.TemplateDirs); err != nil { - return err - } - if err := templates.CheckTemplate(t.QuotedReply, t.TemplateDirs); err != nil { - return err - } - if err := templates.CheckTemplate(t.Forwards, t.TemplateDirs); err != nil { - return err - } return nil } @@ -371,14 +334,8 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { ReplyToSelf: true, }, - Templates: TemplateConfig{ - TemplateDirs: []string{}, - NewMessage: "new_message", - QuotedReply: "quoted_reply", - Forwards: "forward_as_body", - }, - - Openers: make(map[string][]string), + Templates: defaultTemplatesConfig(), + Openers: make(map[string][]string), } if err = config.LoadConfig(file); err != nil { @@ -404,8 +361,10 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { logging.Debugf("aerc.conf: [filters] %#v", config.Filters) logging.Debugf("aerc.conf: [openers] %#v", config.Openers) logging.Debugf("aerc.conf: [triggers] %#v", config.Triggers) - logging.Debugf("aerc.conf: [templates] %#v", config.Templates) + if err := config.parseTemplates(file); err != nil { + return nil, err + } if err := config.parseAccounts(*root, accts); err != nil { return nil, err } diff --git a/config/templates.go b/config/templates.go new file mode 100644 index 00000000..2f581e44 --- /dev/null +++ b/config/templates.go @@ -0,0 +1,62 @@ +package config + +import ( + "path" + "strings" + + "git.sr.ht/~rjarry/aerc/lib/templates" + "git.sr.ht/~rjarry/aerc/logging" + "github.com/go-ini/ini" +) + +type TemplateConfig struct { + TemplateDirs []string `ini:"template-dirs" delim:":"` + NewMessage string `ini:"new-message"` + QuotedReply string `ini:"quoted-reply"` + Forwards string `ini:"forwards"` +} + +func defaultTemplatesConfig() TemplateConfig { + return TemplateConfig{ + TemplateDirs: []string{}, + NewMessage: "new_message", + QuotedReply: "quoted_reply", + Forwards: "forward_as_body", + } +} + +func (config *AercConfig) parseTemplates(file *ini.File) error { + if templatesSec, err := file.GetSection("templates"); err == nil { + if err := templatesSec.MapTo(&config.Templates); err != nil { + return err + } + templateDirs := templatesSec.Key("template-dirs").String() + if templateDirs != "" { + config.Templates.TemplateDirs = strings.Split(templateDirs, ":") + } + } + + // append default paths to template-dirs + for _, dir := range SearchDirs { + config.Templates.TemplateDirs = append( + config.Templates.TemplateDirs, path.Join(dir, "templates"), + ) + } + + // we want to fail during startup if the templates are not ok + // hence we do dummy executes here + t := config.Templates + if err := templates.CheckTemplate(t.NewMessage, t.TemplateDirs); err != nil { + return err + } + if err := templates.CheckTemplate(t.QuotedReply, t.TemplateDirs); err != nil { + return err + } + if err := templates.CheckTemplate(t.Forwards, t.TemplateDirs); err != nil { + return err + } + + logging.Debugf("aerc.conf: [templates] %#v", config.Templates) + + return nil +} |