summary refs log tree commit diff stats
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
parent80704f242206e56d4c07669a5ca42827c1d59b59 (diff)
downloadaerc-f42724caf3b374df0fa4eb170b3527cd20814eed.tar.gz
Install default configs to XDG config if not found
-rw-r--r--Makefile22
-rw-r--r--aerc.go7
-rw-r--r--config/config.go38
3 files changed, 53 insertions, 14 deletions
diff --git a/Makefile b/Makefile
index 64ba7d1..5708845 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,15 @@
+PREFIX?=/usr/local
+_INSTDIR=$(DESTDIR)$(PREFIX)
+BINDIR?=$(_INSTDIR)/bin
+SHAREDIR?=$(_INSTDIR)/share/aerc
+MANDIR?=$(_INSTDIR)/share/man
 GOFLAGS?=
 
 aerc:
-	go build $(GOFLAGS) -o aerc
+	go build $(GOFLAGS) \
+		-ldflags "-X main.Prefix=$(PREFIX)" \
+		-ldflags "-X main.ShareDir=$(SHAREDIR)" \
+		-o aerc
 
 %.1: doc/%.1.scd
 	scdoc < $< > $@
@@ -19,17 +27,13 @@ DOCS := \
 	aerc-smtp.5 \
 	aerc-tutorial.7
 
-all: aerc $(DOCS)
+doc: $(DOCS)
+
+all: aerc doc
 
 clean:
 	rm -f *.1 *.5 aerc
 
-PREFIX?=/usr/local
-_INSTDIR=$(DESTDIR)$(PREFIX)
-BINDIR?=$(_INSTDIR)/bin
-SHAREDIR?=$(_INSTDIR)/share/aerc
-MANDIR?=$(_INSTDIR)/share/man
-
 install: all
 	mkdir -p $(BINDIR) $(MANDIR)/man1 $(MANDIR)/man5 \
 		$(SHAREDIR) $(SHAREDIR)/filters
@@ -47,4 +51,4 @@ install: all
 
 .DEFAULT_GOAL := all
 
-.PHONY: aerc clean install
+.PHONY: aerc all doc clean install
diff --git a/aerc.go b/aerc.go
index 6f794c6..22df069 100644
--- a/aerc.go
+++ b/aerc.go
@@ -47,6 +47,11 @@ func getCommands(selected libui.Drawable) []*commands.Commands {
 	}
 }
 
+var (
+	Prefix   string
+	ShareDir string
+)
+
 func main() {
 	var (
 		logOut io.Writer
@@ -60,7 +65,7 @@ func main() {
 	logger = log.New(logOut, "", log.LstdFlags)
 	logger.Println("Starting up aerc")
 
-	conf, err := config.LoadConfig(nil)
+	conf, err := config.LoadConfig(nil, ShareDir)
 	if err != nil {
 		fmt.Printf("Failed to load config: %v\n", err)
 		os.Exit(1)
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,
s' href='/akkartik/mu/commit/html/continuation3.mu.html?h=hlt&id=363652449ce690b856e48ab58320b102ddeaa190'>36365244 ^
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96