-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpath.go
63 lines (52 loc) · 1.34 KB
/
path.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
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"fmt"
"path/filepath"
"strings"
)
func PathWithoutExt(filename string) string {
ext := filepath.Ext(filename)
return strings.TrimSuffix(filename, ext)
}
// делить название файла на само название и расширение
// при этом на название не влияет путь, в котором расположен файл
func PathSplitExt(path string) (basepath, ext string) {
filename := filepath.Base(path)
if filename == "." {
return "", ""
}
hidden := false
if strings.HasPrefix(filename, ".") {
hidden = true
filename = strings.TrimPrefix(filename, ".")
}
ext = filepath.Ext(filename)
basepath = filename[:len(filename)-len(ext)]
if hidden {
basepath = "." + basepath
}
ext = strings.TrimPrefix(ext, ".")
return
}
func PathIsWritable(path string) bool {
return pathIsWritable(path)
}
func PathNearestExisting(path string) string {
if FileExists(path) {
return path
}
testpath := path
for testpath != "" {
testpath, _ = filepath.Split(testpath)
testpath = testpath[:len(testpath)-1] // removing trailing /
if FileExists(testpath) {
return testpath
}
fmt.Println(testpath)
}
return ""
}