From 50647d589544abab35bcfb3d120bd2b6f907525e Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Wed, 7 Aug 2024 20:52:29 +0200 Subject: config: add reload helper functions Add reload helper function to reload config files. Store the initially used config files for the reloading. Signed-off-by: Koni Marti Tested-by: Inwit Acked-by: Robin Jarry --- config/binds.go | 2 +- config/config.go | 1 + config/reload.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config/ui.go | 10 ++++++--- 4 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 config/reload.go (limited to 'config') diff --git a/config/binds.go b/config/binds.go index 1d391af7..3305fd3a 100644 --- a/config/binds.go +++ b/config/binds.go @@ -163,7 +163,7 @@ func parseBinds(root string, filename string) error { } } } - + SetBindsFilename(filename) if err := parseBindsFromFile(root, filename); err != nil { return fmt.Errorf("%s: %w", filename, err) } diff --git a/config/config.go b/config/config.go index 61ac041e..6bab5fc8 100644 --- a/config/config.go +++ b/config/config.go @@ -134,6 +134,7 @@ func LoadConfigFromFile( } } } + SetConfFilename(filename) if err := parseConf(filename); err != nil { return fmt.Errorf("%s: %w", filename, err) } diff --git a/config/reload.go b/config/reload.go new file mode 100644 index 00000000..f572cea1 --- /dev/null +++ b/config/reload.go @@ -0,0 +1,68 @@ +package config + +import ( + "errors" + "os" + "path/filepath" + + "git.sr.ht/~rjarry/aerc/lib/log" +) + +type reloadStore struct { + binds string + conf string +} + +var rlst reloadStore + +func SetBindsFilename(fn string) { + log.Debugf("reloader: set binds file: %s", fn) + rlst.binds = fn +} + +func SetConfFilename(fn string) { + log.Debugf("reloader: set conf file: %s", fn) + rlst.conf = fn +} + +func ReloadBinds() (string, error) { + f := rlst.binds + if !exists(f) { + return f, os.ErrNotExist + } + log.Debugf("reload binds file: %s", f) + Binds = defaultBindsConfig() + return f, parseBindsFromFile(filepath.Dir(f), f) +} + +func ReloadConf() (string, error) { + f := rlst.conf + if !exists(f) { + return f, os.ErrNotExist + } + log.Debugf("reload conf file: %s", f) + + General = new(GeneralConfig) + Filters = nil + Compose = new(ComposeConfig) + Converters = make(map[string]string) + Viewer = new(ViewerConfig) + Statusline = new(StatuslineConfig) + Openers = nil + Hooks = HooksConfig{} + Ui = defaultUIConfig() + Templates = new(TemplateConfig) + + return f, parseConf(f) +} + +func ReloadAccounts() error { + return errors.New("not implemented") +} + +func exists(fn string) bool { + if _, err := os.Stat(fn); errors.Is(err, os.ErrNotExist) { + return false + } + return true +} diff --git a/config/ui.go b/config/ui.go index 9839db9b..578ad294 100644 --- a/config/ui.go +++ b/config/ui.go @@ -134,9 +134,13 @@ type uiContextKey struct { value string } -var Ui = &UIConfig{ - contextualCounts: make(map[uiContextType]int), - contextualCache: make(map[uiContextKey]*UIConfig), +var Ui = defaultUIConfig() + +func defaultUIConfig() *UIConfig { + return &UIConfig{ + contextualCounts: make(map[uiContextType]int), + contextualCache: make(map[uiContextKey]*UIConfig), + } } var uiContextualSectionRe = regexp.MustCompile(`^ui:(account|folder|subject)([~=])(.+)$`) -- cgit