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?= GOFLAGS?=
aerc: 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 %.1: doc/%.1.scd
scdoc < $< > $@ scdoc < $< > $@
@ -19,17 +27,13 @@ DOCS := \
aerc-smtp.5 \ aerc-smtp.5 \
aerc-tutorial.7 aerc-tutorial.7
all: aerc $(DOCS) doc: $(DOCS)
all: aerc doc
clean: clean:
rm -f *.1 *.5 aerc rm -f *.1 *.5 aerc
PREFIX?=/usr/local
_INSTDIR=$(DESTDIR)$(PREFIX)
BINDIR?=$(_INSTDIR)/bin
SHAREDIR?=$(_INSTDIR)/share/aerc
MANDIR?=$(_INSTDIR)/share/man
install: all install: all
mkdir -p $(BINDIR) $(MANDIR)/man1 $(MANDIR)/man5 \ mkdir -p $(BINDIR) $(MANDIR)/man1 $(MANDIR)/man5 \
$(SHAREDIR) $(SHAREDIR)/filters $(SHAREDIR) $(SHAREDIR)/filters
@ -47,4 +51,4 @@ install: all
.DEFAULT_GOAL := 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() { func main() {
var ( var (
logOut io.Writer logOut io.Writer
@ -60,7 +65,7 @@ func main() {
logger = log.New(logOut, "", log.LstdFlags) logger = log.New(logOut, "", log.LstdFlags)
logger.Println("Starting up aerc") logger.Println("Starting up aerc")
conf, err := config.LoadConfig(nil) conf, err := config.LoadConfig(nil, ShareDir)
if err != nil { if err != nil {
fmt.Printf("Failed to load config: %v\n", err) fmt.Printf("Failed to load config: %v\n", err)
os.Exit(1) os.Exit(1)

View file

@ -3,6 +3,7 @@ package config
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
@ -189,7 +190,25 @@ func parseCredential(cred, command string) (string, error) {
return u.String(), nil 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 { if root == nil {
_root := path.Join(xdg.ConfigHome(), "aerc") _root := path.Join(xdg.ConfigHome(), "aerc")
root = &_root root = &_root
@ -201,8 +220,13 @@ func LoadConfig(root *string) (*AercConfig, error) {
filename = path.Join(*root, "aerc.conf") filename = path.Join(*root, "aerc.conf")
file, err := ini.Load(filename) file, err := ini.Load(filename)
if err != nil { if err != nil {
if err := installTemplate(*root, sharedir, "aerc.conf"); err != nil {
return nil, err return nil, err
} }
if file, err = ini.Load(filename); err != nil {
return nil, err
}
}
file.NameMapper = mapName file.NameMapper = mapName
config := &AercConfig{ config := &AercConfig{
Bindings: BindingConfig{ Bindings: BindingConfig{
@ -291,10 +315,16 @@ func LoadConfig(root *string) (*AercConfig, error) {
} else { } else {
config.Accounts = accounts 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 != nil {
if err := installTemplate(*root, sharedir, "binds.conf"); err != nil {
return nil, err return nil, err
} }
if binds, err = ini.Load(filename); err != nil {
return nil, err
}
}
groups := map[string]**KeyBindings{ groups := map[string]**KeyBindings{
"default": &config.Bindings.Global, "default": &config.Bindings.Global,
"compose": &config.Bindings.Compose, "compose": &config.Bindings.Compose,