diff --git a/drone/jsonnet/jsonnet.go b/drone/jsonnet/jsonnet.go index 97f13bb..2418dd1 100644 --- a/drone/jsonnet/jsonnet.go +++ b/drone/jsonnet/jsonnet.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "os" + "path/filepath" "strings" "github.com/fatih/color" @@ -14,7 +15,6 @@ import ( "github.com/google/go-jsonnet" "github.com/urfave/cli" ) - // Command exports the jsonnet command. var Command = cli.Command{ Name: "jsonnet", @@ -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 } @@ -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 @@ -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) diff --git a/drone/jsonnet/jsonnet_test.go b/drone/jsonnet/jsonnet_test.go index 0dce888..54b66b0 100644 --- a/drone/jsonnet/jsonnet_test.go +++ b/drone/jsonnet/jsonnet_test.go @@ -14,6 +14,7 @@ func TestConvert(t *testing.T) { jsonnetFile, yamlFile string stringOutput, format, stream bool extVars []string + jpath []string }{ { name: "Stream + Format", @@ -21,6 +22,13 @@ func TestConvert(t *testing.T) { 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 { @@ -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) })