/
templatefuncs.go
743 lines (667 loc) · 16.4 KB
/
templatefuncs.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package cmd
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"slices"
"strconv"
"strings"
"github.com/bradenhilton/mozillainstallhash"
"github.com/itchyny/gojq"
"gopkg.in/ini.v1"
"howett.net/plist"
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
"github.com/twpayne/chezmoi/v2/internal/chezmoilog"
"github.com/twpayne/chezmoi/v2/internal/chezmoimaps"
)
type ioregData struct {
value map[string]any
}
// An emptyPathElementError is returned when a path element is empty.
type emptyPathElementError struct {
index int
}
func (e emptyPathElementError) Error() string {
return fmt.Sprintf("empty path element at index %d", e.index)
}
// An invalidPathElementTypeError is returned when an element in a path has an invalid type.
type invalidPathElementTypeError struct {
element any
}
func (e invalidPathElementTypeError) Error() string {
return fmt.Sprintf("%v: invalid path element type %T", e.element, e.element)
}
// An invalidPathTypeError is returned when a path has an invalid type.
type invalidPathTypeError struct {
path any
}
func (e invalidPathTypeError) Error() string {
return fmt.Sprintf("%v: invalid path type %T", e.path, e.path)
}
// errEmptyPath is returned when a path is empty.
var errEmptyPath = errors.New("empty path")
// needsQuoteRx matches any string that contains non-printable characters,
// double quotes, or a backslash.
var needsQuoteRx = regexp.MustCompile(`[^\x21\x23-\x5b\x5d-\x7e]`)
func (c *Config) commentTemplateFunc(prefix, s string) string {
type stateType int
const (
startOfLine stateType = iota
inLine
)
state := startOfLine
var builder strings.Builder
for _, r := range s {
switch state {
case startOfLine:
if _, err := builder.WriteString(prefix); err != nil {
panic(err)
}
if _, err := builder.WriteRune(r); err != nil {
panic(err)
}
if r != '\n' {
state = inLine
}
case inLine:
if _, err := builder.WriteRune(r); err != nil {
panic(err)
}
if r == '\n' {
state = startOfLine
}
}
}
return builder.String()
}
func (c *Config) deleteValueAtPathTemplateFunc(path string, dict map[string]any) any {
keys, lastKey, err := keysFromPath(path)
if err != nil {
panic(err)
}
currentMap := dict
for _, key := range keys {
value, ok := currentMap[key]
if !ok {
return dict
}
nestedMap, ok := value.(map[string]any)
if !ok {
return dict
}
currentMap = nestedMap
}
delete(currentMap, lastKey)
return dict
}
func (c *Config) eqFoldTemplateFunc(first, second string, more ...string) bool {
if strings.EqualFold(first, second) {
return true
}
for _, s := range more {
if strings.EqualFold(first, s) {
return true
}
}
return false
}
func (c *Config) findExecutableTemplateFunc(file string, pathList any) string {
files := []string{file}
paths, err := anyToStringSlice(pathList)
if err != nil {
panic(fmt.Errorf("path list: %w", err))
}
switch path, err := chezmoi.FindExecutable(files, paths); {
case err == nil:
return path
default:
panic(err)
}
}
func (c *Config) findOneExecutableTemplateFunc(fileList, pathList any) string {
files, err := anyToStringSlice(fileList)
if err != nil {
panic(fmt.Errorf("file list: %w", err))
}
paths, err := anyToStringSlice(pathList)
if err != nil {
panic(fmt.Errorf("path list: %w", err))
}
switch path, err := chezmoi.FindExecutable(files, paths); {
case err == nil:
return path
default:
panic(err)
}
}
func (c *Config) fromIniTemplateFunc(s string) map[string]any {
file, err := ini.Load([]byte(s))
if err != nil {
panic(err)
}
return iniFileToMap(file)
}
// fromJsonTemplateFunc parses s as JSON and returns the result. In contrast to
// encoding/json, numbers are represented as int64s or float64s if possible.
//
//nolint:revive,stylecheck
func (c *Config) fromJsonTemplateFunc(s string) any {
var value any
if err := chezmoi.FormatJSON.Unmarshal([]byte(s), &value); err != nil {
panic(err)
}
return value
}
// fromJsoncTemplateFunc parses s as JSONC and returns the result. In contrast
// to encoding/json, numbers are represented as int64s or float64s if possible.
func (c *Config) fromJsoncTemplateFunc(s string) any {
var value any
if err := chezmoi.FormatJSONC.Unmarshal([]byte(s), &value); err != nil {
panic(err)
}
return value
}
func (c *Config) fromTomlTemplateFunc(s string) any {
var value map[string]any
if err := chezmoi.FormatTOML.Unmarshal([]byte(s), &value); err != nil {
panic(err)
}
return value
}
func (c *Config) fromYamlTemplateFunc(s string) any {
var value any
if err := chezmoi.FormatYAML.Unmarshal([]byte(s), &value); err != nil {
panic(err)
}
return value
}
func (c *Config) globTemplateFunc(pattern string) []string {
defer func() {
value := recover()
err := os.Chdir(c.commandDirAbsPath.String())
if value != nil {
panic(value)
}
if err != nil {
panic(err)
}
}()
if err := os.Chdir(c.DestDirAbsPath.String()); err != nil {
panic(err)
}
matches, err := chezmoi.Glob(c.fileSystem, filepath.ToSlash(pattern))
if err != nil {
panic(err)
}
return matches
}
func (c *Config) hexDecodeTemplateFunc(s string) string {
result, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return string(result)
}
func (c *Config) hexEncodeTemplateFunc(s string) string {
return hex.EncodeToString([]byte(s))
}
func (c *Config) includeTemplateFunc(filename string) string {
searchDirAbsPaths := []chezmoi.AbsPath{c.SourceDirAbsPath}
contents, err := c.readFile(filename, searchDirAbsPaths)
if err != nil {
panic(err)
}
return string(contents)
}
func (c *Config) includeTemplateTemplateFunc(filename string, args ...any) string {
var data any
switch len(args) {
case 0:
// Do nothing.
case 1:
data = args[0]
default:
panic(fmt.Errorf("expected 0 or 1 arguments, got %d", len(args)))
}
searchDirAbsPaths := []chezmoi.AbsPath{
c.SourceDirAbsPath.JoinString(chezmoi.TemplatesDirName),
c.SourceDirAbsPath,
}
contents, err := c.readFile(filename, searchDirAbsPaths)
if err != nil {
panic(err)
}
templateOptions := chezmoi.TemplateOptions{
Options: slices.Clone(c.Template.Options),
}
tmpl, err := chezmoi.ParseTemplate(filename, contents, c.templateFuncs, templateOptions)
if err != nil {
panic(err)
}
result, err := tmpl.Execute(data)
if err != nil {
panic(err)
}
return string(result)
}
func (c *Config) ioregTemplateFunc() map[string]any {
if runtime.GOOS != "darwin" {
return nil
}
if c.ioregData.value != nil {
return c.ioregData.value
}
command := "ioreg"
args := []string{"-a", "-l"}
cmd := exec.Command(command, args...)
cmd.Stderr = os.Stderr
output, err := chezmoilog.LogCmdOutput(c.logger, cmd)
if err != nil {
panic(newCmdOutputError(cmd, output, err))
}
var value map[string]any
if _, err := plist.Unmarshal(output, &value); err != nil {
panic(newParseCmdOutputError(command, args, output, err))
}
c.ioregData.value = value
return value
}
func (c *Config) joinPathTemplateFunc(elem ...string) string {
return filepath.Join(elem...)
}
func (c *Config) jqTemplateFunc(source string, input any) any {
query, err := gojq.Parse(source)
if err != nil {
panic(err)
}
code, err := gojq.Compile(query)
if err != nil {
panic(err)
}
iter := code.Run(input)
var result []any
for {
value, ok := iter.Next()
if !ok {
break
}
if err, ok := value.(error); ok {
panic(err)
}
result = append(result, value)
}
return result
}
func (c *Config) lookPathTemplateFunc(file string) string {
switch path, err := chezmoi.LookPath(file); {
case err == nil:
return path
case errors.Is(err, exec.ErrNotFound):
return ""
case errors.Is(err, fs.ErrNotExist):
return ""
default:
panic(err)
}
}
func (c *Config) isExecutableTemplateFunc(file string) bool {
switch fileInfo, err := c.fileSystem.Stat(file); {
case err == nil:
return chezmoi.IsExecutable(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return false
default:
panic(err)
}
}
func (c *Config) lstatTemplateFunc(name string) any {
switch fileInfo, err := c.fileSystem.Lstat(name); {
case err == nil:
return fileInfoToMap(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return nil
default:
panic(err)
}
}
func (c *Config) mozillaInstallHashTemplateFunc(path string) string {
mozillaInstallHash, err := mozillainstallhash.MozillaInstallHash(path)
if err != nil {
panic(err)
}
return mozillaInstallHash
}
func (c *Config) outputTemplateFunc(name string, args ...string) string {
cmd := exec.Command(name, args...)
cmd.Stderr = os.Stderr
output, err := chezmoilog.LogCmdOutput(c.logger, cmd)
if err != nil {
panic(newCmdOutputError(cmd, output, err))
}
// FIXME we should be able to return output directly, but
// github.com/Masterminds/sprig's trim function only accepts strings
return string(output)
}
func (c *Config) pruneEmptyDictsTemplateFunc(dict map[string]any) map[string]any {
pruneEmptyMaps(dict)
return dict
}
func (c *Config) quoteListTemplateFunc(list []any) []string {
result := make([]string, 0, len(list))
for _, elem := range list {
var elemStr string
switch elem := elem.(type) {
case []byte:
elemStr = string(elem)
case string:
elemStr = elem
case error:
elemStr = elem.Error()
case fmt.Stringer:
elemStr = elem.String()
default:
elemStr = fmt.Sprintf("%v", elem)
}
result = append(result, strconv.Quote(elemStr))
}
return result
}
func (c *Config) readFile(filename string, searchDirAbsPaths []chezmoi.AbsPath) ([]byte, error) {
if filepath.IsAbs(filename) {
absPath, err := chezmoi.NewAbsPathFromExtPath(filename, c.homeDirAbsPath)
if err != nil {
return nil, err
}
return c.fileSystem.ReadFile(absPath.String())
}
var data []byte
var err error
for _, searchDir := range searchDirAbsPaths {
data, err = c.fileSystem.ReadFile(searchDir.JoinString(filename).String())
if !errors.Is(err, fs.ErrNotExist) {
return data, err
}
}
return data, err
}
func (c *Config) replaceAllRegexTemplateFunc(expr, repl, s string) string {
return regexp.MustCompile(expr).ReplaceAllString(s, repl)
}
func (c *Config) setValueAtPathTemplateFunc(path, value, dict any) any {
keys, lastKey, err := keysFromPath(path)
if err != nil {
panic(err)
}
result, ok := dict.(map[string]any)
if !ok {
result = make(map[string]any)
}
currentMap := result
for _, key := range keys {
if value, ok := currentMap[key]; ok {
if nestedMap, ok := value.(map[string]any); ok {
currentMap = nestedMap
} else {
nestedMap := make(map[string]any)
currentMap[key] = nestedMap
currentMap = nestedMap
}
} else {
nestedMap := make(map[string]any)
currentMap[key] = nestedMap
currentMap = nestedMap
}
}
currentMap[lastKey] = value
return result
}
func (c *Config) splitListTemplateFunc(sep, s string) []any {
strSlice := strings.Split(s, sep)
result := make([]interface{}, len(strSlice))
for i, v := range strSlice {
result[i] = v
}
return result
}
func (c *Config) statTemplateFunc(name string) any {
switch fileInfo, err := c.fileSystem.Stat(name); {
case err == nil:
return fileInfoToMap(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return nil
default:
panic(err)
}
}
func (c *Config) toIniTemplateFunc(data map[string]any) string {
var builder strings.Builder
if err := writeIniMap(&builder, data, ""); err != nil {
panic(err)
}
return builder.String()
}
func (c *Config) toPrettyJsonTemplateFunc(args ...any) string { //nolint:revive,stylecheck
var (
indent = " "
value any
)
switch len(args) {
case 1:
value = args[0]
case 2:
var ok bool
indent, ok = args[0].(string)
if !ok {
panic(fmt.Errorf("arg 1: expected a string, got a %T", args[0]))
}
value = args[1]
default:
panic(fmt.Errorf("expected 1 or 2 arguments, got %d", len(args)))
}
var builder strings.Builder
encoder := json.NewEncoder(&builder)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", indent)
if err := encoder.Encode(value); err != nil {
panic(err)
}
return builder.String()
}
func (c *Config) toTomlTemplateFunc(data any) string {
toml, err := chezmoi.FormatTOML.Marshal(data)
if err != nil {
panic(err)
}
return string(toml)
}
func (c *Config) toYamlTemplateFunc(data any) string {
yaml, err := chezmoi.FormatYAML.Marshal(data)
if err != nil {
panic(err)
}
return string(yaml)
}
func fileInfoToMap(fileInfo fs.FileInfo) map[string]any {
return map[string]any{
"name": fileInfo.Name(),
"size": fileInfo.Size(),
"mode": int(fileInfo.Mode()),
"perm": int(fileInfo.Mode().Perm()),
"modTime": fileInfo.ModTime().Unix(),
"isDir": fileInfo.IsDir(),
"type": chezmoi.FileModeTypeNames[fileInfo.Mode()&fs.ModeType],
}
}
func iniFileToMap(file *ini.File) map[string]any {
m := make(map[string]any)
for _, section := range file.Sections() {
if section.Name() == ini.DefaultSection {
for _, k := range section.Keys() {
m[k.Name()] = k.Value()
}
} else {
m[section.Name()] = iniSectionToMap(section)
}
}
return m
}
func iniSectionToMap(section *ini.Section) map[string]any {
m := make(map[string]any)
for _, s := range section.ChildSections() {
m[s.Name()] = iniSectionToMap(s)
}
for _, k := range section.Keys() {
m[k.Name()] = k.Value()
}
return m
}
func keysFromPath(path any) ([]string, string, error) {
switch path := path.(type) {
case string:
if path == "" {
return nil, "", errEmptyPath
}
keys := strings.Split(path, ".")
for i, key := range keys {
if key == "" {
return nil, "", emptyPathElementError{
index: i,
}
}
}
return keys[:len(keys)-1], keys[len(keys)-1], nil
case []any:
if len(path) == 0 {
return nil, "", errEmptyPath
}
keys := make([]string, 0, len(path))
for i, pathElement := range path {
switch pathElementStr, ok := pathElement.(string); {
case !ok:
return nil, "", invalidPathElementTypeError{
element: pathElement,
}
case pathElementStr == "":
return nil, "", emptyPathElementError{
index: i,
}
default:
keys = append(keys, pathElementStr)
}
}
return keys[:len(keys)-1], keys[len(keys)-1], nil
case []string:
if len(path) == 0 {
return nil, "", errEmptyPath
}
for i, key := range path {
if key == "" {
return nil, "", emptyPathElementError{
index: i,
}
}
}
return path[:len(path)-1], path[len(path)-1], nil
case nil:
return nil, "", errEmptyPath
default:
return nil, "", invalidPathTypeError{
path: path,
}
}
}
func nestedMapAtPath(m map[string]any, path any) (map[string]any, string, error) {
keys, lastKey, err := keysFromPath(path)
if err != nil {
return nil, "", err
}
for _, key := range keys {
nestedMap, ok := m[key].(map[string]any)
if !ok {
return nil, "", nil
}
m = nestedMap
}
return m, lastKey, nil
}
func writeIniMap(w io.Writer, data map[string]any, sectionPrefix string) error {
// Write keys in order and accumulate subsections.
type subsection struct {
key string
value map[string]any
}
var subsections []subsection
for _, key := range chezmoimaps.SortedKeys(data) {
switch value := data[key].(type) {
case bool:
fmt.Fprintf(w, "%s = %t\n", key, value)
case float32, float64:
fmt.Fprintf(w, "%s = %f\n", key, value)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:
fmt.Fprintf(w, "%s = %d\n", key, value)
case map[string]any:
subsection := subsection{
key: key,
value: value,
}
subsections = append(subsections, subsection)
case string:
fmt.Fprintf(w, "%s = %s\n", key, maybeQuote(value))
default:
return fmt.Errorf("%s%s: %T: unsupported type", sectionPrefix, key, value)
}
}
// Write subsections in order.
for _, subsection := range subsections {
if _, err := fmt.Fprintf(w, "\n[%s%s]\n", sectionPrefix, subsection.key); err != nil {
return err
}
if err := writeIniMap(w, subsection.value, sectionPrefix+subsection.key+"."); err != nil {
return err
}
}
return nil
}
func maybeQuote(s string) string {
if needsQuote(s) {
return strconv.Quote(s)
}
return s
}
func needsQuote(s string) bool {
if s == "" {
return true
}
if needsQuoteRx.MatchString(s) {
return true
}
if _, err := strconv.ParseBool(s); err == nil {
return true
}
if _, err := strconv.ParseFloat(s, 64); err == nil {
return true
}
return false
}
// pruneEmptyMaps prunes all empty maps from m and returns if m is now empty
// itself.
func pruneEmptyMaps(m map[string]any) bool {
for key, value := range m {
nestedMap, ok := value.(map[string]any)
if !ok {
continue
}
if pruneEmptyMaps(nestedMap) {
delete(m, key)
}
}
return len(m) == 0
}