Commit c4e5813a authored by Angus Lees's avatar Angus Lees Committed by GitHub
Browse files

Merge pull request #28 from GauntletWizard/extvar

Add a flag for extvars
parents 08eabe68 518c888d
......@@ -7,6 +7,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
......@@ -21,13 +22,15 @@ import (
)
const (
flagJpath = "jpath"
flagJpath = "jpath"
flagExtVar = "extVar"
)
var clientConfig clientcmd.ClientConfig
func init() {
RootCmd.PersistentFlags().StringP(flagJpath, "J", "", "Additional jsonnet library search path")
RootCmd.PersistentFlags().StringSliceP(flagExtVar, "V", nil, "Values of external variables")
// The "usual" clientcmd/kubectl flags
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
......@@ -76,6 +79,18 @@ func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
vm.JpathAdd(p)
}
extvars, err := flags.GetStringSlice(flagExtVar)
if err != nil {
return nil, err
}
for _, extvar := range extvars {
kv := strings.SplitN(extvar, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("Failed to parse extvar: missing '=' in %s", extvar)
}
vm.ExtVar(kv[0], kv[1])
}
return vm, nil
}
......
......@@ -45,6 +45,7 @@ func TestShow(t *testing.T) {
"bool": true,
"number": 42,
"string": "bar",
"notAVal": "aVal",
"array": ["one", 2, [3]],
"object": {"foo": "bar"}
}
......@@ -60,6 +61,7 @@ func TestShow(t *testing.T) {
"-J", filepath.FromSlash("../testdata/lib"),
"-o", format,
filepath.FromSlash("../testdata/test.jsonnet"),
"-V", "aVar=aVal",
})
t.Log("output is", output)
......
local test = import "test.libsonnet";
local aVar = std.extVar("aVar");
{
apiVersion: "v1",
......@@ -6,6 +7,7 @@ local test = import "test.libsonnet";
items: [
test {
string: "bar",
notAVal : aVar,
}
],
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment