Repository navigation
Expand file tree
/
Copy pathfile.go
More file actions
832 lines (763 loc) · 24.6 KB
/
Copy pathfile.go
File metadata and controls
832 lines (763 loc) · 24.6 KB
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
package ini
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
"sync"
"unicode/utf8"
)
// File represents a combination of one or more INI files in memory.
type File struct {
options loadOptions
dataSources []dataSource
// locking guards the contents with mu, unless loaded with Locking(false).
locking bool
mu sync.RWMutex
// sections are in the order they were added, and byName indexes
// them, in the same order, by name.
sections []*Section
byName map[string][]*Section
}
// newFile initializes File object with given data sources.
func newFile(dataSources []dataSource, opts []LoadOption) *File {
o := newLoadOptions(opts)
return &File{
locking: o.Locking,
dataSources: dataSources,
byName: make(map[string][]*Section),
options: o,
}
}
// Empty returns an empty file object.
func Empty(opts ...LoadOption) *File {
// Ignore error here, we are sure our data is good.
f, _ := Load([]byte(""), opts...)
return f
}
// isDefaultSection reports whether name, as stored, is the default
// section's name. Loading with CaseInsensitive or CaseInsensitiveSections
// stores it lower-cased.
func (f *File) isDefaultSection(name string) bool {
return name == f.options.DefaultSection || name == f.defaultSectionName()
}
// defaultSectionName returns the name the default section is stored under.
func (f *File) defaultSectionName() string {
if f.options.Insensitive || f.options.InsensitiveSections {
return strings.ToLower(f.options.DefaultSection)
}
return f.options.DefaultSection
}
// maybeLock, maybeUnlock, maybeRLock and maybeRUnlock call Lock, Unlock,
// RLock and RUnlock of mu when locking is set, and do nothing otherwise.
// They guard the file's sections, keys and values. Unexported methods that
// change them expect the caller to hold the lock, or the file not to be
// shared yet, as while parsing.
func (f *File) maybeRLock() {
if f.locking {
f.mu.RLock()
}
}
func (f *File) maybeRUnlock() {
if f.locking {
f.mu.RUnlock()
}
}
func (f *File) maybeLock() {
if f.locking {
f.mu.Lock()
}
}
func (f *File) maybeUnlock() {
if f.locking {
f.mu.Unlock()
}
}
// NewSection creates a new section.
func (f *File) NewSection(name string) (*Section, error) {
f.maybeLock()
defer f.maybeUnlock()
return f.addSection(name)
}
// addSection is NewSection for a caller that holds the lock.
func (f *File) addSection(name string) (*Section, error) {
if len(name) == 0 {
return nil, errors.New("empty section name")
}
if (f.options.Insensitive || f.options.InsensitiveSections) && name != f.options.DefaultSection {
name = strings.ToLower(name)
}
if secs := f.byName[name]; len(secs) > 0 && !f.options.AllowNonUniqueSections {
return secs[0], nil
}
sec := newSection(f, name)
f.sections = append(f.sections, sec)
f.byName[name] = append(f.byName[name], sec)
return sec, nil
}
// NewRawSection creates a new section with an unparseable body.
func (f *File) NewRawSection(name, body string) (*Section, error) {
f.maybeLock()
defer f.maybeUnlock()
section, err := f.addSection(name)
if err != nil {
return nil, err
}
section.isRawSection = true
section.rawBody = body
return section, nil
}
// NewSections creates a list of sections.
func (f *File) NewSections(names ...string) (err error) {
for _, name := range names {
if _, err = f.NewSection(name); err != nil {
return err
}
}
return nil
}
// GetSection returns section by given name.
func (f *File) GetSection(name string) (*Section, error) {
return f.sectionWithIndex(name, 0)
}
// sectionWithIndex returns the index-th section with the given name.
func (f *File) sectionWithIndex(name string, index int) (*Section, error) {
f.maybeRLock()
defer f.maybeRUnlock()
secs, err := f.sectionsByName(name)
if err != nil {
return nil, err
}
if index < 0 || index >= len(secs) {
return nil, fmt.Errorf("section %q has no index %d", name, index)
}
return secs[index], nil
}
// HasSection returns true if the file contains a section with given name.
func (f *File) HasSection(name string) bool {
section, _ := f.GetSection(name)
return section != nil
}
// SectionsByName returns all sections with given name.
func (f *File) SectionsByName(name string) ([]*Section, error) {
f.maybeRLock()
defer f.maybeRUnlock()
secs, err := f.sectionsByName(name)
// A copy, as deleting a section shifts the ones after it in place.
return slices.Clone(secs), err
}
// sectionsByName is SectionsByName for a caller that holds the lock.
func (f *File) sectionsByName(name string) ([]*Section, error) {
if len(name) == 0 {
name = f.options.DefaultSection
}
if f.options.Insensitive || f.options.InsensitiveSections {
name = strings.ToLower(name)
}
secs := f.byName[name]
if len(secs) == 0 {
return nil, fmt.Errorf("section %q does not exist", name)
}
return secs, nil
}
// Section assumes named section exists and returns a zero-value when not.
func (f *File) Section(name string) *Section {
sec, err := f.GetSection(name)
if err != nil {
if name == "" {
// The name the lookup used, so the next lookup finds it.
name = f.defaultSectionName()
}
sec, _ = f.NewSection(name)
return sec
}
return sec
}
// SectionWithIndex assumes named section exists and returns a new section when not.
func (f *File) SectionWithIndex(name string, index int) *Section {
if name == "" {
name = f.defaultSectionName()
}
sec, err := f.sectionWithIndex(name, index)
if err != nil {
newSec, _ := f.NewSection(name)
return newSec
}
return sec
}
// Sections returns a list of Section stored in the current instance.
func (f *File) Sections() []*Section {
f.maybeRLock()
defer f.maybeRUnlock()
return slices.Clone(f.sections)
}
// ChildSections returns a list of child sections of given section name.
func (f *File) ChildSections(name string) []*Section {
return f.Section(name).ChildSections()
}
// SectionNames returns list of section names.
func (f *File) SectionNames() []string {
f.maybeRLock()
defer f.maybeRUnlock()
names := make([]string, len(f.sections))
for i, sec := range f.sections {
names[i] = sec.name
}
return names
}
// DeleteSection deletes a section or all sections with given name.
func (f *File) DeleteSection(name string) {
secs, err := f.SectionsByName(name)
if err != nil {
return
}
for i := 0; i < len(secs); i++ {
// For non-unique sections, it is always needed to remove the first one so
// in the next iteration, the subsequent section continue having index 0.
// Ignoring the error as index 0 never returns an error.
_ = f.DeleteSectionWithIndex(name, 0)
}
}
// DeleteSectionWithIndex deletes a section with given name and index.
func (f *File) DeleteSectionWithIndex(name string, index int) error {
if index < 0 {
return fmt.Errorf("delete section with negative index %d", index)
}
if !f.options.AllowNonUniqueSections && index != 0 {
return fmt.Errorf("delete section with non-zero index is only allowed when non-unique sections is enabled")
}
if len(name) == 0 {
name = f.options.DefaultSection
}
if f.options.Insensitive || f.options.InsensitiveSections {
name = strings.ToLower(name)
}
f.maybeLock()
defer f.maybeUnlock()
secs := f.byName[name]
if index >= len(secs) {
return nil
}
sec := secs[index]
if len(secs) == 1 {
delete(f.byName, name)
} else {
f.byName[name] = slices.Delete(secs, index, index+1)
}
f.sections = slices.DeleteFunc(f.sections, func(s *Section) bool { return s == sec })
return nil
}
// loadSource parses one data source into f.
func (f *File) loadSource(s dataSource) error {
r, err := s.ReadCloser()
if err != nil {
return err
}
defer r.Close()
return f.parse(r)
}
// Reload reads all data sources again and replaces the contents of the file
// with them: keys and sections no longer in the sources are gone, and
// shadows don't pile up. Sections and keys that are still there keep their
// *Section and *Key values, so pointers to them see the new contents. On
// error the file is unchanged.
func (f *File) Reload() error {
f.maybeRLock()
sources := f.dataSources
f.maybeRUnlock()
next := &File{options: f.options, byName: make(map[string][]*Section)}
if err := next.load(sources); err != nil {
return err
}
f.maybeLock()
defer f.maybeUnlock()
for name, secs := range next.byName {
old := f.byName[name]
for i, sec := range secs {
if i >= len(old) {
sec.f = f
continue
}
// Keep the section and keys callers may hold, with the new
// contents. Their s and f stay, as readers use them to find
// the lock.
o := old[i]
for j, k := range sec.keys {
if ok := o.byName[k.name]; ok != nil {
ok.comment, ok.value, ok.shadows = k.comment, k.value, k.shadows
ok.isAutoIncrement, ok.isBooleanType = k.isAutoIncrement, k.isBooleanType
ok.nestedValues = k.nestedValues
sec.keys[j], sec.byName[k.name] = ok, ok
} else {
k.s = o
}
}
o.comment, o.keys, o.byName = sec.comment, sec.keys, sec.byName
o.isRawSection, o.rawBody = sec.isRawSection, sec.rawBody
secs[i] = o
}
}
// The n-th section of a name in next.sections is the n-th in
// next.byName, which now holds the sections kept.
seen := make(map[string]int)
for i, sec := range next.sections {
next.sections[i] = next.byName[sec.name][seen[sec.name]]
seen[sec.name]++
}
f.sections, f.byName = next.sections, next.byName
return nil
}
// load parses sources into f, in order.
func (f *File) load(sources []dataSource) error {
for _, s := range sources {
if err := f.loadSource(s); err != nil {
// In loose mode, we create an empty default section for nonexistent files.
if os.IsNotExist(err) && f.options.Loose {
_ = f.parse(bytes.NewBuffer(nil))
continue
}
return err
}
if f.options.ShortCircuit {
return nil
}
}
return nil
}
// Append adds one or more data sources and reads them into the file. Their
// sections and keys add to the current ones and overwrite keys they repeat,
// or add shadow values to them with AllowShadows. Changes made to the file
// in memory stay. With FirstSourceOnly, Append calls Reload instead, so the
// first source that loads wins and changes made in memory are lost.
func (f *File) Append(source any, others ...any) error {
sources := make([]dataSource, 0, 1+len(others))
for _, s := range append([]any{source}, others...) {
ds, err := newDataSource(s)
if err != nil {
return err
}
sources = append(sources, ds)
}
f.maybeLock()
f.dataSources = append(f.dataSources, sources...)
if f.options.ShortCircuit {
f.maybeUnlock()
// Only the first source that loads counts.
return f.Reload()
}
defer f.maybeUnlock()
return f.load(sources)
}
func (f *File) writeToBuffer(opts []WriteOption) (*bytes.Buffer, error) {
f.maybeRLock()
defer f.maybeRUnlock()
o := newWriteOptions(opts)
indent, lineBreak := o.indent, o.lineBreak
equalSign := o.formatLeft + o.keyValueDelimiter + o.formatRight
if o.alignDelimiters {
equalSign = fmt.Sprintf(" %s ", o.keyValueDelimiter)
}
// Comment lines that don't start with a comment symbol get one: ";" when
// it is a comment symbol, otherwise the first one. Without comment
// symbols, a comment can't be written so that it reads back.
symbols := f.options.CommentSymbols
commentPrefix := ";"
if !strings.Contains(symbols, ";") && symbols != "" {
r, _ := utf8.DecodeRuneInString(symbols)
commentPrefix = string(r)
}
// formatComment puts the comment symbol of line, or commentPrefix, and a
// space before its text.
formatComment := func(line string, trim bool) (string, error) {
if symbols == "" {
return "", errors.New("cannot write comment: no comment symbols set")
}
if trim {
line = strings.TrimSpace(line)
}
if !startsWithAny(line, symbols) {
return commentPrefix + " " + line, nil
}
_, n := utf8.DecodeRuneInString(line)
return line[:n] + " " + strings.TrimSpace(line[n:]), nil
}
// keyLine returns the line that writes val as a value of key, written as
// form, or key itself for a boolean key, followed by nested values.
// The usual quoting comes first. When the lexer reads that back
// differently, other quotings and a bare delimiter are tried, and when
// none reads back, it's an error rather than a file that loads
// differently. A file read with delimiters that don't include the
// written one is meant for other readers, and is written unchecked.
verify := strings.ContainsAny(o.keyValueDelimiter, f.options.KeyValueDelimiters)
keyLine := func(key *Key, boolean bool, name, form, eq, val string, nested []string) (string, error) {
var after string
for _, nv := range nested {
after += indent + " " + nv + lineBreak
}
if !boolean && name == "-" && !key.isAutoIncrement {
// Any key named "-" reads back as an auto-increment key.
return "", fmt.Errorf("cannot write key %q in section %q: it would read back as an auto-increment key", key.name, key.s.name)
}
var line string
switch {
case boolean:
line = form + lineBreak
if !f.options.AllowBooleanKeys {
return line, nil
}
case strings.ContainsAny(val, "\n`"):
line = form + eq + `"""` + val + `"""` + lineBreak
case !f.options.IgnoreInlineComment && strings.ContainsAny(val, symbols):
line = form + eq + "`" + val + "`" + lineBreak
case len(strings.TrimSpace(val)) != len(val):
line = form + eq + `"` + val + `"` + lineBreak
default:
line = form + eq + val + lineBreak
}
if !verify || f.readsBack(line+after, boolean, name, val, nested) {
return line + after, nil
}
quotings := func(s string) []string {
q := []string{s, "`" + s + "`", `"""` + s + `"""`, `"` + s + `"`}
if f.options.UnescapeValueDoubleQuotes {
q = append(q, `"`+strings.ReplaceAll(s, `"`, `\"`)+`"`)
}
return q
}
forms := []string{"-"}
if !key.isAutoIncrement {
forms = quotings(name)
}
if boolean {
for _, k := range forms {
if line = k + lineBreak; f.readsBack(line, boolean, name, "", nil) {
return line, nil
}
}
} else {
for _, e := range []string{eq, o.keyValueDelimiter} {
for _, k := range forms {
for _, v := range quotings(val) {
if line = k + e + v + lineBreak + after; f.readsBack(line, boolean, name, val, nested) {
return line, nil
}
}
}
}
}
return "", fmt.Errorf("cannot write key %q with value %q in section %q so that it reads back", key.name, val, key.s.name)
}
// Use buffer to make sure target is safe until finish encoding.
buf := bytes.NewBuffer(nil)
// Per-key buffers, reused for every key.
var vals, lines []string
var empty []bool
lastSectionIdx := len(f.sections) - 1
for i, sec := range f.sections {
sname := sec.name
if strings.Contains(sname, "\n") {
return nil, fmt.Errorf("cannot write section %q: its name has a line break", sname)
}
if len(sec.comment) > 0 {
// Support multiline comments. They are split at "\n" too, so a
// comment set with "\n" and written with another lineBreak has
// no line without a comment symbol.
lines := splitLines(sec.comment, lineBreak)
for i := range lines {
var err error
if lines[i], err = formatComment(lines[i], false); err != nil {
return nil, err
}
if _, err := buf.WriteString(lines[i] + lineBreak); err != nil {
return nil, err
}
}
}
if sec.isRawSection && len(sec.keys) > 0 {
// Only the body of a raw section is written.
return nil, fmt.Errorf("cannot write section %q: it is raw and has keys", sname)
}
// The first section's header is left out when it is the default
// section, unless DefaultHeader is set, it has a comment, which needs
// the header, or it is raw, which only a header makes it. Without
// keys, it is then not written at all. With keys, the header is
// still written when the name upper-cased isn't the configured
// default section name, so a default section renamed in lower case
// keeps its header, as in v1, unless the header would make it raw.
isDefault := i == 0 && f.isDefaultSection(sec.name)
emptyDefault := isDefault && len(sec.keys) == 0
writeHeader := !isDefault || o.defaultHeader || len(sec.comment) > 0 || sec.isRawSection ||
(strings.ToUpper(sec.name) != f.options.DefaultSection && !emptyDefault && !f.readsAsRaw(sname))
if writeHeader {
if _, err := buf.WriteString("[" + sname + "]" + lineBreak); err != nil {
return nil, err
}
} else if len(sec.keys) == 0 {
continue
}
if writeHeader && !sec.isRawSection && len(sec.keys) > 0 && f.readsAsRaw(sname) {
// The keys would read back as the body of a raw section.
return nil, fmt.Errorf("cannot write the keys of section %q: it is read as a raw section", sname)
}
isLastSection := i == lastSectionIdx
if sec.isRawSection {
if !f.rawBodyReadsBack(sname, sec.rawBody) {
return nil, fmt.Errorf("cannot write the body of section %q so that it reads back: a line would start a section or be a comment", sname)
}
if _, err := buf.WriteString(sec.rawBody); err != nil {
return nil, err
}
if !isLastSection && sec.rawBody != "" && !strings.HasSuffix(sec.rawBody, "\n") {
// The next section header starts on its own line.
buf.WriteString(lineBreak)
}
if o.blankLines && !isLastSection {
// Put a line between sections
if _, err := buf.WriteString(lineBreak); err != nil {
return nil, err
}
}
continue
}
// Count and generate alignment length and buffer spaces using the
// longest key. Keys may be modified if they contain certain characters so
// we need to take that into account in our calculation.
alignLength := 0
if o.alignDelimiters {
for _, key := range sec.keys {
kname := key.name
keyLength := len(kname)
// First case will surround key by ` and second by """
if strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters) {
keyLength += 2
} else if strings.Contains(kname, "`") {
keyLength += 6
}
if keyLength > alignLength {
alignLength = keyLength
}
}
}
alignSpaces := bytes.Repeat([]byte(" "), alignLength)
keyIndent := ""
if !f.isDefaultSection(sname) {
keyIndent = indent
}
// swallows is set when the lexer would read an indented line as part
// of the last value: always in Python multiline values, and after an
// empty value with nested values.
swallows := false
writeIndented := func(line string) error {
if keyIndent != "" && swallows {
return fmt.Errorf("cannot indent line %q in section %q: it would be read as part of the value before it", line, sname)
}
buf.WriteString(keyIndent + line)
return nil
}
for _, key := range sec.keys {
kname := key.name
name, form := kname, kname
switch {
case key.isAutoIncrement:
name, form = "-", "-"
case strings.Contains(kname, "\"") || strings.ContainsAny(kname, f.options.KeyValueDelimiters):
form = "`" + kname + "`"
case strings.Contains(kname, "`"):
form = `"""` + kname + `"""`
}
pad := ""
if o.alignDelimiters {
pad = string(alignSpaces[:max(0, alignLength-len(form))])
}
// lines are the lines of the key, each with the nested values
// that follow it, and empty tells whether its value is empty.
lines, empty = lines[:0], empty[:0]
addLine := func(boolean bool, val string, nested []string) error {
eq := pad + equalSign
if boolean {
eq = ""
}
line, err := keyLine(key, boolean, name, form, eq, val, nested)
lines = append(lines, line)
empty = append(empty, !boolean && val == "")
return err
}
// All values are written, empty ones too: an empty value resets
// the values before it in systemd units, and nested values
// follow an empty value.
vals = append(vals[:0], key.value)
vals = append(vals, key.shadows...)
// regular are the values written on key lines. A boolean line
// after a key of the same name makes it a boolean key with the
// value "true" added, and a value set after the boolean line
// replaces "true".
regular, boolFirst := vals, false
if key.isBooleanType {
switch {
case !f.options.AllowBooleanKeys:
regular = nil
case len(key.shadows) > 0:
if vals[len(vals)-1] == "true" {
regular = vals[:len(vals)-1]
}
case key.value != "true":
boolFirst = true
default:
regular = nil
}
}
nested := key.nestedValues
if key.isBooleanType && boolFirst {
if len(nested) > 0 {
// Nested values can't follow a boolean line, so they
// go before it, after an empty value.
if err := addLine(false, "", nested); err != nil {
return nil, err
}
nested = nil
}
if err := addLine(true, "", nil); err != nil {
return nil, err
}
}
if len(nested) > 0 && !slices.Contains(regular, "") {
// Nested values only follow an empty value. Without shadows
// the value after it still wins.
regular = append([]string{""}, regular...)
}
for _, val := range regular {
if val != "" {
if err := addLine(false, val, nil); err != nil {
return nil, err
}
continue
}
if err := addLine(false, val, nested); err != nil {
return nil, err
}
nested = nil
}
if key.isBooleanType && !boolFirst {
if err := addLine(true, "", nil); err != nil {
return nil, err
}
}
// The lexer gives a key the comment before its last line, so a
// key written on several lines has its comment there.
last := len(lines) - 1
for i, line := range lines[:last] {
if err := writeIndented(line); err != nil {
return nil, err
}
swallows = f.options.AllowPythonMultilineValues || (f.options.AllowNestedValues && empty[i])
}
if len(key.comment) > 0 {
if err := writeIndented(""); err != nil {
return nil, err
}
// Support multiline comments
comments := splitLines(key.comment, lineBreak)
for i := range comments {
var err error
if comments[i], err = formatComment(comments[i], true); err != nil {
return nil, err
}
buf.WriteString(comments[i] + lineBreak)
}
}
if err := writeIndented(lines[last]); err != nil {
return nil, err
}
swallows = f.options.AllowPythonMultilineValues || (f.options.AllowNestedValues && empty[last])
}
if o.blankLines && !isLastSection {
// Put a line between sections
if _, err := buf.WriteString(lineBreak); err != nil {
return nil, err
}
}
}
return buf, nil
}
// readsBack reports whether the lexer reads text as one key, or boolean key,
// named name with value val and no comment, followed by nested values.
func (f *File) readsBack(text string, boolean bool, name, val string, nested []string) bool {
// A byte order mark is skipped at the start of a file.
l := lexer{src: skipBOM(text), opts: &f.options}
tok, err := l.next()
if err != nil {
return false
}
kind := tokKey
if boolean {
kind = tokBooleanKey
}
if tok.kind != kind || tok.name != name || tok.value != val || tok.comment != "" {
return false
}
for _, nv := range nested {
if tok, err = l.next(); err != nil || tok.kind != tokNestedValue || tok.value != nv {
return false
}
}
tok, err = l.next()
return err == nil && tok.kind == tokEOF && len(l.comment) == 0
}
// readsAsRaw reports whether the lexer reads the section name, after its
// header, as a raw section.
func (f *File) readsAsRaw(name string) bool {
l := lexer{src: "[" + name + "]\n", opts: &f.options}
tok, err := l.next()
return err == nil && tok.kind == tokSection && l.inRaw
}
// rawBodyReadsBack reports whether the lexer reads body, under the header of
// the raw section name, as that section's body and nothing else. Blank lines
// and the indentation of lines aren't kept, as when loading. A section that
// the file's options don't read as raw is written for other readers, and
// isn't checked.
func (f *File) rawBodyReadsBack(name, body string) bool {
if !f.readsAsRaw(name) {
return true
}
l := lexer{src: "[" + name + "]\n" + body, opts: &f.options}
if _, err := l.next(); err != nil {
return false
}
tok, err := l.next()
if err == nil && tok.kind == tokRawBody {
tok, err = l.next()
}
return err == nil && tok.kind == tokEOF && len(l.comment) == 0
}
// splitLines splits s at lineBreak, and then at "\n", so that no line has
// a "\n" in it.
func splitLines(s, lineBreak string) []string {
var lines []string
for _, line := range strings.Split(s, lineBreak) {
lines = append(lines, strings.Split(line, "\n")...)
}
return lines
}
// Write writes the file to w, formatted by opts.
func (f *File) Write(w io.Writer, opts ...WriteOption) (int64, error) {
buf, err := f.writeToBuffer(opts)
if err != nil {
return 0, err
}
return buf.WriteTo(w)
}
// WriteTo writes the file to w with the default format.
func (f *File) WriteTo(w io.Writer) (int64, error) {
return f.Write(w)
}
// SaveTo writes the file to the named file, formatted by opts.
func (f *File) SaveTo(filename string, opts ...WriteOption) error {
buf, err := f.writeToBuffer(opts)
if err != nil {
return err
}
return os.WriteFile(filename, buf.Bytes(), 0666)
}