Skip to content

Commit

Permalink
Only allow ':' in data value file flags
Browse files Browse the repository at this point in the history
- exclude env flags or key-value flags
  • Loading branch information
cari-lynn committed Mar 16, 2022
1 parent d3718ee commit 235813f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkg/cmd/template/cmd_data_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ from_nested_lib:
opts := cmdtpl.NewOptions()

opts.DataValuesFlags = cmdtpl.DataValuesFlags{
KVsFromYAML: []string{"@~inst1:val0=0", "@~inst1@nested-lib:val1=1"},
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"},
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) {
Expand All @@ -262,7 +262,7 @@ from_nested_lib:
case "dvs3.yml":
return dvs3, nil
case "D:\\User\\user\\dvs4.yml":
return dvs4, nil
return dvs4, nil
case "c:\\User\\user\\dvs6.yml":
return dvs6, nil
default:
Expand Down
30 changes: 18 additions & 12 deletions pkg/cmd/template/data_values_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

const (
dvsKVSep = "="
dvsMapKeySep = "."
dvsKVSep = "="
dvsMapKeySep = "."
libraryKeySep = ":"
)

type DataValuesFlags struct {
Expand Down Expand Up @@ -188,7 +189,7 @@ func (s *DataValuesFlags) env(prefix string, src dataValuesFlagsSource) ([]*data
envVars = s.EnvironFunc()
}

libRef, keyPrefix, err := s.libraryRefAndKey(prefix)
libRef, keyPrefix, err := s.libraryRefAndKeyStrict(prefix)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -235,7 +236,7 @@ func (s *DataValuesFlags) kv(kv string, src dataValuesFlagsSource) (*datavalues.
return nil, fmt.Errorf("Deserializing value for key '%s': %s", pieces[0], err)
}

libRef, key, err := s.libraryRefAndKey(pieces[0])
libRef, key, err := s.libraryRefAndKeyStrict(pieces[0])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,16 +274,23 @@ func (s *DataValuesFlags) kvFile(kv string) (*datavalues.Envelope, error) {

return datavalues.NewEnvelopeWithLibRef(overlay, libRef)
}
func (DataValuesFlags) libraryRefAndKeyStrict(key string) (string, string, error) {
libRef, key, err := DataValuesFlags{}.libraryRefAndKey(key)
if err != nil {
return "", "", err
}
if len(strings.Split(key, libraryKeySep)) > 1 {
// error on a common syntax mistake
return "", "", fmt.Errorf("Expected at most one library-key separator '%s' in '%s'", libraryKeySep, key)
}
return libRef, key, nil
}

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

if strings.HasPrefix(key, ref.LibrarySep) {
keyPieces := strings.SplitN(key, libraryPathSep, 2)
keyPieces := strings.SplitN(key, libraryKeySep, 2)
switch len(keyPieces) {
case 1:
return "", key, nil
Expand All @@ -293,9 +301,7 @@ func (DataValuesFlags) libraryRefAndKey(key string) (string, string, error) {
return keyPieces[0], keyPieces[1], nil
}
}

return "", key, nil

}

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

0 comments on commit 235813f

Please sign in to comment.