summary refs log tree commit diff stats
path: root/config/config.go
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2019-05-22 12:35:44 -0400
committerDrew DeVault <sir@cmpwn.com>2019-05-22 12:35:44 -0400
commitf42724caf3b374df0fa4eb170b3527cd20814eed (patch)
tree6efdb42ce0423e25c172382565a3c317b836737b /config/config.go
parent80704f242206e56d4c07669a5ca42827c1d59b59 (diff)
downloadaerc-f42724caf3b374df0fa4eb170b3527cd20814eed.tar.gz
Install default configs to XDG config if not found
Diffstat (limited to 'config/config.go')
-rw-r--r--config/config.go38
1 files changed, 34 insertions, 4 deletions
diff --git a/config/config.go b/config/config.go
index 1019297..1fb764b 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,