-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
55 lines (43 loc) · 1.15 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
package diplomat
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
const (
DefaultDirectoryPerm = 0755
DefaultFilePerm = 0644
)
type Output interface {
WriteFile(filename string, data []byte) error
}
type OutputDirectory struct {
directory string
}
func NewOutputDirectory(root string) *OutputDirectory {
return &OutputDirectory{root}
}
func (o OutputDirectory) WriteFile(filename string, data []byte) error {
actualPath := o.absPath(filename)
if err := o.ensureDirExistsForPath(actualPath); err != nil {
return err
}
return ioutil.WriteFile(actualPath, data, DefaultFilePerm)
}
func (o OutputDirectory) ensureDirExists(dirPath string) error {
return os.MkdirAll(dirPath, DefaultDirectoryPerm)
}
func (o OutputDirectory) ensureDirExistsForPath(filePath string) error {
return o.ensureDirExists(filepath.Dir(filePath))
}
func (o OutputDirectory) absPath(relative string) string {
return filepath.Join(o.directory, relative)
}
type ConsoleOutput struct {
}
const fileDelimiter = "\n-----\n"
func (c ConsoleOutput) WriteFile(filename string, data []byte) error {
fmt.Printf("%s%s%s%s", fileDelimiter, filename, fileDelimiter, string(data))
return nil
}