-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexists.go
51 lines (44 loc) · 1.07 KB
/
exists.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
package fsutil
import (
"fmt"
"os"
)
// DirExists reports whether path exists and is a directory.
func DirExists(path string) bool {
stat, err := os.Stat(path)
if err != nil {
return false
}
return stat.IsDir()
}
// AssertDirExists panics if path does not exist or is not a directory.
func AssertDirExists(path string) {
if !DirExists(path) {
panic(fmt.Errorf("%s does not exist or is not a directory", path))
}
}
// FileExists reports whether path exists and is a file.
func FileExists(path string) bool {
stat, err := os.Stat(path)
if err != nil {
return false
}
return !stat.IsDir()
}
// AssertFileExists panics if path does not exist or is not a file.
func AssertFileExists(path string) {
if !FileExists(path) {
panic(fmt.Errorf("%s does not exist or is not a file", path))
}
}
// PathExists reports whether path exists.
func PathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// AssertPathExists panics if path does not exist.
func AssertPathExists(path string) {
if !PathExists(path) {
panic(fmt.Errorf("%s does not exist", path))
}
}