feat(templater): override sprig default function with guarded alternative
These changes overrides the `default` function provided by sprig with an alternative to retrieve variable values from variables that might not have been declared at all. Referencing a variable in a template that is not declared, will lead to the underlying templating functionality raising an error, causing kontemplate to exit. The override alternative to `default` accepts a second string argument with the variable name. If the variable in question has not been declared the first argument's value would be returned, just as the original `default` function does.
This commit is contained in:
		
							parent
							
								
									6b39254917
								
							
						
					
					
						commit
						ab059ad41c
					
				
					 1 changed files with 6 additions and 0 deletions
				
			
		| 
						 | 
					@ -203,7 +203,13 @@ func templateFuncs(c *context.Context, rs *context.ResourceSet) template.FuncMap
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return string(data), nil
 | 
							return string(data), nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						m["default"] = func(defaultVal interface{}, varName string) interface{} {
 | 
				
			||||||
 | 
							if val, ok := rs.Values[varName]; ok {
 | 
				
			||||||
 | 
								return val
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							return defaultVal
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return m
 | 
						return m
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue