diff options
author | Robin Jarry <robin@jarry.cc> | 2022-11-15 00:29:19 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-11-16 16:12:03 +0100 |
commit | 17bb9387c4a3ecab17ace3dc78694c2236a1024f (patch) | |
tree | bd1f9e0c249d2838872e8e5e7605de25566052a8 /config | |
parent | eb10c328fd8953f4a2cc7d22126f4214a57c9261 (diff) | |
download | aerc-17bb9387c4a3ecab17ace3dc78694c2236a1024f.tar.gz |
config: move [filters] parsing to separate file
The config.go file is getting too big. Move the aerc.conf [filters]
section parsing logic into a dedicated filters.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 | 50 | ||||
-rw-r--r-- | config/filters.go | 68 |
2 files changed, 71 insertions, 47 deletions
diff --git a/config/config.go b/config/config.go index d889393a..dc749cf4 100644 --- a/config/config.go +++ b/config/config.go @@ -18,11 +18,6 @@ import ( "git.sr.ht/~rjarry/aerc/logging" ) -const ( - FILTER_MIMETYPE = iota - FILTER_HEADER -) - type ComposeConfig struct { Editor string `ini:"editor"` HeaderLayout [][]string `ini:"-"` @@ -31,14 +26,6 @@ type ComposeConfig struct { NoAttachmentWarning *regexp.Regexp `ini:"-"` } -type FilterConfig struct { - FilterType int - Filter string - Command string - Header string - Regex *regexp.Regexp -} - type ViewerConfig struct { Pager string Alternatives []string @@ -156,39 +143,6 @@ func installTemplate(root, name string) error { } func (config *AercConfig) LoadConfig(file *ini.File) error { - if filters, err := file.GetSection("filters"); err == nil { - // TODO: Parse the filter more finely, e.g. parse the regex - for _, match := range filters.KeyStrings() { - cmd := filters.KeysHash()[match] - filter := FilterConfig{ - Command: cmd, - Filter: match, - } - switch { - case strings.Contains(match, ",~"): - filter.FilterType = FILTER_HEADER - header := filter.Filter[:strings.Index(filter.Filter, ",")] //nolint:gocritic // guarded by strings.Contains - regex := filter.Filter[strings.Index(filter.Filter, "~")+1:] - filter.Header = strings.ToLower(header) - filter.Regex, err = regexp.Compile(regex) - if err != nil { - return err - } - case strings.ContainsRune(match, ','): - filter.FilterType = FILTER_HEADER - header := filter.Filter[:strings.Index(filter.Filter, ",")] //nolint:gocritic // guarded by strings.Contains - value := filter.Filter[strings.Index(filter.Filter, ",")+1:] - filter.Header = strings.ToLower(header) - filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value)) - if err != nil { - return err - } - default: - filter.FilterType = FILTER_MIMETYPE - } - config.Filters = append(config.Filters, filter) - } - } if openers, err := file.GetSection("openers"); err == nil { for mimeType, command := range openers.KeysHash() { mimeType = strings.ToLower(mimeType) @@ -313,6 +267,9 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { Openers: make(map[string][]string), } + if err := config.parseFilters(file); err != nil { + return nil, err + } if err = config.LoadConfig(file); err != nil { return nil, err } @@ -326,7 +283,6 @@ func LoadConfigFromFile(root *string, accts []string) (*AercConfig, error) { logging.Debugf("aerc.conf: [statusline] %#v", config.Statusline) logging.Debugf("aerc.conf: [viewer] %#v", config.Viewer) logging.Debugf("aerc.conf: [compose] %#v", config.Compose) - logging.Debugf("aerc.conf: [filters] %#v", config.Filters) logging.Debugf("aerc.conf: [openers] %#v", config.Openers) logging.Debugf("aerc.conf: [triggers] %#v", config.Triggers) diff --git a/config/filters.go b/config/filters.go new file mode 100644 index 00000000..c3d10f68 --- /dev/null +++ b/config/filters.go @@ -0,0 +1,68 @@ +package config + +import ( + "regexp" + "strings" + + "git.sr.ht/~rjarry/aerc/logging" + "github.com/go-ini/ini" +) + +type FilterType int + +const ( + FILTER_MIMETYPE FilterType = iota + FILTER_HEADER +) + +type FilterConfig struct { + Type FilterType + Filter string + Command string + Header string + Regex *regexp.Regexp +} + +func (config *AercConfig) parseFilters(file *ini.File) error { + filters, err := file.GetSection("filters") + if err != nil { + goto end + } + + // TODO: Parse the filter more finely, e.g. parse the regex + for match, cmd := range filters.KeysHash() { + filter := FilterConfig{ + Command: cmd, + Filter: match, + } + switch { + case strings.Contains(match, ",~"): + filter.Type = FILTER_HEADER + //nolint:gocritic // guarded by strings.Contains + header := filter.Filter[:strings.Index(filter.Filter, ",")] + regex := filter.Filter[strings.Index(filter.Filter, "~")+1:] + filter.Header = strings.ToLower(header) + filter.Regex, err = regexp.Compile(regex) + if err != nil { + return err + } + case strings.ContainsRune(match, ','): + filter.Type = FILTER_HEADER + //nolint:gocritic // guarded by strings.Contains + header := filter.Filter[:strings.Index(filter.Filter, ",")] + value := filter.Filter[strings.Index(filter.Filter, ",")+1:] + filter.Header = strings.ToLower(header) + filter.Regex, err = regexp.Compile(regexp.QuoteMeta(value)) + if err != nil { + return err + } + default: + filter.Type = FILTER_MIMETYPE + } + config.Filters = append(config.Filters, filter) + } + +end: + logging.Debugf("aerc.conf: [filters] %#v", config.Filters) + return nil +} |