-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
utils.go
61 lines (53 loc) · 964 Bytes
/
utils.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"text/tabwriter"
)
type Writer interface {
Write([]string) error
Flush()
}
type TSVWriter struct {
w *tabwriter.Writer
}
func NewTSVWriter(w io.Writer) *TSVWriter {
return &TSVWriter{
w: tabwriter.NewWriter(w, 0, 4, 1, ' ', 0),
}
}
func (w *TSVWriter) Flush() {
w.w.Flush()
}
func (w *TSVWriter) Write(record []string) error {
string := strings.Join(record[:], "\t")
fmt.Fprintln(w.w, string)
return nil
}
func Exists(path string) (bool, error) {
_, fileErr := os.Stat(path)
if fileErr == nil {
return true, nil
}
if os.IsNotExist(fileErr) {
return false, nil
}
return true, nil
}
func AssureExists(filePath string) error {
path := filepath.Dir(filePath)
exists, err := Exists(path)
if err != nil {
return err
}
if !exists {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return fmt.Errorf("Couldn't create path: %s", path)
}
}
return nil
}