-
Notifications
You must be signed in to change notification settings - Fork 29
/
os_helper.go
60 lines (54 loc) · 1.14 KB
/
os_helper.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
package toolbox
import (
"fmt"
"os"
"path"
"strings"
)
var dirMode os.FileMode = 0744
// RemoveFileIfExist remove file if exists
func RemoveFileIfExist(filenames ...string) error {
for _, filename := range filenames {
if !FileExists(filename) {
continue
}
err := os.Remove(filename)
if err != nil {
return err
}
}
return nil
}
// FileExists checks if file exists
func FileExists(filename string) bool {
if _, err := os.Stat(filename); err != nil {
return false
}
return true
}
// IsDirectory checks if file is directory
func IsDirectory(location string) bool {
if stat, _ := os.Stat(location); stat != nil {
return stat.IsDir()
}
return false
}
// CreateDirIfNotExist creates directory if they do not exist
func CreateDirIfNotExist(dirs ...string) error {
for _, dir := range dirs {
if len(dir) > 1 && strings.HasSuffix(dir, "/") {
dir = dir[:len(dir)-1]
}
parent, _ := path.Split(dir)
if parent != "/" && parent != dir {
CreateDirIfNotExist(parent)
}
if !FileExists(dir) {
err := os.Mkdir(dir, dirMode)
if err != nil {
return fmt.Errorf("failed to create dir %v %v", dir, err)
}
}
}
return nil
}