-
Notifications
You must be signed in to change notification settings - Fork 132
/
fileutils.go
55 lines (51 loc) · 1.25 KB
/
fileutils.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 fileutils
import (
"errors"
"io/fs"
"os"
)
// IsFileOrDirectory returns true if path points to an existing file or directory
// and false otherwise. Errors not satisfying errors.Is(err, fs.ErrNotExist) are
// passed on to the caller.
func IsFileOrDirectory(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
// IsFile returns true if path points to an existing file
// and false otherwise. Errors not satisfying errors.Is(err, fs.ErrNotExist) are
// passed on to the caller.
func IsFile(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err == nil {
if fileInfo.IsDir() {
return false, nil
}
return true, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
// IsDirectory returns true if path points to an existing directory
// and false otherwise. Errors not satisfying errors.Is(err, fs.ErrNotExist) are
// passed on to the caller.
func IsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err == nil {
if fileInfo.IsDir() {
return true, nil
}
return false, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}