summary refs log tree commit diff stats
path: root/config
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
parent80704f242206e56d4c07669a5ca42827c1d59b59 (diff)
downloadaerc-f42724caf3b374df0fa4eb170b3527cd20814eed.tar.gz
Install default configs to XDG config if not found
Diffstat (limited to 'config')
-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,
ster names to lowercase' href='/akkartik/mu/commit/examples/ex3.subx?h=hlt&id=333525360b22f3d3ea31db46a4d2f1b4edbfebdb'>33352536 ^
6070c23e ^
6030d7e2 ^
33352536 ^
6030d7e2 ^
33352536 ^
6030d7e2 ^

97a9bd17 ^
f3901d90 ^
ecfbbfb5 ^

294a1520 ^
ee9a9237 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36