This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
203 lines (176 loc) · 3.82 KB
/
fs.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package bs
import (
"bytes"
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"strings"
)
func Getwd() string {
dir, err := os.Getwd()
if err != nil {
fnErrorHandler(err)
}
return dir
}
func Chdir(dir string) {
Verbosef("Chdir: %s", dir)
if err := os.Chdir(dir); err != nil {
fnErrorHandler(err)
}
}
func MkdirAll(dir string) {
Verbosef("MkdirAll: %s", dir)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
fnErrorHandler(err)
}
}
// TODO: if file doesn't exist, don't err
func Remove(dir string) {
Verbosef("Remove: %s", dir)
if err := os.Remove(dir); err != nil {
fnErrorHandler(err)
}
}
func RemoveAll(dir string) {
Verbosef("RemoveAll: %s", dir)
if err := os.RemoveAll(dir); err != nil {
fnErrorHandler(err)
}
}
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
fnErrorHandler(err)
}
return false
}
return true
}
func IsFile(path string) bool {
fi, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
fnErrorHandler(err)
}
return false
}
return !fi.IsDir()
}
func IsDir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
if !os.IsNotExist(err) {
fnErrorHandler(err)
}
return false
}
return fi.IsDir()
}
func Stat(path string) fs.FileInfo {
Verbosef("Stat: %s", path)
fi, err := os.Stat(path)
if err != nil {
fnErrorHandler(err)
}
return fi
}
// Write file (create or truncate)
func Write(path string, contents string) {
if err := writeImpl(path, contents, nil, false); err != nil {
fnErrorHandler(err)
}
}
func Writef(path string, format string, args ...interface{}) {
if err := writeImpl(path, fmt.Sprintf(format, args...), nil, false); err != nil {
fnErrorHandler(err)
}
}
func WriteErr(path string, contents string) error {
return writeImpl(path, contents, nil, false)
}
func WriteBytes(path string, b []byte) {
if err := writeImpl(path, "", b, false); err != nil {
fnErrorHandler(err)
}
}
func WriteBytesErr(path string, b []byte) error {
return writeImpl(path, "", b, false)
}
// Append file
func Append(path string, contents string) {
if err := writeImpl(path, contents, nil, true); err != nil {
fnErrorHandler(err)
}
}
func Appendf(path string, format string, args ...interface{}) {
if err := writeImpl(path, fmt.Sprintf(format, args...), nil, true); err != nil {
fnErrorHandler(err)
}
}
func AppendErr(path string, contents string) error {
return writeImpl(path, contents, nil, true)
}
func AppendBytes(path string, b []byte) {
if err := writeImpl(path, "", b, true); err != nil {
fnErrorHandler(err)
}
}
func AppendBytesErr(path string, b []byte) error {
return writeImpl(path, "", b, true)
}
func writeImpl(path string, str string, b []byte, append bool) error {
if len(str) > 0 && len(b) > 0 {
return fmt.Errorf("this should never happen: writeImpl has both string and []byte")
}
var f *os.File
var err error
if append {
Verbosef("Append to file: %s", path)
f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
} else {
Verbosef("Write to file: %s", path)
f, err = os.Create(path)
}
if err != nil {
return err
}
defer f.Close()
if len(str) > 0 {
_, err = io.Copy(f, strings.NewReader(str))
} else {
_, err = io.Copy(f, bytes.NewReader(b))
}
if err != nil {
return err
}
return nil
}
// Read file
func Read(path string) string {
str, err := ReadErr(path)
if err != nil {
fnErrorHandler(err)
}
return str
}
func ReadErr(path string) (string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
// TODO: this cast from []byte to string involves an allocation and copy.
// Is there a way to skip that work and read straight into a string?
return string(b), nil
}
func ReadFile(path string) []byte {
Verbosef("Read from file: %s", path)
b, err := ioutil.ReadFile(path)
if err != nil {
fnErrorHandler(err)
}
return b
}