Skip to content
Snippets Groups Projects
Commit 703328fb authored by Alex Clemmer's avatar Alex Clemmer Committed by GitHub
Browse files

Merge pull request #164 from jessicayuen/import-components

Construct base components Obj
parents fa6d1a69 c97498df
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,7 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"reflect"
"strings"
......@@ -58,6 +59,8 @@ const (
// environment or the -f flag.
flagFile = "file"
flagFileShort = "f"
componentsExtCodeKey = "__ksonnet/components"
)
var clientConfig clientcmd.ClientConfig
......@@ -369,8 +372,9 @@ func expandEnvCmdObjs(cmd *cobra.Command, envSpec *envSpec, cwd metadata.AbsPath
if err != nil {
return nil, err
}
baseObjExtCode := fmt.Sprintf("%s=%s", componentsExtCodeKey, constructBaseObj(fileNames))
expander.ExtCodes = append([]string{baseObjExtCode})
}
}
//
......@@ -379,3 +383,25 @@ func expandEnvCmdObjs(cmd *cobra.Command, envSpec *envSpec, cwd metadata.AbsPath
return expander.Expand(fileNames)
}
// constructBaseObj constructs the base Jsonnet object that represents k-v
// pairs of component name -> component imports. For example,
//
// {
// foo: import "components/foo.jsonnet"
// }
func constructBaseObj(paths []string) string {
var obj bytes.Buffer
obj.WriteString("{\n")
for _, p := range paths {
ext := path.Ext(p)
if path.Ext(p) != ".jsonnet" {
continue
}
name := strings.TrimSuffix(path.Base(p), ext)
fmt.Fprintf(&obj, " %s: import \"%s\",\n", name, p)
}
obj.WriteString("}\n")
return obj.String()
}
// Copyright 2017 The kubecfg authors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"testing"
)
func TestConstructBaseObj(t *testing.T) {
tests := []struct {
inputPaths []string
expected string
}{
// test simple case with 1 .jsonnet path
{
[]string{
"some/fake/path/foo.jsonnet",
},
`{
foo: import "some/fake/path/foo.jsonnet",
}
`,
},
// test multiple .jsonnet path case
{
[]string{
"some/fake/path/foo.jsonnet",
"another/fake/path/bar.jsonnet",
},
`{
foo: import "some/fake/path/foo.jsonnet",
bar: import "another/fake/path/bar.jsonnet",
}
`,
},
// test zero path case
{
[]string{},
`{
}
`,
},
// test non-jsonnet extension case
{
[]string{
"some/fake/path/foo.libsonnet",
"another/fake/path/bar.jsonnet",
},
`{
bar: import "another/fake/path/bar.jsonnet",
}
`,
},
}
for _, s := range tests {
res := constructBaseObj(s.inputPaths)
if res != s.expected {
t.Errorf("Wrong object constructed\n expected: %v\n got: %v", s.expected, res)
}
}
}
......@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Run me with `../kubecfg show kubecfg_test.jsonnet`
// Run me with `../kubecfg show -f kubecfg_test.jsonnet`
local kubecfg = import "kubecfg.libsonnet";
assert kubecfg.parseJson("[3, 4]") == [3, 4];
......
......@@ -20,6 +20,7 @@ type Expander struct {
ExtVarFiles []string
TlaVars []string
TlaVarFiles []string
ExtCodes []string
Resolver string
FailAction string
......@@ -112,6 +113,21 @@ func (spec *Expander) jsonnetVM() (*jsonnet.VM, error) {
vm.TlaVar(kv[0], string(v))
}
for _, extcode := range spec.ExtCodes {
kv := strings.SplitN(extcode, "=", 2)
switch len(kv) {
case 1:
v, present := os.LookupEnv(kv[0])
if present {
vm.ExtCode(kv[0], v)
} else {
return nil, fmt.Errorf("Missing environment variable: %s", kv[0])
}
case 2:
vm.ExtCode(kv[0], kv[1])
}
}
resolver, err := spec.buildResolver()
if err != nil {
return nil, err
......
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