github.com/verrazzano/verrazzano@v1.7.0/pkg/yaml/helm.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package yaml
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/verrazzano/verrazzano/pkg/bom"
    11  	"helm.sh/helm/v3/pkg/strvals"
    12  	"sigs.k8s.io/yaml"
    13  )
    14  
    15  // HelmValueFileConstructor creates a YAML file from a set of key value pairs
    16  func HelmValueFileConstructor(kvs []bom.KeyValue) (string, error) {
    17  	yamlObject := map[string]interface{}{}
    18  	for _, kv := range kvs {
    19  		// replace unwanted characters in the value to avoid splitting
    20  		ignoreChars := ",[.{}"
    21  		for _, char := range ignoreChars {
    22  			kv.Value = strings.Replace(kv.Value, string(char), "\\"+string(char), -1)
    23  		}
    24  
    25  		composedStr := fmt.Sprintf("%s=%s", kv.Key, kv.Value)
    26  		var err error
    27  		if kv.SetString {
    28  			err = strvals.ParseIntoString(composedStr, yamlObject)
    29  		} else {
    30  			err = strvals.ParseInto(composedStr, yamlObject)
    31  		}
    32  		if err != nil {
    33  			return "", err
    34  		}
    35  	}
    36  
    37  	yamlFile, err := yaml.Marshal(yamlObject)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return string(yamlFile), nil
    42  }