/
irods_path.go
121 lines (99 loc) · 2.65 KB
/
irods_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package util
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strings"
"golang.org/x/xerrors"
)
// MakeIRODSPath makes the path from collection and data object
func MakeIRODSPath(collectionPath string, dataobjectName string) string {
if strings.HasSuffix(collectionPath, "/") {
return fmt.Sprintf("%s/%s", collectionPath[0:len(collectionPath)-1], dataobjectName)
}
return fmt.Sprintf("%s/%s", collectionPath, dataobjectName)
}
// SplitIRODSPath splits the path into dir and file
func SplitIRODSPath(p string) (string, string) {
return path.Split(p)
}
// GetIRODSPathDirname returns the dir of the path
func GetIRODSPathDirname(p string) string {
return path.Dir(p)
}
// GetIRODSPathFileName returns the filename of the path
func GetIRODSPathFileName(p string) string {
return path.Base(p)
}
// GetIRODSZone returns the zone of the path
func GetIRODSZone(p string) (string, error) {
if len(p) == 0 || p[0] != '/' {
return "", xerrors.Errorf("cannot extract Zone from path %s", p)
}
parts := strings.Split(p[1:], "/")
if len(parts) >= 1 {
return parts[0], nil
}
return "", xerrors.Errorf("cannot extract Zone from path %s", p)
}
// GetCorrectIRODSPath corrects the path
func GetCorrectIRODSPath(p string) string {
if p == "" || p == "/" {
return "/"
}
newPath := path.Clean(p)
if !strings.HasPrefix(newPath, "/") {
newPath = fmt.Sprintf("/%s", newPath)
newPath = path.Clean(newPath)
}
return newPath
}
// GetIRODSPathDepth returns depth of the path
// "/" returns 0
// "abc" returns -1
// "/abc" returns 0
// "/a/b" returns 1
// "/a/b/c" returns 2
func GetIRODSPathDepth(p string) int {
if !strings.HasPrefix(p, "/") {
return -1
}
cleanPath := path.Clean(p)
if cleanPath == "/" {
return 0
}
pArr := strings.Split(p[1:], "/")
return len(pArr) - 1
}
// GetParentDirs returns all parent dirs
func GetParentIRODSDirs(p string) []string {
parents := []string{}
if p == "/" {
return parents
}
curPath := p
for len(curPath) > 0 && curPath != "/" {
curDir := path.Dir(curPath)
if len(curDir) > 0 {
parents = append(parents, curDir)
}
curPath = curDir
}
// sort
sort.Slice(parents, func(i int, j int) bool {
return len(parents[i]) < len(parents[j])
})
return parents
}
// GetRelativePath returns relative path
func GetRelativeIRODSPath(base string, target string) (string, error) {
osBase := strings.ReplaceAll(base, "/", string(os.PathSeparator))
osTarget := strings.ReplaceAll(target, "/", string(os.PathSeparator))
rel, err := filepath.Rel(osBase, osTarget)
if err != nil {
return "", xerrors.Errorf("failed to calculate relative path from %s to %s: %w", osBase, osTarget, err)
}
return filepath.ToSlash(rel), nil
}