Commit 81377fa0 authored by Thomas Hahn's avatar Thomas Hahn
Browse files

Allow extvars to be populated from envvars

parent 9c493f1f
......@@ -90,10 +90,17 @@ func JsonnetVM(cmd *cobra.Command) (*jsonnet.VM, error) {
}
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)
switch len(kv) {
case 1:
v, present := os.LookupEnv(kv[0])
if present {
vm.ExtVar(kv[0], v)
} else {
return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
}
case 2:
vm.ExtVar(kv[0], kv[1])
}
vm.ExtVar(kv[0], kv[1])
}
resolver, err := buildResolver(cmd)
......
......@@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"reflect"
"testing"
......@@ -46,6 +47,7 @@ func TestShow(t *testing.T) {
"number": 42,
"string": "bar",
"notAVal": "aVal",
"notAnotherVal": "aVal2",
"array": ["one", 2, [3]],
"object": {"foo": "bar"}
}
......@@ -57,11 +59,15 @@ func TestShow(t *testing.T) {
t.Errorf("error parsing *expected* value: %s", err)
}
os.Setenv("anVar", "aVal2")
defer os.Unsetenv("anVar")
output := cmdOutput(t, []string{"show",
"-J", filepath.FromSlash("../testdata/lib"),
"-o", format,
filepath.FromSlash("../testdata/test.jsonnet"),
"-V", "aVar=aVal",
"-V", "anVar",
})
t.Log("output is", output)
......
local test = import "test.libsonnet";
local aVar = std.extVar("aVar");
local anVar = std.extVar("anVar");
{
apiVersion: "v1",
......@@ -8,6 +9,7 @@ local aVar = std.extVar("aVar");
test {
string: "bar",
notAVal : aVar,
notAnotherVal : anVar,
}
],
}
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