-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.go
More file actions
322 lines (297 loc) · 8.54 KB
/
Copy pathexpr.go
File metadata and controls
322 lines (297 loc) · 8.54 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package expression
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"time"
"github.com/expr-lang/expr"
"github.com/expr-lang/expr/vm"
"mvdan.cc/sh/v3/expand"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
)
func IsTruthy(ex string, data Data) (bool, error) {
output, err := Evaluate(ex, data)
if err != nil {
return false, err
}
switch v := output.(type) {
case bool:
return v, nil
case int, int64, float64, uint, uint64:
return v != 0, nil
case string:
truthy, err := strconv.ParseBool(strings.Trim(v, `"' `))
if err != nil {
return false, err
}
return truthy, nil
default:
return false, nil
}
}
func Evaluate(ex string, data Data) (interface{}, error) {
var program *vm.Program
var err error
opts := additionalFunctions()
if data != nil && !reflect.ValueOf(data).IsNil() {
opts = append(opts, expr.Env(data))
}
program, err = expr.Compile(ex, opts...)
if err != nil {
return nil, err
}
output, err := expr.Run(program, data)
if err != nil {
return nil, err
}
return output, nil
}
func EvaluateString(ex string, data Data) (string, error) {
output, err := Evaluate(ex, data)
if err != nil {
return "", err
}
switch o := output.(type) {
case string:
return o, nil
case int, int64, float64, uint, uint64:
return fmt.Sprintf("%v", o), nil
case bool:
return strconv.FormatBool(o), nil
case []byte:
return string(o), nil
default:
if output == nil {
return "", nil
}
if reflect.TypeOf(output).Kind() == reflect.Ptr && reflect.ValueOf(output).IsNil() {
return "", nil // Handle nil pointer gracefully
}
if reflect.TypeOf(output).Kind() == reflect.Map ||
reflect.TypeOf(output).Kind() == reflect.Slice ||
reflect.TypeOf(output).Kind() == reflect.Array {
return fmt.Sprintf("%v", output), nil
}
}
return "", fmt.Errorf("unexpected output type %T from expression %q", output, ex)
}
type Data interface{}
// BuildData constructs a Data object from a context, environment map, and key-value pairs.
// It provides the following variables by default:
// - `os`: string for the operating system (e.g., "linux", "darwin")
// - `arch`: string for the architecture (e.g., "amd64", "arm64")
// - `env`: the environment variables passed in the envMap
// - `$`: a function that takes a shell command as input and returns its output as a string
func BuildData(ctx context.Context, envMap map[string]string, kvPairs ...interface{}) (Data, error) {
kvMap := make(map[string]interface{})
if len(kvPairs)%2 != 0 {
return nil, fmt.Errorf("uneven number of key-value pairs")
}
for i := 0; i < len(kvPairs); i += 2 {
key, ok := kvPairs[i].(string)
if !ok {
return nil, fmt.Errorf("key must be a string, got %T", kvPairs[i])
}
value := kvPairs[i+1]
kvMap[key] = value
}
kvMap["os"] = runtime.GOOS
kvMap["arch"] = runtime.GOARCH
kvMap["env"] = envMap
kvMap["$"] = func(command string) (string, error) {
output, err := execute(ctx, command, environmentToSlice(envMap))
if err != nil {
return "", fmt.Errorf("command failed: %v, output: %s", err, output)
}
return strings.TrimSpace(output), nil
}
return kvMap, nil
}
func execute(ctx context.Context, cmd string, envList []string) (string, error) {
if ctx == nil {
ctx = context.Background()
}
parser := syntax.NewParser()
reader := strings.NewReader(strings.TrimSpace(cmd))
prog, err := parser.Parse(reader, "")
if err != nil {
return "", fmt.Errorf("unable to parse command - %w", err)
}
if envList == nil {
envList = make([]string, 0)
}
envList = append(os.Environ(), envList...)
stdOutBuffer := &strings.Builder{}
stdErrBuffer := &strings.Builder{}
runner, err := interp.New(
interp.Env(expand.ListEnviron(envList...)),
interp.StdIO(
os.Stdin,
stdOutBuffer,
stdErrBuffer,
),
)
if err != nil {
return "", fmt.Errorf("unable to create runner - %w", err)
}
err = runner.Run(ctx, prog)
if err != nil {
var exitStatus interp.ExitStatus
if errors.As(err, &exitStatus) {
return stdErrBuffer.String(), fmt.Errorf("command exited with non-zero status %w", exitStatus)
}
return stdErrBuffer.String(), fmt.Errorf("encountered an error executing command - %w", err)
}
output := stdOutBuffer.String()
if stderr := stdErrBuffer.String(); stderr != "" {
output += "\n" + stderr
}
return strings.TrimSpace(output), nil
}
func environmentToSlice(env map[string]string) []string {
for k, v := range env {
if strings.Contains(v, "$") || strings.Contains(v, "{") {
env[k] = os.ExpandEnv(v)
}
}
var envSlice []string
for key, value := range env {
envSlice = append(envSlice, fmt.Sprintf("%s=%s", key, value))
}
return envSlice
}
func additionalFunctions() []expr.Option {
return []expr.Option{
// File existence and type checking
expr.Function("fileExists", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return false, fmt.Errorf("fileExists() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return false, fmt.Errorf("fileExists() requires string argument")
}
_, err := os.Stat(path)
return err == nil, nil
}),
expr.Function("dirExists", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return false, fmt.Errorf("dirExists() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return false, fmt.Errorf("dirExists() requires string argument")
}
info, err := os.Stat(path)
return err == nil && info.IsDir(), nil
}),
expr.Function("isFile", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return false, fmt.Errorf("isFile() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return false, fmt.Errorf("isFile() requires string argument")
}
info, err := os.Stat(path)
return err == nil && !info.IsDir(), nil
}),
expr.Function("isDir", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return false, fmt.Errorf("isDir() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return false, fmt.Errorf("isDir() requires string argument")
}
info, err := os.Stat(path)
return err == nil && info.IsDir(), nil
}),
// Path operations
expr.Function("basename", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return "", fmt.Errorf("basename() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return "", fmt.Errorf("basename() requires string argument")
}
return filepath.Base(path), nil
}),
expr.Function("dirname", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return "", fmt.Errorf("dirname() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return "", fmt.Errorf("dirname() requires string argument")
}
return filepath.Dir(path), nil
}),
// File content operations
expr.Function("readFile", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return "", fmt.Errorf("readFile() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return "", fmt.Errorf("readFile() requires string argument")
}
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}),
expr.Function("fileSize", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return int64(0), fmt.Errorf("fileSize() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return int64(0), fmt.Errorf("fileSize() requires string argument")
}
info, err := os.Stat(path)
if err != nil {
return int64(0), err
}
return info.Size(), nil
}),
// File time operations
expr.Function("fileModTime", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return time.Time{}, fmt.Errorf("fileModTime() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return time.Time{}, fmt.Errorf("fileModTime() requires string argument")
}
info, err := os.Stat(path)
if err != nil {
return time.Time{}, err
}
return info.ModTime(), nil
}),
expr.Function("fileAge", func(params ...interface{}) (interface{}, error) {
if len(params) != 1 {
return time.Duration(0), fmt.Errorf("fileAge() takes exactly 1 argument")
}
path, ok := params[0].(string)
if !ok {
return time.Duration(0), fmt.Errorf("fileAge() requires string argument")
}
info, err := os.Stat(path)
if err != nil {
return time.Duration(0), err
}
return time.Since(info.ModTime()), nil
}),
}
}