-
Notifications
You must be signed in to change notification settings - Fork 78
/
op_file.go
108 lines (85 loc) · 2.77 KB
/
op_file.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
package spruce
import (
"fmt"
"os"
"path/filepath"
"github.com/starkandwayne/goutils/ansi"
"github.com/starkandwayne/goutils/tree"
. "github.com/geofffranks/spruce/log"
)
// FileOperator ...
type FileOperator struct{}
// Setup ...
func (FileOperator) Setup() error {
return nil
}
// Phase ...
func (FileOperator) Phase() OperatorPhase {
return EvalPhase
}
// Dependencies ...
func (FileOperator) Dependencies(_ *Evaluator, _ []*Expr, _ []*tree.Cursor, auto []*tree.Cursor) []*tree.Cursor {
return auto
}
// Run ...
func (FileOperator) Run(ev *Evaluator, args []*Expr) (*Response, error) {
DEBUG("running (( file ... )) operation at $.%s", ev.Here)
defer DEBUG("done with (( file ... )) operation at $%s\n", ev.Here)
if len(args) != 1 {
return nil, fmt.Errorf("file operator requires exactly one string or reference argument")
}
var fname string
fbasepath := os.Getenv("SPRUCE_FILE_BASE_PATH")
arg := args[0]
i := 0
v, err := arg.Resolve(ev.Tree)
if err != nil {
DEBUG(" arg[%d]: failed to resolve expression to a concrete value", i)
DEBUG(" [%d]: error was: %s", i, err)
return nil, err
}
switch v.Type {
case Literal:
DEBUG(" arg[%d]: using string literal '%v'", i, v.Literal)
DEBUG(" [%d]: appending '%v' to resultant string", i, v.Literal)
fname = fmt.Sprintf("%v", v.Literal)
case Reference:
DEBUG(" arg[%d]: trying to resolve reference $.%s", i, v.Reference)
s, err := v.Reference.Resolve(ev.Tree)
if err != nil {
DEBUG(" [%d]: resolution failed\n error: %s", i, err)
return nil, fmt.Errorf("Unable to resolve `%s`: %s", v.Reference, err)
}
switch s.(type) {
case map[interface{}]interface{}:
DEBUG(" arg[%d]: %v is not a string scalar", i, s)
return nil, ansi.Errorf("@R{tried to read file} @c{%s}@R{, which is not a string scalar}", v.Reference)
case []interface{}:
DEBUG(" arg[%d]: %v is not a string scalar", i, s)
return nil, ansi.Errorf("@R{tried to read file} @c{%s}@R{, which is not a string scalar}", v.Reference)
default:
DEBUG(" [%d]: appending '%s' to resultant string", i, s)
fname = fmt.Sprintf("%v", s)
}
default:
DEBUG(" arg[%d]: I don't know what to do with '%v'", i, arg)
return nil, fmt.Errorf("file operator only accepts string literals and key reference argument")
}
DEBUG("")
if !filepath.IsAbs(fname) {
fname = filepath.Join(fbasepath, fname)
}
contents, err := os.ReadFile(fname)
if err != nil {
DEBUG(" File %s cannot be read: %s", fname, err)
return nil, ansi.Errorf("@R{tried to read file} @c{%s}@R{: could not be read - %s}", fname, err)
}
DEBUG(" resolved (( file ... )) operation to the string:\n \"%s\"", string(contents))
return &Response{
Type: Replace,
Value: string(contents),
}, nil
}
func init() {
RegisterOp("file", FileOperator{})
}