-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.go
43 lines (38 loc) · 1008 Bytes
/
files.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
package pkg
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// check a list of dirs
func CheckDirLists(dirs ...string) error {
for _, dir := range dirs {
if err := CheckDir(filepath.Join(dir)); err != nil {
return err
}
}
return nil
}
// check if the dir exits,if not create it.
func CheckDir(path string) error {
if fileInfo, err := os.Stat(path); err != nil {
if os.IsNotExist(err) { // directory not exists.
if err := os.MkdirAll(path, 0744); err != nil { // todo
return err // create dir error.
}
} else {
return err
}
} else if !fileInfo.IsDir() { // if exists,but is not dir.
return fmt.Errorf("%s is not a directory", path)
}
return nil
}
// change path to relative path, replace PKG_DIR with relative path.
func RelativePath(base, target string) string {
// // replace absolute path with relative path.
relPath := strings.TrimPrefix(target, base) // relative pkg path
relPath = strings.TrimPrefix(relPath, string(filepath.Separator))
return relPath
}