From ddaf2edb993da37650feea30dbcda4a5162a32a9 Mon Sep 17 00:00:00 2001 From: Sjoerd Simons Date: Fri, 10 Nov 2023 20:55:28 +0100 Subject: [PATCH] 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 --- actions/recipe.go | 14 +++++++++++++- go.mod | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/actions/recipe.go b/actions/recipe.go index ddc03c9..1befa16 100644 --- a/actions/recipe.go +++ b/actions/recipe.go @@ -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) diff --git a/go.mod b/go.mod index b7669e5..9b7589e 100644 --- a/go.mod +++ b/go.mod @@ -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