-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_str.go
More file actions
272 lines (234 loc) · 6.2 KB
/
path_str.go
File metadata and controls
272 lines (234 loc) · 6.2 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package pathlib
import (
"io/fs"
"iter"
"os"
"path/filepath"
)
type PathStr string
// Beholder --------------------------------------------------------------------
var _ Beholder[PathStr] = PathStr(".")
// Note: go's `os.Stat/Lstat` imitates `stat(2)` from POSIX's libc spec.
// See [os.Stat].
//
// Stat implements [Beholder].
func (p PathStr) Stat() (Info[PathStr], error) {
return stat(p)
}
// Observe the file info of the path on-disk. Note that this does not follow symlinks.
//
// see [os.Lstat].
//
// Lstat implements [Beholder].
func (p PathStr) Lstat() (Info[PathStr], error) {
return lstat(p)
}
// Exists implements [Beholder].
func (p PathStr) Exists() bool {
return exists(p)
}
// PurePath --------------------------------------------------------------------
var _ PurePath = PathStr(".")
// A wrapper around [path/filepath.Join].
func (p PathStr) Join(segments ...string) PathStr {
return join(p, segments...)
}
// Splits the path at every character that's a path separator. Omits empty segments.
// See [os.IsPathSeparator].
func (p PathStr) Parts() (parts []string) {
input := string(p)
vol := filepath.VolumeName(input)
if vol != "" {
parts = append(parts, vol)
input = input[len(vol):]
}
if input == "" {
return
}
var i, last = 1, 0
if os.IsPathSeparator(input[0]) {
parts = append(parts, input[:1])
last = 1
}
for i < len(input) {
if os.IsPathSeparator(input[i]) {
part := input[last:i]
if part != "" {
parts = append(parts, part)
}
last = i + 1
}
i++
}
if last < i {
parts = append(parts, input[last:])
}
return
}
// a wrapper around [path/filepath.Dir].
//
// Parent implements [PurePath].
func (p PathStr) Parent() Dir {
return parent(p)
}
// experimental
func (p PathStr) Ancestors() iter.Seq[Dir] {
return ancestors(p)
}
// A wrapper around [path/filepath.Base].
//
// BaseName implements [PurePath].
func (p PathStr) BaseName() string {
return baseName(p)
}
// A wrapper around [path/filepath.Ext].
//
// Ext implements [PurePath]
func (p PathStr) Ext() string {
return ext(p)
}
// Returns true if the path is absolute, false otherwise.
// See [path/filepath.IsAbs] for more details.
//
// IsAbsolute implements [PurePath].
func (p PathStr) IsAbsolute() bool {
return isAbsolute(p)
}
// returns true if the path is local/relative, false otherwise.
// see [path/filepath.IsLocal] for more details.
//
// IsLocal implements [PurePath].
func (p PathStr) IsLocal() bool {
return isLocal(p)
}
// Readable --------------------------------------------------------------------
var _ Readable[any] = PathStr(".")
// Read attempts to read what the path represents. See [File.Read], [Dir.Read], and
// [Symlink.Read] for the possible return types.
//
// Read implements [Readable].
func (p PathStr) Read() (any, error) {
// can't define this switch as a method of OnDisk[P] since OnDisk[P] has to handle
// any kind of path
var actual os.FileInfo
var val any
var err error
actual, err = p.Lstat()
if err != nil {
return nil, err
}
if actual.Mode().IsDir() {
val, err = Dir(p).Read()
} else if actual.Mode()&fs.ModeSymlink == fs.ModeSymlink {
val, err = Symlink(p).Read()
} else {
val, err = File(p).Read()
}
return val, err
}
// Transformer -----------------------------------------------------------------
var _ Transformer[PathStr] = PathStr(".")
// Convenience method to cast get the untyped string representation of the path.
//
// String implements [Transformer].
func (p PathStr) String() string {
return string(p)
}
// Remove ".", "..", and repeated slashes from a path.
//
// See [path/filepath.Clean].
//
// Clean implements [Transformer].
func (p PathStr) Clean() PathStr {
return clean(p)
}
// Returns an absolute path, or an error if the path cannot be made absolute. Note that there may be more than one
// absolute path for a given input path.
//
// See [path/filepath.Abs].
//
// Abs implements [Transformer].
func (p PathStr) Abs() (PathStr, error) {
return abs(p)
}
// See [path/filepath.Localize].
// Localize implements [Transformer].
func (p PathStr) Localize() (PathStr, error) {
return localize(p)
}
// Returns a relative path to the target directory, or an error if the path cannot be made relative.
//
// See [path/filepath.Rel].
//
// Rel implements [Transformer]
func (p PathStr) Rel(base Dir) (PathStr, error) {
return rel(base, p)
}
// Expand a leading "~" into the user's home directory. If the home directory cannot be
// determined, the path is returned unchanged.
func (p PathStr) ExpandUser() (PathStr, error) {
return expandUser(p)
}
// Changer ----------------------------------------------------------------------
var _ Changer = PathStr(".")
// See [os.Chmod].
//
// Chmod implements [Changer].
func (p PathStr) Chmod(mode os.FileMode) error {
return chmod(p, mode)
}
// Change Ownership of the path.
//
// Chown implements [Changer].
func (p PathStr) Chown(uid int, gid int) error {
return chown(p, uid, gid)
}
// Mover ------------------------------------------------------------------------
var _ Remover[PathStr] = PathStr(".")
// See [os.Remove].
//
// Remove implements [Remover].
func (p PathStr) Remove() error {
return remove(p)
}
// See [os.Rename].
//
// Rename implements [Remover].
func (p PathStr) Rename(newPath PathStr) (PathStr, error) {
return rename(p, newPath)
}
// -----------------------------------------------------------------------------
// Returns true if the two paths represent the same path.
//
// Eq implements [Transformer].
func (p PathStr) Eq(q PathStr) bool {
// try to avoid panicking if Cwd() can't be obtained
p = p.Clean()
q = q.Clean()
if p.IsLocal() && q.IsLocal() {
return p == q
}
x, err := p.Abs()
if err != nil {
return false
}
y, err := q.Abs()
if err != nil {
return false
}
// TODO: check that this still works with UNC strings on windows
return x == y
}
// casts -----------------------------------------------------------------------
// Utility function to declare that the PathStr represents a directory
func (p PathStr) AsDir() Dir {
return Dir(p)
}
// Utility function to declare that the PathStr represents a file
func (p PathStr) AsFile() File {
return File(p)
}
// Utility function to declare that the PathStr represents a symlink.
func (p PathStr) AsSymlink() Symlink {
return Symlink(p)
}