Add escape template helper function

To help using variables as part of shell command add a escape template
helper function which escapes the variable properly for shell usage

Signed-off-by: Sjoerd Simons <sjoerd@collabora.com>
This commit is contained in:
Sjoerd Simons
2023-11-10 20:55:28 +01:00
parent 1954affd5f
commit ddaf2edb99
2 changed files with 14 additions and 1 deletions

View File

@@ -29,7 +29,13 @@ Comments are allowed and should be prefixed with '#' symbol.
# Use value of variable 'Var' defined above
property2: {{$Var}}
Mandatory properties for receipt:
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 }}`
Mandatory properties for recipe:
- architecture -- target architecture
@@ -74,6 +80,7 @@ import (
"fmt"
"github.com/go-debos/debos"
"gopkg.in/yaml.v2"
"github.com/alessio/shellescape"
"path"
"text/template"
"log"
@@ -145,6 +152,10 @@ func sector(s int) int {
return s * 512
}
func escape(s string) string {
return shellescape.Quote(s)
}
func DumpActionStruct(iface interface{}) string {
var a []string
@@ -231,6 +242,7 @@ func (r *Recipe) Parse(file string, printRecipe bool, dump bool, templateVars ..
t := template.New(path.Base(file))
funcs := template.FuncMap{
"sector": sector,
"escape": escape,
}
t.Funcs(funcs)

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.15
require (
github.com/14rcole/gopopulate v0.0.0-20180821133914-b175b219e774 // indirect
github.com/alessio/shellescape v1.4.2
github.com/docker/go-units v0.5.0
github.com/go-debos/fakemachine v0.0.7
github.com/google/uuid v1.4.0