-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.go
124 lines (99 loc) · 2.5 KB
/
os.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
package funcs
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/rytsh/liz/fstore/generic"
"github.com/spf13/afero"
"github.com/spf13/cast"
)
func init() {
generic.CallReg.AddFunction("os", new(Os).init, "workDir")
}
type Os struct {
workFs afero.Fs
workDir string
}
func (o *Os) init(workDir string) any {
o.workFs = afero.NewOsFs()
o.workDir = workDir
return o
}
// ReadDir lists the directory contents relative to the configured WorkingDir.
func (o *Os) ReadDir(i any) ([]os.FileInfo, error) {
path, err := cast.ToStringE(i)
if err != nil {
return nil, err
}
// check filename is relative
if !filepath.IsAbs(path) {
path = filepath.Join(o.workDir, path)
}
list, err := afero.ReadDir(o.workFs, path)
if err != nil {
return nil, fmt.Errorf("failed to read directory %q: %s", path, err)
}
return list, nil
}
// readFile reads the file named by filename in the given filesystem
// and returns the contents as a string.
func (o *Os) readFile(filename string) (string, error) {
filename = filepath.Clean(filename)
if filename == "" || filename == "." || filename == string(os.PathSeparator) {
return "", errors.New("invalid filename")
}
// check filename is relative
if !filepath.IsAbs(filename) {
filename = filepath.Join(o.workDir, filename)
}
b, err := afero.ReadFile(o.workFs, filename)
if err != nil {
return "", err
}
return string(b), nil
}
// ReadFile reads the file named by filename relative to the configured WorkingDir.
// It returns the contents as a string.
// There is an upper size limit set at 1 megabytes.
func (o *Os) ReadFile(i any) (string, error) {
s, err := cast.ToStringE(i)
if err != nil {
return "", err
}
return o.readFile(s)
}
// FileExists checks whether a file exists under the given path.
func (o *Os) FileExists(i any) (bool, error) {
path, err := cast.ToStringE(i)
if err != nil {
return false, err
}
if path == "" {
return false, errors.New("fileExists needs a path to a file")
}
// check filename is relative
if !filepath.IsAbs(path) {
path = filepath.Join(o.workDir, path)
}
status, err := afero.Exists(o.workFs, path)
if err != nil {
return false, err
}
return status, nil
}
// Stat returns the os.FileInfo structure describing file.
func (o *Os) Stat(i any) (os.FileInfo, error) {
path, err := cast.ToStringE(i)
if err != nil {
return nil, err
}
if path == "" {
return nil, errors.New("fileStat needs a path to a file")
}
r, err := o.workFs.Stat(path)
if err != nil {
return nil, err
}
return r, nil
}