aboutsummaryrefslogtreecommitdiffstats
path: root/config/general.go
blob: 65c20cfd2e06b57cc9168545a22f50570b520ad6 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package config

import (
	"fmt"
	"os"

	"git.sr.ht/~rjarry/aerc/log"
	"github.com/go-ini/ini"
	"github.com/mattn/go-isatty"
	"github.com/mitchellh/go-homedir"
)

type GeneralConfig struct {
	DefaultSavePath    string       `ini:"default-save-path"`
	PgpProvider        string       `ini:"pgp-provider" default:"auto" parse:"ParsePgpProvider"`
	UnsafeAccountsConf bool         `ini:"unsafe-accounts-conf"`
	LogFile            string       `ini:"log-file"`
	LogLevel           log.LogLevel `ini:"log-level" default:"info" parse:"ParseLogLevel"`
	DisableIPC         bool         `ini:"disable-ipc"`
	EnableOSC8         bool         `ini:"enable-osc8" default:"false"`
	Term               string       `ini:"term" default:"xterm-256color"`
}

var General = new(GeneralConfig)

func parseGeneral(file *ini.File) error {
	var logFile *os.File

	if err := MapToStruct(file.Section("general"), General, true); err != nil {
		return err
	}
	if !isatty.IsTerminal(os.Stdout.Fd()) {
		logFile = os.Stdout
		// redirected to file, force TRACE level
		General.LogLevel = log.TRACE
	} else if General.LogFile != "" {
		path, err := homedir.Expand(General.LogFile)
		if err != nil {
			return fmt.Errorf("log-file: %w", err)
		}
		logFile, err = os.OpenFile(path,
			os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
		if err != nil {
			return fmt.Errorf("log-file: %w", err)
		}
	}
	log.Init(logFile, General.LogLevel)
	log.Debugf("aerc.conf: [general] %#v", General)
	return nil
}

func (gen *GeneralConfig) ParseLogLevel(sec *ini.Section, key *ini.Key) (log.LogLevel, error) {
	return log.ParseLevel(key.String())
}

func (gen *GeneralConfig) ParsePgpProvider(sec *ini.Section, key *ini.Key) (string, error) {
	switch key.String() {
	case "gpg", "internal", "auto":
		return key.String(), nil
	}
	return "", fmt.Errorf("must be either auto, gpg or internal")
}