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 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, -- cgit 1.4.1-2-gfad0 r/tree/?h=v1.7.0&id=ab3ecc707fa9f4243a02809c7de7a781dbf06b5a'>root/setup.py
blob: e63e28d27612854a1c449b93ab78708a339123b0 (plain) (blame)
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