-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrite.go
64 lines (56 loc) · 1.31 KB
/
write.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
package testmark
import (
"io"
"os"
"strings"
)
func (d Document) String() string {
var sb strings.Builder
Write(&d, &sb)
return sb.String()
}
func Write(doc *Document, wr io.Writer) (int, error) {
n := 0
for _, line := range doc.Lines {
if n2, err := wr.Write(line); err != nil {
return n + n2, err
} else {
n += n2
}
if n2, err := wr.Write(sigilLineBreak); err != nil {
return n + n2, err
} else {
n += n2
}
}
return n, nil
}
func WriteFile(doc *Document, filename string) error {
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = Write(doc, f)
return err
}
func WriteWithPatches(doc *Document, wr io.Writer, patches ...Hunk) (int, error) {
if len(patches) == 0 {
return 0, nil
}
doc = Patch(doc, patches...)
return Write(doc, wr)
}
func WriteFileWithPatches(doc *Document, filename string, patches ...Hunk) error {
if len(patches) == 0 {
return nil
}
doc = Patch(doc, patches...)
return WriteFile(doc, filename)
}
func (pa PatchAccumulator) WriteWithPatches(doc *Document, wr io.Writer) (int, error) {
return WriteWithPatches(doc, wr, pa.Patches...)
}
func (pa PatchAccumulator) WriteFileWithPatches(doc *Document, filename string) error {
return WriteFileWithPatches(doc, filename, pa.Patches...)
}