Skip to content

Commit

Permalink
Add support for jpath in jsonnet (#224)
Browse files Browse the repository at this point in the history
* Add support for jpath in jsonnet
Co-authored-by: TP Honey <[email protected]>
  • Loading branch information
rhiaxion authored Dec 6, 2022
1 parent 24bea58 commit 55e0add
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
23 changes: 20 additions & 3 deletions drone/jsonnet/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/ghodss/yaml"
"github.com/google/go-jsonnet"
"github.com/urfave/cli"
)

// Command exports the jsonnet command.
var Command = cli.Command{
Name: "jsonnet",
Expand Down Expand Up @@ -56,11 +56,22 @@ var Command = cli.Command{
Name: "extVar, V",
Usage: "Pass extVars to Jsonnet (can be specified multiple times)",
},
cli.StringSliceFlag{
Name: "jpath, J",
Usage: "Specify an additional library search dir (right-most wins)",
},
},
}

func generate(c *cli.Context) error {
result, err := convert(c.String("source"), c.Bool("string"), c.Bool("format"), c.Bool("stream"), c.StringSlice("extVar"))
result, err := convert(
c.String("source"),
c.Bool("string"),
c.Bool("format"),
c.Bool("stream"),
c.StringSlice("extVar"),
c.StringSlice("jpath"),
)
if err != nil {
return err
}
Expand All @@ -75,7 +86,7 @@ func generate(c *cli.Context) error {
return ioutil.WriteFile(target, []byte(result), 0644)
}

func convert(source string, stringOutput bool, format bool, stream bool, vars []string) (string, error) {
func convert(source string, stringOutput bool, format bool, stream bool, vars []string, jpath []string) (string, error) {
data, err := ioutil.ReadFile(source)
if err != nil {
return "", err
Expand All @@ -92,6 +103,12 @@ func convert(source string, stringOutput bool, format bool, stream bool, vars []
// register native functions
RegisterNativeFuncs(vm)

jsonnetPath := filepath.SplitList(os.Getenv("JSONNET_PATH"))
jsonnetPath = append(jsonnetPath, jpath...)
vm.Importer(&jsonnet.FileImporter{
JPaths: jsonnetPath,
})

// extVars
for _, v := range vars {
name, value, err := getVarVal(v)
Expand Down
10 changes: 9 additions & 1 deletion drone/jsonnet/jsonnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ func TestConvert(t *testing.T) {
jsonnetFile, yamlFile string
stringOutput, format, stream bool
extVars []string
jpath []string
}{
{
name: "Stream + Format",
jsonnetFile: "stream_format.jsonnet",
yamlFile: "stream_format.yaml",
format: true, stream: true,
},
{
name: "Jsonnet Path",
jsonnetFile: "stream_format.jsonnet",
yamlFile: "stream_format.yaml",
format: true, stream: true,
jpath: []string{"/path/to/jsonnet/lib"},
},
}

for _, tc := range testcases {
Expand All @@ -29,7 +37,7 @@ func TestConvert(t *testing.T) {
expected, err := os.ReadFile(filepath.Join("./testdata", tc.yamlFile))
assert.NoError(t, err)

result, err := convert(filepath.Join("./testdata", tc.jsonnetFile), tc.stringOutput, tc.format, tc.stream, tc.extVars)
result, err := convert(filepath.Join("./testdata", tc.jsonnetFile), tc.stringOutput, tc.format, tc.stream, tc.extVars, tc.jpath)
assert.NoError(t, err)
assert.Equal(t, string(expected), result)
})
Expand Down

0 comments on commit 55e0add

Please sign in to comment.