-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathdefault.go
437 lines (415 loc) · 8.68 KB
/
default.go
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package config
import (
"encoding/json"
"fmt"
"github.com/metafates/mangal/color"
"github.com/metafates/mangal/constant"
"github.com/metafates/mangal/key"
"github.com/metafates/mangal/style"
"github.com/samber/lo"
"github.com/spf13/viper"
"reflect"
"strconv"
"strings"
"text/template"
)
// Field represents a single config field
type Field struct {
// Key is the key of the field
Key string
// Value is the default value of the field
Value any
// Description is the description of the field
Description string
}
// typeName returns the type of the field without reflection
func (f *Field) typeName() string {
switch f.Value.(type) {
case string:
return "string"
case int:
return "int"
case bool:
return "bool"
case []string:
return "[]string"
case []int:
return "[]int"
default:
return "unknown"
}
}
func (f *Field) MarshalJSON() ([]byte, error) {
field := struct {
Key string `json:"key"`
Value any `json:"value"`
Default any `json:"default"`
Description string `json:"description"`
Type string `json:"type"`
}{
Key: f.Key,
Value: viper.Get(f.Key),
Default: f.Value,
Description: f.Description,
Type: f.typeName(),
}
return json.Marshal(field)
}
var prettyTemplate = lo.Must(template.New("pretty").Funcs(template.FuncMap{
"faint": style.Faint,
"bold": style.Bold,
"purple": style.Fg(color.Purple),
"blue": style.Fg(color.Blue),
"cyan": style.Fg(color.Cyan),
"value": func(k string) any { return viper.Get(k) },
"hl": func(v any) string {
switch value := v.(type) {
case bool:
b := strconv.FormatBool(value)
if value {
return style.Fg(color.Green)(b)
}
return style.Fg(color.Red)(b)
case string:
return style.Fg(color.Yellow)(value)
default:
return fmt.Sprint(value)
}
},
"typename": func(v any) string { return reflect.TypeOf(v).String() },
}).Parse(`{{ faint .Description }}
{{ blue "Key:" }} {{ purple .Key }}
{{ blue "Env:" }} {{ .Env }}
{{ blue "Value:" }} {{ hl (value .Key) }}
{{ blue "Default:" }} {{ hl (.Value) }}
{{ blue "Type:" }} {{ typename .Value }}`))
func (f *Field) Pretty() string {
var b strings.Builder
lo.Must0(prettyTemplate.Execute(&b, f))
return b.String()
}
func (f *Field) Env() string {
env := strings.ToUpper(EnvKeyReplacer.Replace(f.Key))
appPrefix := strings.ToUpper(constant.Mangal + "_")
if strings.HasPrefix(env, appPrefix) {
return env
}
return appPrefix + env
}
// Pretty format field as string for further cli output
//func (f *Field) Pretty() string {
// return fmt.Sprintf(
// `%s
//%s: %s = %s
//`,
// style.Faint(f.Description),
// style.Fg(color.Purple)(f.Key),
// style.Fg(color.Yellow)(f.typeName()),
// style.Fg(color.Cyan)(fmt.Sprintf("%v", viper.Get(f.Key))),
// )
//}
// defaults contains all default values for the config.
// It must contain all fields defined in the constant package.
var defaults = [key.DefinedFieldsCount]Field{
{
key.DownloaderPath,
".",
`Where to download manga
Absolute or relative.
You can also use tilde (~) to refer to your home directory or use env variables.
Examples: ~/... or $HOME/... or ${MANGA_PATH}-mangal`,
},
{
key.DownloaderChapterNameTemplate,
"[{padded-index}] {chapter}",
`Key template of the downloaded chapters
Path forbidden symbols will be replaced with "_"
Available variables:
{index} - index of the chapters
{padded-index} - same as index but padded with leading zeros
{chapters-count} - total number of chapters
{chapter} - name of the chapter
{manga} - name of the manga
{volume} - volume of the chapter
{source} - name of the source`,
},
{
key.DownloaderAsync,
true,
`Use asynchronous downloader (faster)
Do no turn it off unless you have some issues`,
},
{
key.DownloaderCreateMangaDir,
true,
`Create a subdirectory for each manga`,
},
{
key.DownloaderCreateVolumeDir,
false,
`Create a subdirectory for each volume`,
},
{
key.DownloaderReadDownloaded,
true,
"If chapter is already downloaded, read it instead of downloading it to temp",
},
{
key.DownloaderRedownloadExisting,
false,
`Redownload chapters that already exist`,
},
{
key.DownloaderDefaultSources,
[]string{},
`Default sources to use.
Will prompt if not set.
Type "mangal sources list" to show available sources`,
},
{
key.DownloaderStopOnError,
false,
`Stop downloading other chapters on error`,
},
{
key.DownloaderDownloadCover,
true,
`Whether to download manga cover or not`,
},
{
key.FormatsUse,
"pdf",
`Default format to export chapters
Available options are: pdf, zip, cbz, plain`,
},
{
key.FormatsSkipUnsupportedImages,
true,
`Will skip images that can't be converted to the specified format
Example: if you want to export to pdf, but some images are gifs, they will be skipped`,
},
{
key.MetadataFetchAnilist,
true,
`Fetch metadata from Anilist
It will also cache the results to not spam the API`,
},
{
key.MetadataComicInfoXML,
true,
`Generate ComicInfo.xml file for each chapter`,
},
{
key.MetadataComicInfoXMLAddDate,
true,
`Add series release date to each chapter in ComicInfo.xml file`,
},
{
key.MetadataComicInfoXMLAlternativeDate,
false,
"Use download date instead of series release date in ComicInfo.xml file",
},
{
key.MetadataComicInfoXMLTagRelevanceThreshold,
60,
"Minimum relevance of a tag to be added to ComicInfo.xml file. From 0 to 100",
},
{
key.MetadataSeriesJSON,
true,
`Generate series.json file for each manga`,
},
{
key.MiniSearchLimit,
20,
`Limit of search results to show`,
},
{
key.IconsVariant,
"plain",
`Icons variant.
Available options are: emoji, kaomoji, plain, squares, nerd (nerd-font required)`,
},
{
key.ReaderPDF,
"",
"What app to use to open pdf files",
},
{
key.ReaderCBZ,
"",
"What app to use to open cbz files",
},
{
key.ReaderZIP,
"",
"What app to use to open zip files",
},
{
key.RaderPlain,
"",
"What app to use to open folders",
},
{
key.ReaderBrowser,
"",
"What browser to use to open webpages",
},
{
key.ReaderFolder,
"",
"What app to use to open folders",
},
{
key.ReaderReadInBrowser,
false,
"Open chapter url in browser instead of downloading it",
},
{
key.HistorySaveOnRead,
true,
"Save history on chapter read",
},
{
key.HistorySaveOnDownload,
false,
"Save history on chapter download",
},
{
key.SearchShowQuerySuggestions,
true,
"Show query suggestions in when searching",
},
{
key.MangadexLanguage,
"en",
`Preferred language for mangadex
Use "any" to show all languages`,
},
{
key.MangadexNSFW,
false,
"Show NSFW content",
},
{
key.MangadexShowUnavailableChapters,
false,
"Show chapters that cannot be downloaded",
},
{
key.InstallerUser,
"metafates",
"Custom scrapers repository owner",
},
{
key.InstallerRepo,
"mangal-scrapers",
"Custom scrapers repository name",
},
{
key.InstallerBranch,
"main",
"Custom scrapers repository branch",
},
{
key.GenAuthor,
"",
"Key to use in generated scrapers as author",
},
{
key.LogsWrite,
false,
"Write logs",
},
{
key.LogsLevel,
"info",
`Available options are: (from less to most verbose)
panic, fatal, error, warn, info, debug, trace`,
},
{
key.LogsJson,
false,
"Use json format for logs",
},
{
key.AnilistEnable,
false,
"Enable Anilist integration",
},
{
key.AnilistCode,
"",
"Anilist code to use for authentication",
},
{
key.AnilistID,
"",
"Anilist ID to use for authentication",
},
{
key.AnilistSecret,
"",
"Anilist secret to use for authentication",
},
{
key.AnilistLinkOnMangaSelect,
true,
"Show link to Anilist on manga select",
},
{
key.TUIItemSpacing,
1,
"Spacing between items in the TUI",
},
{
key.TUIReadOnEnter,
true,
"Read chapter on enter if other chapters aren't selected",
},
{
key.TUISearchPromptString,
"> ",
"Search prompt string to use",
},
{
key.TUIShowURLs,
true,
"Show URLs under list items",
},
{
key.TUIReverseChapters,
false,
"Reverse chapters order",
},
{
key.TUIShowDownloadedPath,
true,
"Show path where chapters were downloaded",
},
{
key.CliColored,
true,
"Use colors in CLI help page",
},
{
key.CliVersionCheck,
true,
"Check for a new version of the CLI occasionally",
},
}
func init() {
var count int
for _, field := range defaults {
if _, ok := Default[field.Key]; ok {
panic("Duplicate key in defaults: " + field.Key)
}
Default[field.Key] = field
EnvExposed = append(EnvExposed, field.Key)
count++
}
if count != key.DefinedFieldsCount {
panic(fmt.Sprintf("Expected %d default values, got %d", key.DefinedFieldsCount, count))
}
}
var Default = make(map[string]Field, key.DefinedFieldsCount)