-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs_abs.go
More file actions
68 lines (60 loc) · 1.47 KB
/
Copy pathfs_abs.go
File metadata and controls
68 lines (60 loc) · 1.47 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
package command
import (
"context"
"lesiw.io/fs"
"lesiw.io/fs/path"
)
var _ fs.AbsFS = (*cmdFS)(nil)
func (cfs *cmdFS) Abs(ctx context.Context, name string) (string, error) {
if err := cfs.init(ctx); err != nil {
return "", err
}
return abs(ctx, cfs.Machine, cfs.kind, name)
}
func abs(
ctx context.Context, m Machine, kind cfsKind, name string,
) (string, error) {
dir := path.IsDir(name) // Remember if input is a directory.
// Join with WorkDir if provided and path is relative.
if workDir := fs.WorkDir(ctx); workDir != "" && !path.IsAbs(name) {
name = path.Join(workDir, name)
ctx = fs.WithWorkDir(ctx, "")
}
// Localize the path (lexical operation).
name, err := localize(ctx, kind, name)
if err != nil {
return "", err
}
// Then, make it absolute (filesystem operation).
switch kind {
case kindGNU, kindBSD:
real, err := Read(ctx, m, "realpath", name)
if err != nil {
return name, nil
} else if real == "" && dir {
return "./", nil
} else if real == "" {
return ".", nil
} else if dir {
return real + "/", nil
} else {
return real, nil
}
case kindWindows:
real, err := psRead(ctx, m,
"(Resolve-Path -Path '%s' -ErrorAction Stop).Path", name)
if err != nil {
return name, nil
} else if real == "" && dir {
return `.\`, nil
} else if real == "" {
return ".", nil
} else {
return real, nil
}
case kindDOS: // No realpath equivalent, best effort.
return name, nil
default:
return "", errUnsupportedOS
}
}