github.com/codefresh-io/kcfi@v0.0.0-20230301195427-c1578715cc46/pkg/action/helpers.go (about)

     1  /*
     2  Copyright The Codefresh Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package action
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"text/template"
    23  
    24  	"github.com/codefresh-io/kcfi/pkg/engine"
    25  	"sigs.k8s.io/yaml"
    26  )
    27  
    28  // ReadYamlFile - reads yaml file
    29  func ReadYamlFile(fileName string) (map[string]interface{}, error) {
    30  	fileB, err := ioutil.ReadFile(fileName)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	var yamlResult map[string]interface{}
    35  	if err := yaml.Unmarshal(fileB, &yamlResult); err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	return yamlResult, nil
    40  }
    41  
    42  // ExecuteTemplate - executes templates in tpl str with config as values
    43  func ExecuteTemplate(tplStr string, data interface{}) (string, error) {
    44  
    45  	template, err := template.New("base").Funcs(engine.FuncMap()).Parse(tplStr)
    46  	if err != nil {
    47  		return "", err
    48  	}
    49  
    50  	buf := bytes.NewBufferString("")
    51  	err = template.Execute(buf, data)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	return buf.String(), nil
    57  }
    58  
    59  // ExecuteTemplateToValues - exacutes template to map[string]interface{}
    60  func ExecuteTemplateToValues(tplStr string, data interface{}) (map[string]interface{}, error) {
    61  	tplResultS, err := ExecuteTemplate(tplStr, data)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	tplResult := map[string]interface{}{}
    66  	err = yaml.Unmarshal([]byte(tplResultS), &tplResult)
    67  	return tplResult, err
    68  }
    69  
    70  // MergeMaps - merges two map[string]interface{} into one
    71  func MergeMaps(a, b map[string]interface{}) map[string]interface{} {
    72  	out := make(map[string]interface{}, len(a))
    73  	for k, v := range a {
    74  		out[k] = v
    75  	}
    76  	for k, v := range b {
    77  		if v, ok := v.(map[string]interface{}); ok {
    78  			if bv, ok := out[k]; ok {
    79  				if bv, ok := bv.(map[string]interface{}); ok {
    80  					out[k] = MergeMaps(bv, v)
    81  					continue
    82  				}
    83  			}
    84  		}
    85  		out[k] = v
    86  	}
    87  	return out
    88  }