Skip to content

Commit

Permalink
Add add & sub functions to template language
Browse files Browse the repository at this point in the history
Add helper functions "add" and "sub" to the templating language to help
with basic math functions addition and subtraction with two arguments.

These functions can be used for some basic calculations in recipes, e.g:

   {{ add 1 1 }} => 2,
   {{ sub 1 1 }} => 0,
   {{ sub (add 1 1) 1 }} => 1,

Signed-off-by: Christopher Obbard <[email protected]>
  • Loading branch information
obbardc committed Nov 24, 2023
1 parent 03db917 commit 6fa9d7b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions actions/recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ The following custom template functions are available
- sector: Returns the argument * 512 (convential sector size) e.g. `{{ sector 64 }}`
- escape: Shell escape the argument `{{ escape $var }}`
- decimal: Returns the integer argument as a decimal integer e.g. `{{ decimal 0x40 }} => 64`
- add: Returns the calculation of two integer arguments `a + b`, e.g. `{{ add 1 1 }} => 2`
- sub: Returns the calculation of two integer arguments `a - b`, e.g. `{{ sub 10 1 }} => 9`
Mandatory properties for recipe:
Expand Down Expand Up @@ -161,6 +163,14 @@ func decimal(i int) int {
return i
}

func add(a int, b int) int {
return a + b
}

func sub(a int, b int) int {
return a - b
}

func DumpActionStruct(iface interface{}) string {
var a []string

Expand Down Expand Up @@ -249,6 +259,8 @@ func (r *Recipe) Parse(file string, printRecipe bool, dump bool, templateVars ..
"sector": sector,
"escape": escape,
"decimal": decimal,
"add": add,
"sub": sub,
}
t.Funcs(funcs)

Expand Down

0 comments on commit 6fa9d7b

Please sign in to comment.