Skip to content

Commit 94f352f

Browse files
committed
Update file helpers
1 parent 0ef8ff8 commit 94f352f

File tree

4 files changed

+76
-27
lines changed

4 files changed

+76
-27
lines changed

pkg/lib/files/errors.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type ErrorKind int
2828
const (
2929
ErrUnknown ErrorKind = iota
3030
ErrCreateDir
31+
ErrDeleteDir
3132
ErrReadFormFile
3233
ErrCreateFile
3334
ErrReadDir
@@ -43,6 +44,7 @@ const (
4344
var _errorKinds = []string{
4445
"err_unknown",
4546
"err_create_dir",
47+
"err_delete_dir",
4648
"err_read_form_file",
4749
"err_create_file",
4850
"err_read_dir",
@@ -107,6 +109,13 @@ func ErrorCreateDir(path string) error {
107109
})
108110
}
109111

112+
func ErrorDeleteDir(path string) error {
113+
return errors.WithStack(Error{
114+
Kind: ErrDeleteDir,
115+
message: fmt.Sprintf("%s: unable to delete directory", path),
116+
})
117+
}
118+
110119
func ErrorReadFormFile(fileName string) error {
111120
return errors.WithStack(Error{
112121
Kind: ErrReadFormFile,

pkg/lib/files/files.go

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ func OpenFile(path string, flag int, perm os.FileMode) (*os.File, error) {
6565
return file, err
6666
}
6767

68+
func OpenNewFile(path string) (*os.File, error) {
69+
cleanPath, err := EscapeTilde(path)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
file, err := os.Create(cleanPath)
75+
if err != nil {
76+
return nil, errors.Wrap(err, errors.Message(ErrorCreateFile(path)))
77+
}
78+
79+
return file, nil
80+
}
81+
6882
func ReadFile(path string) (string, error) {
6983
fileBytes, err := ReadFileBytes(path)
7084
if err != nil {
@@ -96,18 +110,19 @@ func ReadFileBytesErrPath(path string, errMsgPath string) ([]byte, error) {
96110
return fileBytes, nil
97111
}
98112

99-
func CreateFile(path string) (*os.File, error) {
113+
func CreateFile(path string) error {
100114
cleanPath, err := EscapeTilde(path)
101115
if err != nil {
102-
return nil, err
116+
return err
103117
}
104118

105119
file, err := os.Create(cleanPath)
106120
if err != nil {
107-
return nil, errors.Wrap(err, errors.Message(ErrorCreateFile(path)))
121+
return errors.Wrap(err, errors.Message(ErrorCreateFile(path)))
108122
}
123+
defer file.Close()
109124

110-
return file, nil
125+
return nil
111126
}
112127

113128
func WriteFile(data []byte, path string) error {
@@ -123,19 +138,6 @@ func WriteFile(data []byte, path string) error {
123138
return nil
124139
}
125140

126-
func MkdirAll(path string) error {
127-
cleanPath, err := EscapeTilde(path)
128-
if err != nil {
129-
return err
130-
}
131-
132-
if err := os.MkdirAll(cleanPath, os.ModePerm); err != nil {
133-
return errors.Wrap(err, errors.Message(ErrorCreateDir(path)))
134-
}
135-
136-
return nil
137-
}
138-
139141
// Returns original path if there was an error
140142
func EscapeTilde(path string) (string, error) {
141143
if !(path == "~" || strings.HasPrefix(path, "~/")) {
@@ -256,23 +258,61 @@ func CheckFileErrPath(path string, errMsgPath string) error {
256258
return nil
257259
}
258260

259-
func CreateDirIfMissing(path string) (bool, error) {
261+
func CreateDir(path string) error {
260262
cleanPath, err := EscapeTilde(path)
261263
if err != nil {
262-
return false, err
264+
return err
265+
}
266+
267+
if err := os.MkdirAll(cleanPath, os.ModePerm); err != nil {
268+
return errors.Wrap(err, errors.Message(ErrorCreateDir(path)))
263269
}
264270

265-
if err := CheckDir(path); err == nil {
271+
return nil
272+
}
273+
274+
func CreateDirIfMissing(path string) (bool, error) {
275+
if IsDir(path) {
266276
return false, nil
267277
}
268278

269-
if err := CheckFile(path); err == nil {
279+
if IsFile(path) {
270280
return false, ErrorFileAlreadyExists(path)
271281
}
272282

273-
err = os.MkdirAll(cleanPath, os.ModePerm)
283+
err := CreateDir(path)
284+
if err != nil {
285+
return false, err
286+
}
287+
288+
return true, nil
289+
}
290+
291+
func DeleteDir(path string) error {
292+
cleanPath, err := EscapeTilde(path)
274293
if err != nil {
275-
return false, errors.Wrap(err, path)
294+
return err
295+
}
296+
297+
if err := os.RemoveAll(cleanPath); err != nil {
298+
return errors.Wrap(err, errors.Message(ErrorDeleteDir(path)))
299+
}
300+
301+
return nil
302+
}
303+
304+
func DeleteDirIfPresent(path string) (bool, error) {
305+
if IsFile(path) {
306+
return false, ErrorFileAlreadyExists(path)
307+
}
308+
309+
if !IsDir(path) {
310+
return false, nil
311+
}
312+
313+
err := DeleteDir(path)
314+
if err != nil {
315+
return false, err
276316
}
277317

278318
return true, nil

pkg/lib/json/json.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func WriteJSON(obj interface{}, outPath string) error {
6363
if err != nil {
6464
return err
6565
}
66-
if err := files.MkdirAll(filepath.Dir(outPath)); err != nil {
66+
if err := files.CreateDir(filepath.Dir(outPath)); err != nil {
6767
return err
6868
}
6969

pkg/lib/zip/zip.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func ToWriter(zipInput *Input, writer io.Writer) error {
121121
}
122122

123123
func ToFile(zipInput *Input, destPath string) error {
124-
zipfile, err := files.CreateFile(destPath)
124+
zipfile, err := files.OpenNewFile(destPath)
125125
if err != nil {
126126
return err
127127
}
@@ -276,12 +276,12 @@ func UnzipToFile(src string, destPath string) ([]string, error) {
276276
filenames = append(filenames, fpath)
277277

278278
if f.FileInfo().IsDir() {
279-
err := files.MkdirAll(fpath)
279+
err := files.CreateDir(fpath)
280280
if err != nil {
281281
return nil, err
282282
}
283283
} else {
284-
err := files.MkdirAll(filepath.Dir(fpath))
284+
err := files.CreateDir(filepath.Dir(fpath))
285285
if err != nil {
286286
return nil, err
287287
}

0 commit comments

Comments
 (0)