forked from openware/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
124 lines (110 loc) · 2.71 KB
/
file.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package file
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// Write writes selected data to a file or returns an error if it fails. This
// func also ensures that all files are set to this permission (only rw access
// for the running user and the group the user is a member of)
func Write(file string, data []byte) error {
basePath := filepath.Dir(file)
if !Exists(basePath) {
if err := os.MkdirAll(basePath, 0770); err != nil {
return err
}
}
return ioutil.WriteFile(file, data, 0770)
}
// Writer creates a writer to a file or returns an error if it fails. This
// func also ensures that all files are set to this permission (only rw access
// for the running user and the group the user is a member of)
func Writer(file string) (*os.File, error) {
basePath := filepath.Dir(file)
if !Exists(basePath) {
if err := os.MkdirAll(basePath, 0770); err != nil {
return nil, err
}
}
return os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0770)
}
// Move moves a file from a source path to a destination path
// This must be used across the codebase for compatibility with Docker volumes
// and Golang (fixes Invalid cross-device link when using os.Rename)
func Move(sourcePath, destPath string) error {
sourceAbs, err := filepath.Abs(sourcePath)
if err != nil {
return err
}
destAbs, err := filepath.Abs(destPath)
if err != nil {
return err
}
if sourceAbs == destAbs {
return nil
}
inputFile, err := os.Open(sourcePath)
if err != nil {
return err
}
destDir := filepath.Dir(destPath)
if !Exists(destDir) {
err = os.MkdirAll(destDir, 0770)
if err != nil {
return err
}
}
outputFile, err := os.Create(destPath)
if err != nil {
inputFile.Close()
return err
}
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
outputFile.Close()
if err != nil {
if errRem := os.Remove(destPath); errRem != nil {
return fmt.Errorf(
"unable to os.Remove error: %s after io.Copy error: %s",
errRem,
err,
)
}
return err
}
return os.Remove(sourcePath)
}
// Exists returns whether or not a file or path exists
func Exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}
// WriteAsCSV takes a table of records and writes it as CSV
func WriteAsCSV(filename string, records [][]string) error {
if len(records) == 0 {
return errors.New("no records in matrix")
}
buf := bytes.Buffer{}
w := csv.NewWriter(&buf)
alignment := len(records[0])
for i := range records {
if len(records[i]) != alignment {
return errors.New("incorrect alignment")
}
err := w.Write(records[i])
if err != nil {
return err
}
}
w.Flush()
err := w.Error()
if err != nil {
return err
}
return Write(filename, buf.Bytes())
}