github.com/appscode/helm@v3.0.0-alpha.1+incompatible/pkg/engine/funcs.go (about)

     1  /*
     2  Copyright The Helm 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 engine
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"strings"
    23  	"text/template"
    24  
    25  	"github.com/BurntSushi/toml"
    26  	"github.com/Masterminds/sprig"
    27  	"github.com/pkg/errors"
    28  	yaml "gopkg.in/yaml.v2"
    29  )
    30  
    31  // funcMap returns a mapping of all of the functions that Engine has.
    32  //
    33  // Because some functions are late-bound (e.g. contain context-sensitive
    34  // data), the functions may not all perform identically outside of an Engine
    35  // as they will inside of an Engine.
    36  //
    37  // Known late-bound functions:
    38  //
    39  //	- "include"
    40  //	- "tpl"
    41  //
    42  // These are late-bound in Engine.Render().  The
    43  // version included in the FuncMap is a placeholder.
    44  //
    45  func funcMap() template.FuncMap {
    46  	f := sprig.TxtFuncMap()
    47  	delete(f, "env")
    48  	delete(f, "expandenv")
    49  
    50  	// Add some extra functionality
    51  	extra := template.FuncMap{
    52  		"toToml":   toTOML,
    53  		"toYaml":   toYAML,
    54  		"fromYaml": fromYAML,
    55  		"toJson":   toJSON,
    56  		"fromJson": fromJSON,
    57  		"required": required,
    58  
    59  		// This is a placeholder for the "include" function, which is
    60  		// late-bound to a template. By declaring it here, we preserve the
    61  		// integrity of the linter.
    62  		"include": func(string, interface{}) string { return "not implemented" },
    63  		"tpl":     func(string, interface{}) interface{} { return "not implemented" },
    64  	}
    65  
    66  	for k, v := range extra {
    67  		f[k] = v
    68  	}
    69  
    70  	return f
    71  }
    72  
    73  func required(warn string, val interface{}) (interface{}, error) {
    74  	if val == nil {
    75  		return val, errors.Errorf(warn)
    76  	} else if _, ok := val.(string); ok {
    77  		if val == "" {
    78  			return val, errors.Errorf(warn)
    79  		}
    80  	}
    81  	return val, nil
    82  }
    83  
    84  // toYAML takes an interface, marshals it to yaml, and returns a string. It will
    85  // always return a string, even on marshal error (empty string).
    86  //
    87  // This is designed to be called from a template.
    88  func toYAML(v interface{}) string {
    89  	data, err := yaml.Marshal(v)
    90  	if err != nil {
    91  		// Swallow errors inside of a template.
    92  		return ""
    93  	}
    94  	return strings.TrimSuffix(string(data), "\n")
    95  }
    96  
    97  // fromYAML converts a YAML document into a map[string]interface{}.
    98  //
    99  // This is not a general-purpose YAML parser, and will not parse all valid
   100  // YAML documents. Additionally, because its intended use is within templates
   101  // it tolerates errors. It will insert the returned error message string into
   102  // m["Error"] in the returned map.
   103  func fromYAML(str string) map[string]interface{} {
   104  	m := map[string]interface{}{}
   105  
   106  	if err := yaml.Unmarshal([]byte(str), &m); err != nil {
   107  		m["Error"] = err.Error()
   108  	}
   109  	return m
   110  }
   111  
   112  // toTOML takes an interface, marshals it to toml, and returns a string. It will
   113  // always return a string, even on marshal error (empty string).
   114  //
   115  // This is designed to be called from a template.
   116  func toTOML(v interface{}) string {
   117  	b := bytes.NewBuffer(nil)
   118  	e := toml.NewEncoder(b)
   119  	err := e.Encode(v)
   120  	if err != nil {
   121  		return err.Error()
   122  	}
   123  	return b.String()
   124  }
   125  
   126  // toJSON takes an interface, marshals it to json, and returns a string. It will
   127  // always return a string, even on marshal error (empty string).
   128  //
   129  // This is designed to be called from a template.
   130  func toJSON(v interface{}) string {
   131  	data, err := json.Marshal(v)
   132  	if err != nil {
   133  		// Swallow errors inside of a template.
   134  		return ""
   135  	}
   136  	return string(data)
   137  }
   138  
   139  // fromJSON converts a JSON document into a map[string]interface{}.
   140  //
   141  // This is not a general-purpose JSON parser, and will not parse all valid
   142  // JSON documents. Additionally, because its intended use is within templates
   143  // it tolerates errors. It will insert the returned error message string into
   144  // m["Error"] in the returned map.
   145  func fromJSON(str string) map[string]interface{} {
   146  	m := make(map[string]interface{})
   147  
   148  	if err := json.Unmarshal([]byte(str), &m); err != nil {
   149  		m["Error"] = err.Error()
   150  	}
   151  	return m
   152  }