Debian Codegraph @

config/general.go

raw
1 package config
2
3 import (
4 "errors"
5 "fmt"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "sync/atomic"
10
11 "git.sr.ht/~rjarry/aerc/lib/log"
12 "git.sr.ht/~rjarry/aerc/lib/xdg"
13 "github.com/go-ini/ini"
14 "github.com/mattn/go-isatty"
15 )
16
17 type GeneralConfig struct {
18 DefaultSavePath string `ini:"default-save-path"`
19 PgpProvider string `ini:"pgp-provider" default:"auto" parse:"ParsePgpProvider"`
20 UnsafeAccountsConf bool `ini:"unsafe-accounts-conf"`
21 LogFile string `ini:"log-file"`
22 LogLevel log.LogLevel `ini:"log-level" default:"info" parse:"ParseLogLevel"`
23 DisableIPC bool `ini:"disable-ipc"`
24 DisableIPCMailto bool `ini:"disable-ipc-mailto"`
25 DisableIPCMbox bool `ini:"disable-ipc-mbox"`
26 InheritHostTTYFeatures bool `ini:"inherit-host-tty-features" default:"true"`
27 Term string `ini:"term" default:"xterm-256color"`
28 DefaultMenuCmd string `ini:"default-menu-cmd"`
29 QuakeMode bool `ini:"enable-quake-mode" default:"false"`
30 UsePinentry bool `ini:"use-terminal-pinentry" default:"false"`
31 TempDir string `ini:"temporary-directory" default:"" parse:"ParseTempDir"`
32 }
33
34 var generalConfig atomic.Pointer[GeneralConfig]
35
36 func General() *GeneralConfig {
37 return generalConfig.Load()
38 }
39
40 func parseGeneral(file *ini.File) (*GeneralConfig, error) {
41 var logFile *os.File
42
43 conf := new(GeneralConfig)
44
45 if err := MapToStruct(file.Section("general"), conf, true); err != nil {
46 return nil, err
47 }
48
49 useStdout := false
50 if !isatty.IsTerminal(os.Stdout.Fd()) {
51 logFile = os.Stdout
52 useStdout = true
53 // redirected to file, force TRACE level
54 conf.LogLevel = log.TRACE
55 } else if conf.LogFile != "" {
56 var err error
57 path := xdg.ExpandHome(conf.LogFile)
58 err = os.MkdirAll(filepath.Dir(path), 0o700)
59 if err != nil {
60 return nil, fmt.Errorf("log-file: %w", err)
61 }
62 logFile, err = os.OpenFile(path,
63 os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
64 if err != nil {
65 return nil, err
66 }
67 }
68
69 err := log.Init(logFile, useStdout, conf.LogLevel)
70 if err != nil {
71 return nil, err
72 }
73
74 if file.Section("general").HasKey("enable-osc8") {
75 Warnings = append(Warnings, Warning{
76 Title: "aerc.conf: [general].enable-osc8 is deprecated",
77 Body: `The enable-osc8 option is deprecated and has no effect.
78
79 OSC 8 hyperlink support can be now automatically detected from the host
80 terminal. The [general].inherit-host-tty-features option enables this
81 capability detection for embedded terminals.
82
83 Please remove enable-osc8 from aerc.conf.`,
84 })
85 }
86
87 log.Debugf("aerc.conf: [general] %#v", conf)
88
89 return conf, nil
90 }
91
92 func (gen *GeneralConfig) ParseLogLevel(sec *ini.Section, key *ini.Key) (log.LogLevel, error) {
93 return log.ParseLevel(key.String())
94 }
95
96 func (gen *GeneralConfig) ParsePgpProvider(sec *ini.Section, key *ini.Key) (string, error) {
97 switch key.String() {
98 case "gpg", "internal", "auto":
99 return key.String(), nil
100 }
101 return "", fmt.Errorf("must be either auto, gpg or internal")
102 }
103
104 func (gen *GeneralConfig) ParseTempDir(_ *ini.Section, key *ini.Key) (string, error) {
105 tmp := key.String()
106 if tmp == "" {
107 tmp = os.TempDir()
108 }
109 tmpPath := xdg.ExpandHome(tmp)
110 err := os.MkdirAll(tmpPath, 0o700)
111 if err != nil && !errors.Is(err, fs.ErrExist) {
112 return "", err
113 }
114 return tmpPath, nil
115 }