-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathlib.go
675 lines (543 loc) · 14.2 KB
/
pathlib.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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// Package pathlib contains every functionality for go-pathlib.
// It's a one-file library that can be used in other projects by using Go's package system
// or by placing the source code file itself into the source tree.
package pathlib
import (
"errors"
"os"
"path/filepath"
"runtime"
"strings"
)
const (
// pathCheckNoExist indicates that the checked Path does not exist.
pathCheckNoExist = iota
// pathCheckFile indicates that the checked Path is a file.
pathCheckFile
// pathCheckDir indicates that the checked Path is a directory.
pathCheckDir
)
// pathSeparator is the string representation of filepath.Separator
const pathSeparator = string(filepath.Separator)
/*
Path is a struct that represents a filesystem path.
Create a new instance using NewPath().
Other constructor functions are prefixed with 'New'.
*/
type Path struct {
// The underlying filepath string representation. This is the source of
// truth and other functions are relying on the assumption that this
// value has not been changed between operations.
path string
}
/*
NewPath is the constructor function for a new Path struct instance.
The passed path string is automatically cleaned and ready for further use.
*/
func NewPath(path string) *Path {
return &Path{path: cleanPathString(path)}
}
/*
NewCwd returns a new Path instance pointing to the application's current working directory.
This function utilizes os.Getwd.
*/
func NewCwd() (*Path, error) {
cwdPath, err := os.Getwd()
if err != nil {
return nil, err
}
return NewPath(cwdPath), nil
}
/*
NewHome returns a new Path instance pointing to the user's home directory.
This function utilizes os.UserHomeDir.
*/
func NewHome() (*Path, error) {
homePath, err := os.UserHomeDir()
if err != nil {
return nil, err
}
return NewPath(homePath), nil
}
/*
PathFromParts combines passed parts into a new Path.
*/
func PathFromParts(parts ...string) *Path {
return NewPath(".").JoinStrings(parts...)
}
/*
IsFile returns whether this Path is an existing file.
*/
func (p *Path) IsFile() bool {
return pathCheck(p) == pathCheckFile
}
/*
IsDir returns whether this Path is an existing directory.
*/
func (p *Path) IsDir() bool {
return pathCheck(p) == pathCheckDir
}
/*
Exists returns whether this Path exists.
*/
func (p *Path) Exists() bool {
return pathCheck(p) != pathCheckNoExist
}
/*
Parent returns a copy of this Path in the parent directory.
This function utilizes filepath.Dir.
*/
func (p *Path) Parent() *Path {
return NewPath(filepath.Dir(p.path))
}
/*
Parts returns all single parts of the Path.
It uses filepath.Separator to split the path string.
*/
func (p *Path) Parts() []string {
separator := pathSeparator
toSplit := strings.Trim(p.path, separator)
if toSplit == "" {
return []string{}
}
return strings.Split(toSplit, separator)
}
/*
Split splits this Path into its parent and base.
*/
func (p *Path) Split() (*Path, string) {
dir, file := filepath.Split(p.path)
return NewPath(dir), file
}
/*
Base returns the last element of this Path.
This function utilizes filepath.Base.
*/
func (p *Path) Base() string {
return filepath.Base(p.path)
}
/*
Stem returns the base of this Path without all extensions.
*/
func (p *Path) Stem() string {
ok, stem := getStem(p)
if ok {
return stem
}
return ""
}
/*
HasExtensions returns whether this Path has file extensions.
*/
func (p *Path) HasExtensions() bool {
ok, base := createExtensionEvaluationBase(p)
if !ok {
return false
}
return hasDots(base)
}
/*
ExtensionCount returns the amount of extensions this Path has.
*/
func (p *Path) ExtensionCount() int {
ok, base := createExtensionEvaluationBase(p)
if !ok {
return 0
}
return dotCount(base)
}
/*
Extension returns the complete extension of this Path.
Any prefixed dots are included.
Everything starting from the first non-leading dot in this Path's Stem()
is considered to be an extension.
*/
func (p *Path) Extension() string {
stem := p.Stem()
// if no stem exists, then there also are no extensions
if len(stem) == 0 {
return ""
}
return p.Base()[len(stem):]
}
/*
ExtensionParts returns all this Path's extensions as an array without the leading dots.
*/
func (p *Path) ExtensionParts() []string {
ok, base := createExtensionEvaluationBase(p)
if !ok {
return []string{}
}
return strings.Split(base, ".")[1:]
}
/*
Root returns the first part of the path.
On absolute paths this is the filesystem root, on relative paths all parts
up to the first non-'..' part are included.
On Unix-based operating systems, the Windows path root (e.g. 'C:\')
is not considered a filepath root. However, it will be returned as a root
because 'C:\' or 'C:/' is seen as the root of a relative path.
*/
func (p *Path) Root() string {
if p.IsRelative() {
parts := p.Parts()
var rootParts []string
for _, part := range parts {
rootParts = append(rootParts, part)
if part != ".." {
break
}
}
return filepath.Join(rootParts...)
}
pathStr := p.path
if pathStr == pathSeparator {
return pathStr
}
root := strings.SplitN(p.path, pathSeparator, 2)[0]
if root == "" {
return pathSeparator
}
return root
}
/*
IsAbsolute returns whether this Path is absolute.
On non-Windows operating systems, the Windows path root (e.g. 'C:\')
is not considered a file root but as a regular (relative) path element.
Thus, this function would return false.
This function utilizes filepath.IsAbs.
*/
func (p *Path) IsAbsolute() bool {
return filepath.IsAbs(p.path)
}
/*
IsRelative returns whether this Path is relative.
This function returns the inverse of IsAbsolute.
*/
func (p *Path) IsRelative() bool {
return !p.IsAbsolute()
}
/*
RelativeTo returns this Path relative to another.
This function utilizes filepath.Rel.
*/
func (p *Path) RelativeTo(o *Path) (*Path, error) {
rp, err := filepath.Rel(o.path, p.path)
return NewPath(rp), err
}
/*
Absolute returns an absolute representation of this Path.
If the Path is relative, it will be joined with the current working directory.
This function utilizes filepath.Abs.
*/
func (p *Path) Absolute() (*Path, error) {
ap, err := filepath.Abs(p.path)
return NewPath(ap), err
}
/*
AbsoluteTo returns an absolute representation of this Path towards another.
If the Path is relative, it will be joined with the provided Path, else this Path is returned.
Requires the other Path to be absolute.
*/
func (p *Path) AbsoluteTo(o *Path) (*Path, error) {
// if path is already absolute, ignore it
if p.IsAbsolute() {
return p, nil
}
if o.IsRelative() {
return nil, errors.New("other path must be absolute")
}
return o.Join(p), nil
}
/*
Resolve resolves all symbolic links. If this Path is relative,
the result will be relative to the current directory, unless
one of the components is an absolute symbolic link.
Resolve requires this Path to exist.
This function utilizes filepath.EvalSymlinks.
*/
func (p *Path) Resolve() (*Path, error) {
if !p.Exists() {
return nil, errors.New("this path does not exist")
}
ep, err := filepath.EvalSymlinks(p.path)
if err != nil {
return nil, err
}
return NewPath(ep), nil
}
/*
Join returns a new Path with all passed Path structs joined together.
Use JoinStrings to join strings with this Path.
This function utilizes filepath.Join.
*/
func (p *Path) Join(paths ...*Path) *Path {
pathsStr := make([]string, len(paths))
for i, path := range paths {
pathsStr[i] = path.path
}
return NewPath(filepath.Join(append([]string{p.path}, pathsStr...)...))
}
/*
JoinStrings returns a new Path with all passed strings joined together.
This function utilizes filepath.Join.
*/
func (p *Path) JoinStrings(paths ...string) *Path {
return NewPath(filepath.Join(append([]string{p.path}, paths...)...))
}
/*
Glob returns all paths matching the given pattern within this Path's directory.
This function utilizes filepath.Glob. It ignores IO errors.
*/
func (p *Path) Glob(pattern string) ([]*Path, error) {
matches, err := nativeGlob(p, pattern)
if err != nil {
return nil, err
}
paths := make([]*Path, len(matches))
for idx, match := range matches {
paths[idx] = NewPath(match)
}
return paths, nil
}
/*
Contains returns whether the passed pattern exist within this Path's directory.
This function utilizes filepath.Glob.
*/
func (p *Path) Contains(pattern string) (bool, error) {
matches, err := nativeGlob(p, pattern)
if err != nil {
return false, err
}
return len(matches) != 0, nil
}
/*
BContains returns whether the passed pattern exists within this Path's directory.
It wraps Contains and returns the boolean success value.
*/
func (p *Path) BContains(pattern string) bool {
contains, _ := p.Contains(pattern)
return contains
}
/*
Equals returns whether this and another Path match lexically.
*/
func (p *Path) Equals(other *Path) bool {
return p.path == other.path
}
/*
EqualsString returns whether this and the passed string match lexically.
This function converts the passed string to a Path object and calls Equals.
*/
func (p *Path) EqualsString(other string) bool {
return p.path == cleanPathString(other)
}
/*
EqualsFlat returns whether this and another Path are structurally equal
by ignoring case sensitivity.
*/
func (p *Path) EqualsFlat(other *Path) bool {
return equalsStringCaseInsensitive(p.String(), other.String())
}
/*
EqualsStringFlat returns whether the passed string matches this Path
by ignoring case sensitivity.
*/
func (p *Path) EqualsStringFlat(other string) bool {
return equalsStringCaseInsensitive(p.String(), cleanPathString(other))
}
/*
ToPosix returns a string representation with forward slashes.
*/
func (p *Path) ToPosix() string {
return filepath.ToSlash(p.String())
}
/*
WithName returns this Path but with another base.
*/
func (p *Path) WithName(name string) *Path {
return p.Parent().JoinStrings(name)
}
/*
Copy creates a copy of this Path.
Fresh out of the oven, just for you.
*/
func (p *Path) Copy() *Path {
return NewPath(p.path)
}
/*
String returns this Path as a string.
*/
func (p *Path) String() string {
pathStr := p.path
// re-add removed whitespace escape characters
if runtime.GOOS != "windows" {
pathStr = strings.ReplaceAll(pathStr, " ", "\\ ")
}
return pathStr
}
/*
UnmarshalText unmarshalls any byte array into a Path type.
Implements the encoding.TextUnmarshaler interface.
*/
func (p *Path) UnmarshalText(text []byte) error {
*p = *NewPath(string(text))
return nil
}
/*
MarshalText marshals this Path into a byte array.
Implements the encoding.TextMarshaler interface.
*/
func (p *Path) MarshalText() (text []byte, err error) {
return []byte(p.String()), nil
}
/*
clean cleans up this Path.
This function utilizes filepath.Clean.
*/
func cleanPathString(p string) string {
dirty := strings.TrimSpace(p)
// on non-windows operating systems
if runtime.GOOS != "windows" {
// remove whitespace escape characters during internal representation
dirty = strings.ReplaceAll(dirty, "\\ ", " ")
// replace all other '\\' characters with separator
dirty = strings.ReplaceAll(dirty, "\\", pathSeparator)
}
cleanPath := filepath.Clean(dirty)
return cleanPath
}
func createExtensionEvaluationBase(p *Path) (bool, string) {
base := p.Base()
// define edge cases
if base == "." || base == ".." || base == pathSeparator {
return false, ""
}
// ignore leading dots
base = strings.TrimLeft(base, ".")
return true, base
}
func getStem(p *Path) (bool, string) {
base := p.Base()
ok, baseNoLeadingDots := createExtensionEvaluationBase(p)
// handle stem-specific edge case
if base == ".." {
return true, ".."
}
if !ok {
return false, ""
}
// check for any existing trailing dot
firstDotIdx := strings.IndexAny(baseNoLeadingDots, ".")
// if no dot found, return complete base
if firstDotIdx == -1 {
return true, base
}
// else return base without extensions
baseNoDots := baseNoLeadingDots[:firstDotIdx]
return true, base[:len(base)-len(baseNoLeadingDots)] + baseNoDots
}
/*
hasDots is a simple helper function that returns whether the given
string contains a '.' character.
It does not use strings.Contains for overhead reasons.
*/
func hasDots(s string) bool {
switch len(s) {
case 0:
return false
case 1:
return s[0] == '.'
}
dotAscii := '.'
for _, v := range s {
if v == dotAscii {
return true
}
}
return false
}
func dotCount(s string) int {
switch len(s) {
case 0:
return 0
case 1:
if s[0] == '.' {
return 1
}
return 0
}
dotAscii := '.'
count := 0
for _, v := range s {
if v == dotAscii {
count += 1
}
}
return count
}
/*
pathCheck is a lower level Path existence checker.
It returns 0 if the path does not exist, 2 if it's a file and 2 if it's a directory.
*/
func pathCheck(p *Path) int {
fileInfo, err := os.Stat(p.path)
if err != nil {
if os.IsNotExist(err) {
return pathCheckNoExist
}
}
if fileInfo == nil {
return pathCheckNoExist
}
if fileInfo.IsDir() {
return pathCheckDir
}
return pathCheckFile
}
/*
flipCase is a utility function that takes the first character and flips it's case.
The leftover characters are appended.
This results in a string which is different from the original which can be used
for e.g. case sensitivity (in)variance checks.
*/
func flipCase(s string) string {
if s == "" {
return s
}
firstChar := string(s[0])
if strings.ToLower(firstChar) == firstChar {
return strings.ToUpper(firstChar) + s[1:]
}
return strings.ToLower(firstChar) + s[1:]
}
/*
nativeGlob is a wrapper function for Go's filepath.Glob.
It checks if the passed Path exists and returns the raw matches or errors.
Returns an error if pattern is an empty string.
filepath.Glob ignores IO errors.
*/
func nativeGlob(p *Path, pattern string) ([]string, error) {
if strings.TrimSpace(pattern) == "" {
return nil, errors.New("pattern must not be empty")
}
if !p.Exists() {
return nil, errors.New("this Path does not exist")
}
if !p.IsDir() {
return nil, errors.New("this path is not a directory")
}
matches, err := filepath.Glob(filepath.Join(p.path, pattern))
if err != nil {
return nil, err
}
return matches, nil
}
func equalsStringCaseInsensitive(first string, second string) bool {
// lowercase the strings and compare them
thisLowerCase := strings.ToLower(first)
otherLowerCase := strings.ToLower(second)
// if not equal in lowercase, then they are not the same path
// this tests if the actual path strings are equal
return thisLowerCase == otherLowerCase
}