-
Notifications
You must be signed in to change notification settings - Fork 303
/
io.go
184 lines (150 loc) · 4.06 KB
/
io.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package io
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"go.starlark.net/starlark"
"github.com/windmilleng/tilt/internal/sliceutils"
"github.com/windmilleng/tilt/internal/tiltfile/starkit"
"github.com/windmilleng/tilt/internal/tiltfile/value"
)
type Extension struct{}
func NewExtension() Extension {
return Extension{}
}
func (Extension) NewState() interface{} {
return ReadState{}
}
func (Extension) OnStart(e *starkit.Environment) error {
err := e.AddBuiltin("read_file", readFile)
if err != nil {
return err
}
err = e.AddBuiltin("watch_file", watchFile)
if err != nil {
return err
}
err = e.AddBuiltin("listdir", listdir)
if err != nil {
return err
}
err = e.AddBuiltin("blob", blob)
if err != nil {
return err
}
return nil
}
func (Extension) OnExec(t *starlark.Thread, tiltfilePath string) error {
return RecordReadFile(t, tiltfilePath)
}
func readFile(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path starlark.Value
defaultReturn := ""
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "paths", &path, "default?", &defaultReturn)
if err != nil {
return nil, err
}
p, err := value.ValueToAbsPath(thread, path)
if err != nil {
return nil, fmt.Errorf("invalid type for paths: %v", err)
}
bs, err := ReadFile(thread, p)
if os.IsNotExist(err) {
bs = []byte(defaultReturn)
} else if err != nil {
return nil, err
}
return NewBlob(string(bs), fmt.Sprintf("file: %s", p)), nil
}
func watchFile(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path starlark.Value
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "paths", &path)
if err != nil {
return nil, err
}
p, err := value.ValueToAbsPath(thread, path)
if err != nil {
return nil, fmt.Errorf("invalid type for paths: %v", err)
}
err = RecordReadFile(thread, p)
if err != nil {
return nil, err
}
return starlark.None, nil
}
func listdir(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var dir starlark.String
var recursive bool
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "dir", &dir, "recursive?", &recursive)
if err != nil {
return nil, err
}
localPath, err := value.ValueToAbsPath(thread, dir)
if err != nil {
return nil, fmt.Errorf("Argument 0 (paths): %v", err)
}
err = RecordReadFile(thread, localPath)
if err != nil {
return nil, err
}
var files []string
err = filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error {
if path == localPath {
return nil
}
if !info.IsDir() {
files = append(files, path)
} else if info.IsDir() && !recursive {
return filepath.SkipDir
}
return nil
})
if err != nil {
return nil, err
}
var ret []starlark.Value
for _, f := range files {
ret = append(ret, starlark.String(f))
}
return starlark.NewList(ret), nil
}
func blob(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var input starlark.String
err := starkit.UnpackArgs(thread, fn.Name(), args, kwargs, "input", &input)
if err != nil {
return nil, err
}
return NewBlob(input.GoString(), "Tiltfile blob() call"), nil
}
// Track all the files read while loading
type ReadState struct {
Files []string
}
func ReadFile(thread *starlark.Thread, p string) ([]byte, error) {
err := RecordReadFile(thread, p)
if err != nil {
return nil, err
}
return ioutil.ReadFile(p)
}
func RecordReadFile(t *starlark.Thread, files ...string) error {
return starkit.SetState(t, func(s ReadState) ReadState {
s.Files = sliceutils.AppendWithoutDupes(s.Files, files...)
return s
})
}
var _ starkit.StatefulExtension = Extension{}
var _ starkit.OnExecExtension = Extension{}
func MustState(model starkit.Model) ReadState {
state, err := GetState(model)
if err != nil {
panic(err)
}
return state
}
func GetState(m starkit.Model) (ReadState, error) {
var state ReadState
err := m.Load(&state)
return state, err
}