Skip to content

Commit 3cfe21a

Browse files
committed
Add shellQuote template function
1 parent b70a660 commit 3cfe21a

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- A new `shellQuote` was added to the template system
6+
(`{{shellQuote "a string"}}`) to ensure a string is safe for use in shell
7+
([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/[email protected]/syntax#Quote))
58
- In this version [mvdan.cc/sh](https://github.com/mvdan/sh) was upgraded
69
with some small fixes and features
710
- The `read -p` flag is now supported

docs/usage.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,9 @@ Task also adds the following functions:
639639
converts a string from `/` path format to `\`.
640640
- `exeExt`: Returns the right executable extension for the current OS
641641
(`".exe"` for Windows, `""` for others).
642+
- `shellQuote`: Quotes a string to make it safe for use in shell scripts.
643+
Task uses [this Go function](https://pkg.go.dev/mvdan.cc/sh/[email protected]/syntax#Quote)
644+
for this. The Bash dialect is assumed.
642645

643646
Example:
644647

internal/templater/funcs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import (
66
"strings"
77
"text/template"
88

9-
"github.com/go-task/slim-sprig"
9+
sprig "github.com/go-task/slim-sprig"
10+
"mvdan.cc/sh/v3/syntax"
1011
)
1112

1213
var (
@@ -37,6 +38,9 @@ func init() {
3738
}
3839
return ""
3940
},
41+
"shellQuote": func(str string) (string, error) {
42+
return syntax.Quote(str, syntax.LangBash)
43+
},
4044
// IsSH is deprecated.
4145
"IsSH": func() bool { return true },
4246
}

0 commit comments

Comments
 (0)