-
Notifications
You must be signed in to change notification settings - Fork 18
/
fs.go
133 lines (105 loc) · 3.29 KB
/
fs.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
package builtin
import (
"context"
"errors"
"github.com/cirruslabs/cirrus-cli/pkg/larker/fs"
"go.starlark.net/starlark"
"os"
)
type ResolveFSFunc func(string) (fs.FileSystem, string, error)
func FS(ctx context.Context, resolveFS ResolveFSFunc) starlark.StringDict {
return starlark.StringDict{
"exists": exists(ctx, resolveFS),
"read": read(ctx, resolveFS),
"readdir": readdir(ctx, resolveFS),
"isdir": isdir(ctx, resolveFS),
}
}
func exists(ctx context.Context, resolveFS ResolveFSFunc) starlark.Value {
const funcName = "exists"
return starlark.NewBuiltin(funcName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(funcName, args, kwargs, 1, &path); err != nil {
return nil, err
}
resolvedFS, path, err := resolveFS(path)
if err != nil {
return nil, err
}
_, err = resolvedFS.Stat(ctx, path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return starlark.Bool(false), nil
}
return nil, err
}
return starlark.Bool(true), nil
})
}
func read(ctx context.Context, resolveFS ResolveFSFunc) starlark.Value {
const funcName = "read"
return starlark.NewBuiltin(funcName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(funcName, args, kwargs, 1, &path); err != nil {
return nil, err
}
resolvedFS, path, err := resolveFS(path)
if err != nil {
return nil, err
}
fileBytes, err := resolvedFS.Get(ctx, path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return starlark.None, nil
}
return nil, err
}
return starlark.String(fileBytes), nil
})
}
func readdir(ctx context.Context, resolveFS ResolveFSFunc) starlark.Value {
const funcName = "readdir"
return starlark.NewBuiltin(funcName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(funcName, args, kwargs, 1, &path); err != nil {
return nil, err
}
resolvedFS, path, err := resolveFS(path)
if err != nil {
return nil, err
}
entries, err := resolvedFS.ReadDir(ctx, path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return starlark.None, nil
}
return nil, err
}
var starlarkEntries []starlark.Value
for _, entry := range entries {
starlarkEntries = append(starlarkEntries, starlark.String(entry))
}
return starlark.NewList(starlarkEntries), nil
})
}
func isdir(ctx context.Context, resolveFS ResolveFSFunc) starlark.Value {
const funcName = "isdir"
return starlark.NewBuiltin(funcName, func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path string
if err := starlark.UnpackPositionalArgs(funcName, args, kwargs, 1, &path); err != nil {
return nil, err
}
resolvedFS, path, err := resolveFS(path)
if err != nil {
return nil, err
}
fileInfo, err := resolvedFS.Stat(ctx, path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return starlark.False, nil
}
return nil, err
}
return starlark.Bool(fileInfo.IsDir), nil
})
}