Install default configs to XDG config if not found

This commit is contained in:
Drew DeVault 2019-05-22 12:35:44 -04:00
parent 80704f2422
commit f42724caf3
3 changed files with 53 additions and 14 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,8 +220,13 @@ func LoadConfig(root *string) (*AercConfig, error) {
filename = path.Join(*root, "aerc.conf")
file, err := ini.Load(filename)
if err != nil {
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{
Bindings: BindingConfig{
@ -291,10 +315,16 @@ 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 {
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,
"compose": &config.Bindings.Compose,