func()

in pkg/transform_regex_replace_expression.go [41:64]


func (r *RegexReplaceExpressionTransform) applyRegexReplace(body *hclwrite.Body, filename string, re *regexp.Regexp) error {
	var err error
	for name, attr := range body.Attributes() {
		oldValue := strings.TrimSpace(string(attr.Expr().BuildTokens(nil).Bytes()))
		newValue := re.ReplaceAllString(oldValue, r.Replacement)
		if oldValue == newValue {
			continue
		}
		tokens, diag := hclsyntax.LexExpression([]byte(newValue), filename, hcl.InitialPos)
		if diag.HasErrors() {
			err = multierror.Append(err, diag)
			continue
		}
		body.SetAttributeRaw(name, writerTokens(tokens))
	}

	for _, block := range body.Blocks() {
		if subErr := r.applyRegexReplace(block.Body(), filename, re); subErr != nil {
			err = multierror.Append(err, subErr)
			continue
		}
	}
	return err
}