Skip to content

Commit

Permalink
drop all comments from yaml file before applying string substitutions
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Jul 22, 2021
1 parent efad24e commit 2747bbf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
23 changes: 21 additions & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,35 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.

func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
if !opts.SkipInterpolation {
substitute, err := opts.Interpolate.Substitute(string(b), template.Mapping(opts.Interpolate.LookupValue))
withoutComments, err := removeYamlComments(b)
if err != nil {
return nil, err
}
b = []byte(substitute)

substituted, err := opts.Interpolate.Substitute(string(withoutComments), template.Mapping(opts.Interpolate.LookupValue))
if err != nil {
return nil, err
}
b = []byte(substituted)
}

return ParseYAML(b)
}

// removeYamlComments drop all comments from the yaml file, so we don't try to apply string substitutions on irrelevant places
func removeYamlComments(b []byte) ([]byte, error) {
var cfg interface{}
err := yaml.Unmarshal(b, &cfg)
if err != nil {
return nil, err
}
b, err = yaml.Marshal(cfg)
if err != nil {
return nil, err
}
return b, nil
}

func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
extras := map[string]interface{}{}
for key, value := range dict {
Expand Down
1 change: 1 addition & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ services:
func TestLoadWithEnvironmentInterpolation(t *testing.T) {
home := "/home/foo"
config, err := loadYAMLWithEnv(`
# This is a comment, so using variable syntax here ${SHOULD_NOT_BREAK} parsing
services:
test:
image: busybox
Expand Down
2 changes: 1 addition & 1 deletion template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Mapping func(string) (string, bool)
// the substitution and an error.
type SubstituteFunc func(string, Mapping) (string, bool, error)

// SubstituteWith subsitute variables in the string with their values.
// SubstituteWith substitute variables in the string with their values.
// It accepts additional substitute function.
func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) {
var err error
Expand Down

0 comments on commit 2747bbf

Please sign in to comment.