-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.go
70 lines (62 loc) · 1.73 KB
/
os.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
package oslib
import (
"os"
"strconv"
"strings"
)
var (
quotedPathSep = strconv.QuoteRuneToASCII(os.PathSeparator)
PathSep = quotedPathSep[1 : len(quotedPathSep)-1]
)
// PathExists returns whether or not the referenced filesystem path exists.
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// PathBaseName takes a full path and returns the end-most path name - this is the base name!
// Counter-part of PathDirName.
func PathBaseName(path string) string {
pieces := strings.Split(path, string(os.PathSeparator))
baseName := pieces[len(pieces)-1]
return baseName
}
// PathDirName takes a full path and returns the parent-directory portion of it.
// Counter-part of PathBaseName.
func PathDirName(path string) string {
pieces := strings.Split(path, string(os.PathSeparator))
dirName := strings.Join(pieces[0:len(pieces)-1], string(os.PathSeparator))
return dirName
}
// IsDirectory returns a boolean indicating if the provided path is a directory.
func IsDirectory(path string) (bool, error) {
fileInfo, err := os.Stat(path)
if err == nil {
return fileInfo.IsDir(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// IsRegularFile returns a boolean indicating if the provided path is a regular file.
func IsRegularFile(path string) (bool, error) {
stat, err := os.Stat(path)
if err == nil {
return stat.Mode().IsRegular(), nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// OsPath returns a fully assembled path string with appropriate OS-specific
// separators.
func OsPath(pathElements ...string) string {
return strings.Join(pathElements, PathSep)
}