Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for jpath in jsonnet #224

Merged
merged 3 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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