aboutsummaryrefslogtreecommitdiff
path: root/acid.go
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2024-04-16 07:38:23 +0200
committerPřemysl Eric Janouch <p@janouch.name>2024-04-16 08:30:25 +0200
commitb594ff78b22452b1260286f86fc5a40dbf3d38d9 (patch)
tree2155919c214082fe2e99728b6d0d0ac187d9fea8 /acid.go
parentfe81d713e1c59f2175974f0bc3eda5ff7a5f0749 (diff)
downloadacid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.tar.gz
acid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.tar.xz
acid-b594ff78b22452b1260286f86fc5a40dbf3d38d9.zip
Improve shell quoting
Diffstat (limited to 'acid.go')
-rw-r--r--acid.go27
1 files changed, 24 insertions, 3 deletions
diff --git a/acid.go b/acid.go
index b029c2a..2c59c38 100644
--- a/acid.go
+++ b/acid.go
@@ -93,10 +93,30 @@ func parseConfig(path string) error {
}
var err error
- gNotifyScript, err = ttemplate.New("notify").Parse(gConfig.Notify)
+ gNotifyScript, err =
+ ttemplate.New("notify").Funcs(shellFuncs).Parse(gConfig.Notify)
return err
}
+var shellFuncs = ttemplate.FuncMap{
+ "quote": func(word string) string {
+ // History expansion is annoying, don't let it cut us.
+ if strings.IndexRune(word, '!') >= 0 {
+ return "'" + strings.ReplaceAll(word, "'", `'"'"'`) + "'"
+ }
+
+ const special = "$`\"\\"
+ quoted := []rune{'"'}
+ for _, r := range word {
+ if strings.IndexRune(special, r) >= 0 {
+ quoted = append(quoted, '\\')
+ }
+ quoted = append(quoted, r)
+ }
+ return string(append(quoted, '"'))
+ },
+}
+
// --- Utilities ---------------------------------------------------------------
func giteaSign(b []byte) string {
@@ -910,8 +930,9 @@ func executorRunTask(ctx context.Context, task Task) error {
// - we might have to clone submodules as well.
// Otherwise, we could download a source archive from Gitea,
// and use SFTP to upload it to the runner.
- tmplScript, err := ttemplate.New("script").Parse(rt.Runner.Setup + "\n" +
- rt.ProjectRunner.Setup + "\n" + rt.ProjectRunner.Build)
+ tmplScript, err := ttemplate.New("script").Funcs(shellFuncs).
+ Parse(rt.Runner.Setup + "\n" +
+ rt.ProjectRunner.Setup + "\n" + rt.ProjectRunner.Build)
if err != nil {
return fmt.Errorf("script: %w", err)
}