/
output.go
63 lines (49 loc) · 1.04 KB
/
output.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
package grst
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/tychoish/grip"
)
func (self *RstBuilder) Write(fn string) error {
lines, err := self.GetLines()
if err != nil {
return err
}
dirName := filepath.Dir(fn)
err = os.MkdirAll(dirName, 0755)
if err == nil {
grip.Noticeln("created directory:", dirName)
} else {
grip.Warning(err.Error())
}
file, err := os.Create(fn)
if err != nil {
return err
}
defer file.Close()
w := bufio.NewWriter(file)
catcher := grip.NewCatcher()
var numBytes int
for _, line := range lines {
nb, err := fmt.Fprintln(w, line)
numBytes += nb
catcher.Add(err)
}
catcher.Add(w.Flush())
if catcher.HasErrors() == false {
grip.Debugf("wrote %d bytes to file '%f'.", numBytes, fn)
}
return catcher.Resolve()
}
func (self *RstBuilder) Print() error {
lines, err := self.GetLines()
fmt.Println(strings.Join(lines, "\n"))
return err
}
func (self *RstBuilder) Resolve() (string, error) {
lines, err := self.GetLines()
return strings.Join(lines, "\n") + "\n", err
}