Select Git revision
triggers.go
triggers.go 2.29 KiB
package watcher
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"git.autistici.org/ai3/tools/replds2/common"
pb "git.autistici.org/ai3/tools/replds2/proto"
)
// The nullTriggerManager does nothing.
type nullTriggerManager struct{}
func (nullTriggerManager) Has(string) bool { return false }
func (nullTriggerManager) Notify(*common.NotifyBatch) {}
// The scriptTriggerManager runs user-configured trigger scripts.
type scriptTriggerManager map[string]*scriptTrigger
func (m scriptTriggerManager) Has(path string) bool {
_, ok := m[path]
return ok
}
func (m scriptTriggerManager) Notify(b *common.NotifyBatch) {
b.Apply(func(path string, nodes []*pb.Node) {
trigger := m[path]
if err := trigger.Run(nodes); err != nil {
log.Printf("trigger error: %v", err)
}
})
}
type scriptTrigger struct {
name string
Path string `json:"path"`
Command string `json:"command"`
}
func (t *scriptTrigger) Run(nodes []*pb.Node) error {
// Build an environment for the script.
changedEnvStr := "REPLDS_CHANGES="
for _, node := range nodes {
if !node.Deleted {
changedEnvStr += node.Path
}
}
env := os.Environ()
env = append(env, "REPLDS=1")
env = append(env, changedEnvStr)
// Run the command using the shell.
cmd := exec.Command("/bin/sh", "-c", t.Command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
cmd.Env = env
log.Printf("executing trigger '%s'", t.name)
return cmd.Run()
}