Skip to content

Commit

Permalink
fix(cli): ensure TLACode works with EvalScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Duologic committed Jan 4, 2021
1 parent 21bdb9e commit cc14e40
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion cmd/tk/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"

"github.com/go-clix/cli"

Expand All @@ -24,7 +25,7 @@ func evalCmd() *cli.Command {
JsonnetOpts: getJsonnetOpts(),
}
if *evalPattern != "" {
jsonnetOpts.EvalScript = "(import '%s')." + *evalPattern
jsonnetOpts.EvalScript = fmt.Sprintf(tanka.PatternEvalScript, *evalPattern)
}
raw, err := tanka.Eval(args[0], jsonnetOpts)

Expand Down
24 changes: 22 additions & 2 deletions pkg/tanka/evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tanka

import (
"fmt"
"strings"

"github.com/pkg/errors"

Expand All @@ -18,7 +19,24 @@ func EvalJsonnet(path string, opts jsonnet.Opts) (raw string, err error) {

// evaluate Jsonnet
if opts.EvalScript != "" {
evalScript := fmt.Sprintf(opts.EvalScript, entrypoint)
var tla []string
for k, _ := range opts.TLACode {
tla = append(tla, k)
}
evalScript := fmt.Sprintf(`
local main = (import '%s');
%s
`, entrypoint, opts.EvalScript)

if len(tla) != 0 {
tlaJoin := strings.Join(tla, ", ")
evalScript = fmt.Sprintf(`
function(%s)
local main = (import '%s')(%s);
%s
`, tlaJoin, entrypoint, tlaJoin, opts.EvalScript)
}

raw, err = jsonnet.Evaluate(path, evalScript, opts)
if err != nil {
return "", errors.Wrap(err, "evaluating jsonnet")
Expand All @@ -33,6 +51,8 @@ func EvalJsonnet(path string, opts jsonnet.Opts) (raw string, err error) {
return raw, nil
}

const PatternEvalScript = "main.%s"

// EnvsOnlyEvalScript finds the Environment object (without its .data object)
const EnvsOnlyEvalScript = `
local noDataEnv(object) =
Expand Down Expand Up @@ -61,5 +81,5 @@ local noDataEnv(object) =
else {}
);
noDataEnv(import '%s')
noDataEnv(main)
`

0 comments on commit cc14e40

Please sign in to comment.