1
0
mirror of https://github.com/drone/drone-cli.git synced 2024-11-22 17:01:58 +01:00

Add support for jpath in jsonnet (#224)

* Add support for jpath in jsonnet
Co-authored-by: TP Honey <tp@harness.io>
This commit is contained in:
Sean Ryan 2022-12-06 12:30:36 +00:00 committed by GitHub
parent 24bea58512
commit 55e0addb31
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 4 deletions

@ -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)

@ -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)
})