-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfs.go
More file actions
98 lines (84 loc) · 2.03 KB
/
fs.go
File metadata and controls
98 lines (84 loc) · 2.03 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
package command
import (
"context"
"fmt"
"sync"
"lesiw.io/fs"
)
// FS returns a filesystem (lesiw.io/fs.FS) that executes commands on m to
// perform filesystem operations.
//
// If m implements FSMachine, FS() calls m.FS(ctx) and uses the returned
// filesystem. If m.FS(ctx) returns nil, FS falls back to creating a
// command-based filesystem.
func FS(m Machine) fs.FS {
if fsm, ok := m.(FSMachine); ok {
if fsys := fsm.FS(); fsys != nil {
return fsys
}
}
return &cmdFS{Machine: m}
}
type cfsKind int
const (
kindUnknown cfsKind = iota
kindGNU
kindBSD
kindWindows
kindDOS
)
type cmdFS struct {
Machine // After init(), the filesystem-capable machine.
once sync.Once
initErr error
// Capabilities discovered after init().
kind cfsKind
hasTar bool
}
func (cfs *cmdFS) init(ctx context.Context) error {
cfs.once.Do(func() { cfs.initErr = cfs.doInit(ctx) })
return cfs.initErr
}
func (cfs *cmdFS) doInit(ctx context.Context) error {
switch OS(ctx, cfs.Machine) {
case "linux":
cfs.kind = kindGNU
case "darwin", "freebsd", "openbsd", "netbsd", "dragonfly":
cfs.kind = kindBSD
case "windows":
if err := psDo(ctx, cfs.Machine, "exit 0"); err == nil {
cfs.kind = kindWindows
} else {
cfs.kind = kindDOS
}
default:
return fmt.Errorf("failed to detect OS type")
}
// Use a simple command to probe for a filesystem-capable Machine.
var args []string
switch cfs.kind {
case kindGNU, kindBSD:
args = []string{"cat", "/dev/null"}
case kindWindows, kindDOS:
args = []string{"cmd", "/c", "type", "NUL"}
default:
panic("unreachable")
}
m := cfs.Machine
for {
if err := Do(ctx, m, args...); err == nil || !NotFound(err) {
break // Found a machine that can execute filesystem commands.
}
if u, ok := m.(Unsheller); ok {
if unshelled := u.Unshell(); unshelled != nil {
m = unshelled
continue
}
}
return fmt.Errorf("no filesystem-capable machine found")
}
cfs.Machine = m
cfs.hasTar = !NotFound(Do(ctx, cfs, "tar"))
return nil
}
var errUnsupportedOS = fmt.Errorf("unknown OS")