From 3cfe21af58990e0f0cc4b8bb03e9043f09f8771a Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 2 Oct 2021 18:29:07 -0300 Subject: [PATCH] Add `shellQuote` template function --- CHANGELOG.md | 3 +++ docs/usage.md | 3 +++ internal/templater/funcs.go | 6 +++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cad6c320..7fb5ea44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- A new `shellQuote` was added to the template system + (`{{shellQuote "a string"}}`) to ensure a string is safe for use in shell + ([mvdan/sh#727](https://github.com/mvdan/sh/pull/727), [mvdan/sh#737](https://github.com/mvdan/sh/pull/737), [Documentation](https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/syntax#Quote)) - In this version [mvdan.cc/sh](https://github.com/mvdan/sh) was upgraded with some small fixes and features - The `read -p` flag is now supported diff --git a/docs/usage.md b/docs/usage.md index 4b0fe773..607462e6 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -639,6 +639,9 @@ Task also adds the following functions: converts a string from `/` path format to `\`. - `exeExt`: Returns the right executable extension for the current OS (`".exe"` for Windows, `""` for others). +- `shellQuote`: Quotes a string to make it safe for use in shell scripts. + Task uses [this Go function](https://pkg.go.dev/mvdan.cc/sh/v3@v3.4.0/syntax#Quote) + for this. The Bash dialect is assumed. Example: diff --git a/internal/templater/funcs.go b/internal/templater/funcs.go index dd6017c9..20e9ed9b 100644 --- a/internal/templater/funcs.go +++ b/internal/templater/funcs.go @@ -6,7 +6,8 @@ import ( "strings" "text/template" - "github.com/go-task/slim-sprig" + sprig "github.com/go-task/slim-sprig" + "mvdan.cc/sh/v3/syntax" ) var ( @@ -37,6 +38,9 @@ func init() { } return "" }, + "shellQuote": func(str string) (string, error) { + return syntax.Quote(str, syntax.LangBash) + }, // IsSH is deprecated. "IsSH": func() bool { return true }, }