Skip to content

Commit

Permalink
Allow ":" in data values flags
Browse files Browse the repository at this point in the history
- Move test
  • Loading branch information
cari-lynn committed Mar 15, 2022
1 parent 7bc4d7c commit d3718ee
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 100 deletions.
69 changes: 0 additions & 69 deletions pkg/cmd/template/cmd_data_values_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,75 +118,6 @@ array:
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValuesFileFlagsWithLibRefPath(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
#@ load("@ytt:library", "library")
#@ load("@ytt:template", "template")
---
fromRoot: #@ data.values
#@ lib = library.get("lib")
--- #@ template.replace(lib.eval())`)

libYamlTplData := []byte(`#@ load("@ytt:data", "data")
---
fromLibrary: #@ data.values`)

dvs1 := []byte(`val1: 1`)

dvs2 := []byte(`val2: 2`)

dvs3 := []byte(`val3: 3`)

dvs4 := []byte(`4`)

expectedYAMLTplData := `fromRoot:
val1: 1
---
fromLibrary:
val2: 2
val3: 3
val4: "4"
`

filesToProcess := files.NewSortedFiles([]*files.File{
files.MustNewFileFromSource(files.NewBytesSource("tpl.yml", yamlTplData)),
files.MustNewFileFromSource(files.NewBytesSource("_ytt_lib/lib/tpl.yml", libYamlTplData)),
})

ui := ui.NewTTY(false)
opts := cmdtpl.NewOptions()

opts.DataValuesFlags = cmdtpl.DataValuesFlags{
FromFiles: []string{"c:\\User\\user\\dvs1.yml", "@lib:dvs2.yml", "@lib:D:\\User\\user\\dvs3.yml"},
KVsFromFiles: []string{"@lib:val4=c:\\User\\user\\dvs4.yml"},
ReadFileFunc: func(path string) ([]byte, error) {
switch path {
case "c:\\User\\user\\dvs1.yml":
return dvs1, nil
case "dvs2.yml":
return dvs2, nil
case "D:\\User\\user\\dvs3.yml":
return dvs3, nil
case "c:\\User\\user\\dvs4.yml":
return dvs4, nil
default:
return nil, fmt.Errorf("Unknown file '%s'", path)
}
},
}

out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui)
require.NoError(t, out.Err)
require.Len(t, out.Files, 1, "unexpected number of output files")

file := out.Files[0]

assert.Equal(t, "tpl.yml", file.RelativePath())
assert.Equal(t, expectedYAMLTplData, string(file.Bytes()))
}

func TestDataValuesWithDataValuesFileFlagsForbiddenComment(t *testing.T) {
yamlTplData := []byte(`
#@ load("@ytt:data", "data")
Expand Down
53 changes: 45 additions & 8 deletions pkg/cmd/template/cmd_data_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package template_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -183,39 +184,58 @@ func TestDataValuesWithLibraryAttachedFlags(t *testing.T) {
tplBytes := []byte(`
#@ load("@ytt:library", "library")
#@ load("@ytt:template", "template")
#@ load("@ytt:data", "data")
root: #@ data.values
--- #@ template.replace(library.get("lib", alias="inst1").eval())`)

libTplBytes := []byte(`
#@ load("@ytt:library", "library")
#@ load("@ytt:template", "template")
#@ load("@ytt:data", "data")
lib-val: #@ data.values.lib_val
from_library: #@ data.values
--- #@ template.replace(library.get("nested-lib").eval())
`)

libValuesBytes := []byte(`
#@data/values
---
lib_val: override-me
val0: override-me
`)

nestedLibTplBytes := []byte(`
#@ load("@ytt:data", "data")
nested-lib-val: #@ data.values.nested_lib_val
from_nested_lib: #@ data.values
`)

nestedLibValuesBytes := []byte(`
#@data/values
---
nested_lib_val: override-me
val1: override-me
`)

expectedYAMLTplData := `lib-val: test
dvs2 := []byte(`val2: 2`)

dvs3 := []byte(`val3: 3`)

dvs4 := []byte(`val4: 4`)

dvs6 := []byte(`6`)

expectedYAMLTplData := `root:
val2: 2
---
nested-lib-val: passes
from_library:
val0: 0
val3: 3
val4: 4
val5: "5"
val6: "6"
---
from_nested_lib:
val1: 1
`

filesToProcess := files.NewSortedFiles([]*files.File{
Expand All @@ -230,8 +250,25 @@ nested-lib-val: passes
opts := cmdtpl.NewOptions()

opts.DataValuesFlags = cmdtpl.DataValuesFlags{
// TODO env and files?
KVsFromYAML: []string{"@~inst1:lib_val=test", "@~inst1@nested-lib:nested_lib_val=passes"},
KVsFromYAML: []string{"@~inst1:val0=0", "@~inst1@nested-lib:val1=1"},
FromFiles: []string{"c:\\User\\user\\dvs2.yml", "@~inst1:dvs3.yml", "@lib:D:\\User\\user\\dvs4.yml"},
EnvFromStrings: []string{"@lib::DVS"},
EnvironFunc: func() []string { return []string{"DVS_val5=5"} },
KVsFromFiles: []string{"@lib:val6=c:\\User\\user\\dvs6.yml"},
ReadFileFunc: func(path string) ([]byte, error) {
switch path {
case "c:\\User\\user\\dvs2.yml":
return dvs2, nil
case "dvs3.yml":
return dvs3, nil
case "D:\\User\\user\\dvs4.yml":
return dvs4, nil
case "c:\\User\\user\\dvs6.yml":
return dvs6, nil
default:
return nil, fmt.Errorf("Unknown file '%s'", path)
}
},
}

out := opts.RunWithFiles(cmdtpl.Input{Files: filesToProcess}, ui)
Expand Down
41 changes: 18 additions & 23 deletions pkg/cmd/template/data_values_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ func (s *DataValuesFlags) AsOverlays(strict bool) ([]*datavalues.Envelope, []*da
return overlayValues, libraryOverlays, nil
}

func (s *DataValuesFlags) file(path string, strict bool) ([]*datavalues.Envelope, error) {
libRef, path, err := s.libraryRefAndKey(path)
func (s *DataValuesFlags) file(fullPath string, strict bool) ([]*datavalues.Envelope, error) {
libRef, path, err := s.libraryRefAndKey(fullPath)
if err != nil {
return nil, err
}

contents, err := s.readFile(path)
if err != nil {
return nil, fmt.Errorf("Reading file '%s'", path)
return nil, fmt.Errorf("Unable to read file '%s' from flag value '%s'", path, fullPath)
}

docSetOpts := yamlmeta.DocSetOpts{
Expand Down Expand Up @@ -274,33 +274,28 @@ func (s *DataValuesFlags) kvFile(kv string) (*datavalues.Envelope, error) {
return datavalues.NewEnvelopeWithLibRef(overlay, libRef)
}

// libraryRefAndKey separates a library reference and a key.
// A library reference starts with ref.LibrarySep, and ends with the first occurrence of libraryPathSep.
func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) {
const (
libraryKeySep = ":"
libraryPathSep = ":"
)

keyPieces := strings.Split(key, libraryKeySep)

switch len(keyPieces) {
case 1:
return "", key, nil

case 2:
if len(keyPieces[0]) == 0 {
return "", "", fmt.Errorf("Expected library ref to not be empty")
}
if strings.HasPrefix(keyPieces[0], ref.LibrarySep) {
if strings.HasPrefix(key, ref.LibrarySep) {
keyPieces := strings.SplitN(key, libraryPathSep, 2)
switch len(keyPieces) {
case 1:
return "", key, nil
case 2:
if len(keyPieces[0]) == 1 {
return "", "", fmt.Errorf("Expected library ref to not be empty")
}
return keyPieces[0], keyPieces[1], nil
}
// the LibrarySep is part of the path
return "", key, nil
case 3:
if strings.HasPrefix(keyPieces[0], ref.LibrarySep) {
// the second LibrarySep is part of the path
return keyPieces[0], keyPieces[1] + libraryKeySep + keyPieces[2], nil
}
}
return "", "", fmt.Errorf("Expected at most one library-key separator '%s' in '%s'", libraryKeySep, key)

return "", key, nil

}

func (s *DataValuesFlags) buildOverlay(keyPieces []string, value interface{}, desc string, line string) *yamlmeta.Document {
Expand Down

0 comments on commit d3718ee

Please sign in to comment.