From f42724caf3b374df0fa4eb170b3527cd20814eed Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 22 May 2019 12:35:44 -0400 Subject: Install default configs to XDG config if not found --- config/config.go | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'config/config.go') diff --git a/config/config.go b/config/config.go index 1019297b..1fb764b0 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "errors" "fmt" + "io/ioutil" "net/url" "os" "os/exec" @@ -189,7 +190,25 @@ func parseCredential(cred, command string) (string, error) { return u.String(), nil } -func LoadConfig(root *string) (*AercConfig, error) { +func installTemplate(root, sharedir, name string) error { + if _, err := os.Stat(root); os.IsNotExist(err) { + err := os.MkdirAll(root, 0755) + if err != nil { + return err + } + } + data, err := ioutil.ReadFile(path.Join(sharedir, name)) + if err != nil { + return err + } + err = ioutil.WriteFile(path.Join(root, name), data, 0644) + if err != nil { + return err + } + return nil +} + +func LoadConfig(root *string, sharedir string) (*AercConfig, error) { if root == nil { _root := path.Join(xdg.ConfigHome(), "aerc") root = &_root @@ -201,7 +220,12 @@ func LoadConfig(root *string) (*AercConfig, error) { filename = path.Join(*root, "aerc.conf") file, err := ini.Load(filename) if err != nil { - return nil, err + if err := installTemplate(*root, sharedir, "aerc.conf"); err != nil { + return nil, err + } + if file, err = ini.Load(filename); err != nil { + return nil, err + } } file.NameMapper = mapName config := &AercConfig{ @@ -291,9 +315,15 @@ func LoadConfig(root *string) (*AercConfig, error) { } else { config.Accounts = accounts } - binds, err := ini.Load(path.Join(*root, "binds.conf")) + filename = path.Join(*root, "binds.conf") + binds, err := ini.Load(filename) if err != nil { - return nil, err + if err := installTemplate(*root, sharedir, "binds.conf"); err != nil { + return nil, err + } + if binds, err = ini.Load(filename); err != nil { + return nil, err + } } groups := map[string]**KeyBindings{ "default": &config.Bindings.Global, -- cgit