github.com/tooploox/oya@v0.0.21-0.20230524103240-1cda1861aad6/pkg/oyafile/builtins.go (about)

     1  package oyafile
     2  
     3  import (
     4  	"fmt"
     5  	"unicode"
     6  
     7  	"github.com/Masterminds/sprig"
     8  	"github.com/tooploox/oya/pkg/template"
     9  	"github.com/tooploox/oya/pkg/types"
    10  )
    11  
    12  var sprigFunctions = upcaseFuncNames(sprig.GenericFuncMap())
    13  
    14  func (o *Oyafile) addBuiltIns() error {
    15  	o.Values = o.Values.Merge(o.defaultValues())
    16  	// See also plush helpers in pgk/template/plush.go
    17  	return nil
    18  }
    19  
    20  func (o *Oyafile) defaultValues() template.Scope {
    21  	scope := template.Scope{
    22  		"BasePath": o.Dir,
    23  	}
    24  
    25  	// Import sprig functions (http://masterminds.github.io/sprig/).
    26  	for name, f := range sprigFunctions {
    27  		_, exists := scope[name]
    28  		if exists {
    29  			panic(fmt.Sprintf("INTERNAL: Conflicting sprig function name: %q", name))
    30  		}
    31  		scope[name] = f
    32  	}
    33  
    34  	return scope
    35  }
    36  
    37  func aliasBuiltin(alias types.Alias) template.Scope {
    38  	return template.Scope{
    39  		"Import": template.Scope{
    40  			"Alias": string(alias),
    41  		},
    42  	}
    43  }
    44  
    45  func upcaseFuncNames(funcs map[string]interface{}) map[string]interface{} {
    46  	upcased := make(map[string]interface{})
    47  	for name, f := range funcs {
    48  		// We know we can cast the first byte to rune, these are function names.
    49  		upcasedName := string(unicode.ToUpper(rune(name[0]))) + name[1:]
    50  		upcased[upcasedName] = f
    51  	}
    52  	return upcased
    53  }