Skip to content
Snippets Groups Projects
Select Git revision
  • b4f12e2a35b0231314617206e84b3bee9ebf2134
  • master default protected
  • renovate/github.com-google-go-cmp-0.x
  • renovate/github.com-datadog-zstd-1.x
  • renovate/google.golang.org-protobuf-1.x
  • renovate/docker.io-library-golang-1.x
  • renovate/google.golang.org-grpc-1.x
  • renovate/github.com-prometheus-client_golang-1.x
  • renovate/golang.org-x-sync-0.x
  • renovate/github.com-grpc-ecosystem-go-grpc-middleware-2.x
  • renovate/github.com-grpc-ecosystem-go-grpc-middleware-v2-2.x
  • renovate/github.com-golang-protobuf-1.x
  • debian
13 results

triggers.go

Blame
  • 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()
    }