aboutsummaryrefslogtreecommitdiff
path: root/internal/config/settings.go
blob: 22b2f91de4efd765caad576f0b32dc60fd4aa493 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier: GPL-3.0-or-later

package config

import (
	"fmt"
	"slices"
	"time"

	"krino/internal/sexp"
)

// CaseMode says whether name, path and content matching ignores case.
type CaseMode int

const (
	CaseIgnore CaseMode = iota
	CaseStrict
)

// Conflict is what to do when an action's target already exists.
type Conflict int

const (
	ConflictSuffix Conflict = iota
	ConflictSkip
	ConflictOverwrite
)

// String is the word a CaseMode is written as in a configuration.
func (m CaseMode) String() string {
	switch m {
	case CaseIgnore:
		return "ignore"
	case CaseStrict:
		return "strict"
	}
	return fmt.Sprintf("CaseMode(%d)", int(m))
}

// String is the word a Conflict policy is written as in a configuration.
func (c Conflict) String() string {
	switch c {
	case ConflictSuffix:
		return "suffix"
	case ConflictSkip:
		return "skip"
	case ConflictOverwrite:
		return "overwrite"
	}
	return fmt.Sprintf("Conflict(%d)", int(c))
}

// Settings holds what one level of config sets; nil means not set there.
type Settings struct {
	Case       *CaseMode
	Fold       *bool
	Recursive  *bool
	MaxDepth   *int
	MinAge     *time.Duration
	MaxRead    *int64
	MaxSize    *int64
	Busy       *[]string
	OnConflict *Conflict
}

// Resolved is a complete set of settings.
type Resolved struct {
	Case       CaseMode
	Fold       bool
	Recursive  bool
	MaxDepth   int // 0 means unlimited
	MinAge     time.Duration
	MaxRead    int64
	MaxSize    int64 // 0 means unlimited
	Busy       []string
	OnConflict Conflict
}

// Builtin is what applies when nothing is set.
func Builtin() Resolved {
	return Resolved{
		Case:       CaseIgnore,
		Fold:       true,
		MinAge:     2 * time.Minute,
		MaxRead:    50 << 20,
		Busy:       []string{".part", ".aria2", ".crdownload"},
		OnConflict: ConflictSuffix,
	}
}

// Over returns base with every setting that s sets replaced.
func (s Settings) Over(base Resolved) Resolved {
	r := base
	if s.Case != nil {
		r.Case = *s.Case
	}
	if s.Fold != nil {
		r.Fold = *s.Fold
	}
	if s.Recursive != nil {
		r.Recursive = *s.Recursive
	}
	if s.MaxDepth != nil {
		r.MaxDepth = *s.MaxDepth
	}
	if s.MinAge != nil {
		r.MinAge = *s.MinAge
	}
	if s.MaxRead != nil {
		r.MaxRead = *s.MaxRead
	}
	if s.MaxSize != nil {
		r.MaxSize = *s.MaxSize
	}
	if s.Busy != nil {
		r.Busy = *s.Busy
	}
	if s.OnConflict != nil {
		r.OnConflict = *s.OnConflict
	}
	return r
}

var settingNames = []string{"case", "fold", "recursive", "max-depth", "min-age", "max-read", "max-size", "busy", "on-conflict"}

// ruleSettings are the settings a rule may override.
var ruleSettings = map[string]bool{"case": true, "fold": true, "on-conflict": true}

func isSetting(name string) bool { return slices.Contains(settingNames, name) }

var settingHint = map[string]string{
	"case":        "(case ignore) or (case strict)",
	"fold":        "(fold yes) or (fold no)",
	"recursive":   "(recursive yes) or (recursive no)",
	"max-depth":   "a number, like (max-depth 3)",
	"min-age":     "a duration, like (min-age 2m)",
	"max-read":    "a size, like (max-read 50M)",
	"max-size":    "a size, like (max-size 1G)",
	"on-conflict": "(on-conflict suffix), skip or overwrite",
}

// parse reads the setting form n into s, reporting problems to d. seen
// catches a setting given twice at the same level.
func (s *Settings) parse(n *sexp.Node, d *diags, seen map[string]*sexp.Node) {
	name := n.Head()
	if first, ok := seen[name]; ok {
		d.at(n, "%s set twice (first at line %d)", name, first.Pos.Line)
		return
	}
	seen[name] = n
	args := n.Args()
	if name == "busy" {
		list := []string{}
		for _, a := range args {
			if a.Kind != sexp.String {
				d.at(a, `busy takes strings, like ".part"; got %s`, a)
				continue
			}
			list = append(list, a.Text)
		}
		s.Busy = &list
		return
	}
	if len(args) == 1 && args[0].Kind == sexp.String {
		d.at(args[0], "%s values are bare words: write (%s %s)", name, name, args[0].Text)
		return
	}
	if len(args) != 1 || args[0].Kind != sexp.Symbol {
		d.at(n, "%s takes one value: %s", name, settingHint[name])
		return
	}
	v, at := args[0].Text, args[0]
	switch name {
	case "case":
		switch v {
		case "ignore":
			c := CaseIgnore
			s.Case = &c
		case "strict":
			c := CaseStrict
			s.Case = &c
		default:
			d.at(at, "case is ignore or strict, not %s", v)
		}
	case "fold", "recursive":
		b, ok := yesNo(v)
		if !ok {
			d.at(at, "%s is yes or no, not %s", name, v)
		} else if name == "fold" {
			s.Fold = &b
		} else {
			s.Recursive = &b
		}
	case "max-depth":
		depth, err := parseCount(v)
		if err != nil || depth < 1 || depth > 1<<20 {
			d.at(at, "max-depth is a whole number from 1, not %s", v)
			return
		}
		i := int(depth)
		s.MaxDepth = &i
	case "min-age":
		dur, err := ParseDuration(v)
		if err != nil {
			d.at(at, "min-age: %v", err)
			return
		}
		s.MinAge = &dur
	case "max-read":
		size, err := ParseSize(v)
		if err != nil {
			d.at(at, "max-read: %v", err)
			return
		}
		s.MaxRead = &size
	case "max-size":
		size, err := ParseSize(v)
		if err != nil {
			d.at(at, "max-size: %v", err)
			return
		}
		s.MaxSize = &size
	case "on-conflict":
		c, ok := map[string]Conflict{"suffix": ConflictSuffix, "skip": ConflictSkip, "overwrite": ConflictOverwrite}[v]
		if !ok {
			d.at(at, "on-conflict is suffix, skip or overwrite, not %s", v)
			return
		}
		s.OnConflict = &c
	}
}

func yesNo(v string) (bool, bool) {
	switch v {
	case "yes":
		return true, true
	case "no":
		return false, true
	}
	return false, false
}