generated from parrogo/gomod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
102 lines (90 loc) · 2.55 KB
/
buffer.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
package lines
import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
)
// Buffer contains a bytes.Buffer
// and simplify writing lines of text to it.
//
// The empty value of a Buffer is already
// usable.
//
// You can recycle Buffer multiple times,
// any call to WriteFile will truncate the
// underlying bytes.Buffer to 0.
//
// If you want to start from scratch without
// having to write to a file, you can call
// Discard method.
type Buffer struct {
buf bytes.Buffer
}
// AddLineF writes a line of text in the buffer.
// The line to append is built calling `fmt.Sprintf`
// with `lineFormat` and `arguments...`
func (lines *Buffer) AddLineF(lineFormat string, arguments ...interface{}) {
line := fmt.Sprintf(lineFormat, arguments...)
lines.AddLine(line)
}
// AddLine writes a line of text in the buffer.
// A newline is appended at the end of the strigns
// only if it's not already present.
func (lines *Buffer) AddLine(line string) {
lines.buf.WriteString(line)
// # TODO: check if newline is already present.
lines.buf.WriteRune('\n')
}
// WriteFile writes all lines written to
// the buffer so far to a file with given path.
//
// After the file is written successfully, WriteFile
// truncates the internal lines buffer, so that you can recycle
// the same Buffer as if it was newly created.
//
// If an error occurrs during the writing of the file,
// no truncation of the buffer is done, so that you can
// retry the method call later.
func (lines *Buffer) WriteFile(filepath string) error {
// # TODO: add a twin method that uses fs.FS
f, err := os.OpenFile(filepath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fs.FileMode(0644))
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(lines.buf.Bytes())
if err != nil {
return err
}
lines.buf.Truncate(0)
return err
}
// Write writes all lines written to
// the buffer so far in a io.Writer.
//
// If if the write operation succeed, Write
// truncates the internal lines buffer, so that
// you can recycle the same Buffer as if it
// was newly created.
//
// If an error occurrs during the writing,
// no truncation of the buffer is done, so that you can
// retry the method call later.
func (lines *Buffer) Write(dest io.Writer) error {
_, err := dest.Write(lines.buf.Bytes())
if err != nil {
return err
}
lines.buf.Truncate(0)
return err
}
// Reset truncates the internal lines buffer, so that
// you can recycle the same Buffer as if it
// was newly created. Current content of the
// buffer is returned as a `[]byte`
func (lines *Buffer) Reset() []byte {
defer lines.buf.Truncate(0)
return lines.buf.Bytes()
}