53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
/*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"text/template"
|
|
|
|
"github.com/mattn/go-shellwords"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func runTemplate(t *template.Template, env *TemplateEnv) (string, error) {
|
|
var buf strings.Builder
|
|
|
|
err := t.Execute(&buf, env)
|
|
if err != nil {
|
|
return "", fmt.Errorf("unable to parse exec template: %w", err)
|
|
}
|
|
|
|
return buf.String(), nil
|
|
}
|
|
|
|
func mkCmd(cmdline string) (*exec.Cmd, error) {
|
|
args, err := shellwords.Parse(cmdline)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to parse exec line: %w", err)
|
|
}
|
|
|
|
if len(args) < 1 {
|
|
return nil, fmt.Errorf("exec line evaluated as empty")
|
|
}
|
|
|
|
return exec.Command(args[0], args[1:]...), nil
|
|
}
|
|
|
|
func execRuleCmd(c *exec.Cmd, l *zap.Logger) {
|
|
err := c.Run()
|
|
if err != nil {
|
|
l.Named("exec").Warn(
|
|
"watch exec failed",
|
|
zap.String("cmd", c.String()),
|
|
zap.String("error", err.Error()),
|
|
)
|
|
}
|
|
}
|