Debian Codegraph @

execution/func_readfile.go

raw symbols SCIP
1 package execution
2
3 import (
4 "context"
5 "fmt"
6 "github.com/tomwright/dasel/v3/model"
7 "io"
8 "os"
9 )
10
11 // FuncReadFile reads the given filepath at runtime.
12 var FuncReadFile = NewFunc(
13 "readFile",
14 func(ctx context.Context, data *model.Value, args model.Values) (*model.Value, error) {
15 filepath, err := args[0].StringValue()
16 if err != nil {
17 return nil, fmt.Errorf("readFile: %w", err)
18 }
19
20 f, err := os.Open(filepath)
21 if err != nil {
22 return nil, fmt.Errorf("readFile: %w", err)
23 }
24 defer func() {
25 _ = f.Close()
26 }()
27
28 fileBytes, err := io.ReadAll(f)
29 if err != nil {
30 return nil, fmt.Errorf("readFile: %w", err)
31 }
32
33 return model.NewStringValue(string(fileBytes)), nil
34 },
35 ValidateArgsExactly(1),
36 )