Debian Codegraph @

config/config.go

raw
1 package config
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "path"
8 "slices"
9 "strings"
10
11 "git.sr.ht/~rjarry/aerc/lib/xdg"
12 "github.com/go-ini/ini"
13 )
14
15 // Set at build time
16 var (
17 shareDir string
18 libexecDir string
19 )
20
21 func buildDefaultDirs() []string {
22 var defaultDirs []string
23
24 prefixes := []string{
25 xdg.ConfigPath(),
26 "~/.local/libexec",
27 xdg.DataPath(),
28 }
29
30 // Add XDG_CONFIG_HOME and XDG_DATA_HOME
31 for _, v := range prefixes {
32 if v != "" {
33 defaultDirs = append(defaultDirs, xdg.ExpandHome(v, "aerc"))
34 }
35 }
36
37 // Trim null chars inserted post-build by systems like Conda
38 shareDir := strings.TrimRight(shareDir, "\x00")
39 libexecDir := strings.TrimRight(libexecDir, "\x00")
40
41 // Add custom buildtime dirs
42 if libexecDir != "" && libexecDir != "/usr/local/libexec/aerc" {
43 defaultDirs = append(defaultDirs, xdg.ExpandHome(libexecDir))
44 }
45 if shareDir != "" && shareDir != "/usr/local/share/aerc" {
46 defaultDirs = append(defaultDirs, xdg.ExpandHome(shareDir))
47 }
48
49 // Add fixed fallback locations
50 defaultDirs = append(defaultDirs, "/usr/local/libexec/aerc")
51 defaultDirs = append(defaultDirs, "/usr/local/share/aerc")
52 defaultDirs = append(defaultDirs, "/usr/libexec/aerc")
53 defaultDirs = append(defaultDirs, "/usr/share/aerc")
54
55 return defaultDirs
56 }
57
58 var SearchDirs = buildDefaultDirs()
59
60 func installTemplate(root, name string) error {
61 var err error
62 if _, err = os.Stat(root); os.IsNotExist(err) {
63 err = os.MkdirAll(root, 0o755)
64 if err != nil {
65 return err
66 }
67 }
68 var data []byte
69 for _, dir := range SearchDirs {
70 data, err = os.ReadFile(path.Join(dir, name))
71 if err == nil {
72 break
73 }
74 }
75 if err != nil {
76 return err
77 }
78 err = os.WriteFile(path.Join(root, name), data, 0o644)
79 if err != nil {
80 return err
81 }
82 return nil
83 }
84
85 func parseConf(filename string) error {
86 file, err := ini.LoadSources(ini.LoadOptions{
87 KeyValueDelimiters: "=",
88 }, filename)
89 if err != nil {
90 return err
91 }
92 general, err := parseGeneral(file)
93 if err != nil {
94 return err
95 }
96 filters, err := parseFilters(file)
97 if err != nil {
98 return err
99 }
100 compose, err := parseCompose(file)
101 if err != nil {
102 return err
103 }
104 converters, err := parseConverters(file)
105 if err != nil {
106 return err
107 }
108 viewer, err := parseViewer(file)
109 if err != nil {
110 return err
111 }
112 statusline, err := parseStatusline(file)
113 if err != nil {
114 return err
115 }
116 openers, err := parseOpeners(file)
117 if err != nil {
118 return err
119 }
120 hooks, err := parseHooks(file)
121 if err != nil {
122 return err
123 }
124 ui, err := parseUi(file)
125 if err != nil {
126 return err
127 }
128 templates, err := parseTemplates(file)
129 if err != nil {
130 return err
131 }
132
133 // config parse successful, atomically change all items at once
134 generalConfig.Store(general)
135 filtersConfig.Store(&filters)
136 composeConfig.Store(compose)
137 convertersConfig.Store(&converters)
138 viewerConfig.Store(viewer)
139 statuslineConfig.Store(statusline)
140 openersConfig.Store(&openers)
141 hooksConfig.Store(hooks)
142 uiConfig.Store(ui)
143 templatesConfig.Store(templates)
144
145 return nil
146 }
147
148 func init() {
149 // store empty values to ensure unit-tests pass without configuration
150 generalConfig.Store(&GeneralConfig{})
151 filtersConfig.Store(nil)
152 composeConfig.Store(&ComposeConfig{})
153 convertersConfig.Store(nil)
154 viewerConfig.Store(&ViewerConfig{})
155 statuslineConfig.Store(&StatuslineConfig{})
156 openersConfig.Store(nil)
157 hooksConfig.Store(&HooksConfig{})
158 ui := &UIConfig{}
159 ui.style.Store(&StyleSet{})
160 uiConfig.Store(ui)
161 templatesConfig.Store(&TemplateConfig{})
162 }
163
164 func LoadConfigFromFile(
165 root *string, accts []string, filename, bindPath, acctPath string,
166 ) error {
167 if root == nil {
168 _root := xdg.ConfigPath("aerc")
169 root = &_root
170 }
171 if filename == "" {
172 filename = path.Join(*root, "aerc.conf")
173 // if it doesn't exist copy over the template, then load
174 if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
175 fmt.Printf("%s not found, installing the system default\n", filename)
176 if err := installTemplate(*root, "aerc.conf"); err != nil {
177 return err
178 }
179 }
180 }
181 SetConfFilename(filename)
182 if err := parseConf(filename); err != nil {
183 return fmt.Errorf("%s: %w", filename, err)
184 }
185 if err := parseAccounts(*root, accts, acctPath); err != nil {
186 return err
187 }
188 if err := parseBinds(*root, bindPath); err != nil {
189 return err
190 }
191 return nil
192 }
193
194 func parseLayout(layout string) [][]string {
195 rows := strings.Split(layout, ",")
196 l := make([][]string, len(rows))
197 for i, r := range rows {
198 l[i] = strings.Split(r, "|")
199 }
200 return l
201 }
202
203 func contains(list []string, v string) bool {
204 return slices.Contains(list, v)
205 }
206
207 // warning message related to configuration (deprecation, etc.)
208 type Warning struct {
209 Title string
210 Body string
211 }
212
213 var Warnings []Warning