@@ -4,30 +4,53 @@ import (
44 "github.com/sdsc-ordes/quitsh/pkg/exec"
55)
66
7- type EvalOption func (cmd * []string ) error
7+ type EvalOption func (* opts ) error
8+
9+ type opts struct {
10+ addArgs []string
11+ }
12+
13+ func (c * opts ) Apply (options ... EvalOption ) error {
14+ for _ , f := range options {
15+ if err := f (c ); err != nil {
16+ return err
17+ }
18+ }
19+ return nil
20+ }
821
922// WithEvalImpure uses `impure` evaluation on `Eval`.
1023func WithEvalImpure () EvalOption {
11- return func (cmd * [] string ) error {
12- ( * cmd ) = append (( * cmd ) , "--impure" )
24+ return func (o * opts ) error {
25+ o . addArgs = append (o . addArgs , "--impure" )
1326
1427 return nil
1528 }
1629}
1730
1831// WithEvalOutputRaw uses `raw` output on `Eval`.
1932func WithEvalOutputRaw () EvalOption {
20- return func (cmd * [] string ) error {
21- ( * cmd ) = append (( * cmd ) , "--raw" )
33+ return func (o * opts ) error {
34+ o . addArgs = append (o . addArgs , "--raw" )
2235
2336 return nil
2437 }
2538}
2639
27- // WithEvalOutputRaw uses JSON output on `Eval`.
40+ // WithEvalOutputJSON uses JSON output on `Eval`.
2841func WithEvalOutputJSON () EvalOption {
29- return func (cmd * []string ) error {
30- (* cmd ) = append ((* cmd ), "--json" )
42+ return func (o * opts ) error {
43+ o .addArgs = append (o .addArgs , "--json" )
44+
45+ return nil
46+ }
47+ }
48+
49+ // WithEvalNoCache uses no eval cache.
50+ // Note: Good when used in parallel to reduce SQLite contention.
51+ func WithEvalNoCache () EvalOption {
52+ return func (o * opts ) error {
53+ o .addArgs = append (o .addArgs , "--no-eval-cache" )
3154
3255 return nil
3356 }
@@ -38,16 +61,13 @@ func EvalTemplate(
3861 nixx * exec.CmdContext ,
3962 temp string ,
4063 data any ,
41- opts ... EvalOption ) (string , error ) {
64+ option ... EvalOption ) (string , error ) {
65+ var o opts
66+ o .Apply (option ... )
67+
4268 run := func (c * exec.CmdContext , file string ) (string , error ) {
4369 cmd := []string {"eval" , "--file" , file }
44-
45- for i := range opts {
46- e := opts [i ](& cmd )
47- if e != nil {
48- return "" , e
49- }
50- }
70+ cmd = append (cmd , o .addArgs ... )
5171
5272 return c .Get (cmd ... )
5373 }
0 commit comments