-
Notifications
You must be signed in to change notification settings - Fork 11
/
system.go
54 lines (45 loc) · 1.01 KB
/
system.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
package gowork
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
)
const rootDir = "/"
var (
systemFS = os.DirFS(rootDir) //nolint:gochecknoglobals
_ operatingsystem = RealSystem{}
)
// RealSystem is the real, live OS implementation.
type RealSystem struct{}
func (s RealSystem) Get(name string) string {
return os.Getenv(name)
}
func (s RealSystem) Open(name string) (fs.File, error) {
f, err := systemFS.Open(name)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrBug, err)
}
return f, nil
}
func (s RealSystem) Cwd() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("%w: %v", ErrBug, err)
}
p, err := filepath.Rel(rootDir, dir)
if err != nil {
return "", fmt.Errorf("%w: %v", ErrBug, err)
}
return p, nil
}
func (s RealSystem) Abs(filepath string) string {
return path.Clean(path.Join(rootDir, filepath))
}
// OS represents a virtual operating system.
type operatingsystem interface {
FileSystem
Environment
Abs(filepath string) string
}