diff options
-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 +} |