~nvkv/amitm

8e97a3adb5416f4daa7c5fb1be97786ceeece6fd — Semyon Novikov a month ago bffe321
Even more useful now
3 files changed, 17 insertions(+), 16 deletions(-)

M .gitignore
M cmd/amitm/main.go
R internal/executor/v1/{executor.go => cutor.go}
M .gitignore => .gitignore +1 -1
@@ 1,1 1,1 @@
amitm
\ No newline at end of file
/amitm
\ No newline at end of file

M cmd/amitm/main.go => cmd/amitm/main.go +4 -4
@@ 4,8 4,8 @@ import (
	"log"

	"9fans.net/go/acme"
	"git.sr.ht/~nvkv/amitm/internal/amitm/v1"
	"git.sr.ht/~nvkv/amitm/internal/config/v1"
	"git.sr.ht/~nvkv/amitm/internal/executor/v1"
)

func main() {


@@ 26,11 26,11 @@ func main() {
		}

		rules, ok := config.RulesForAction(event.Op)
		matched := executor.Match(rules, event)
		matched := amitm.Match(rules, event)
		if ok {
			for _, rule := range matched {
				out, err := executor.Apply(rule, event.Op, event.Name)
				log.Printf("%s: %s\n", rule.Name, string(out))
				out, err := amitm.Apply(rule, event)
				log.Printf("%s:\n%s", rule.Name, string(out))
				if err != nil {
					log.Printf("error: %s\n", err)
				}

R internal/executor/v1/executor.go => internal/amitm/v1/executor.go +12 -11
@@ 1,4 1,4 @@
package executor
package amitm

import (
	"9fans.net/go/acme"


@@ 14,28 14,24 @@ func Match(rules []*config.Rule, event acme.LogEvent) []*config.Rule {
	var toApply []*config.Rule

	for _, rule := range rules {
		var applicable = false
		for _, glob := range rule.Globs {
			ok, _ := filepath.Match(glob, filepath.Base(event.Name))
			if ok {
				applicable = true
				toApply = append(toApply, rule)
				break
			}
		}
		if applicable {
			toApply = append(toApply, rule)
		}
	}
	return toApply
}

func Apply(rule *config.Rule, op, file string) ([]byte, error) {
	if op != rule.Action {
func Apply(rule *config.Rule, event acme.LogEvent) ([]byte, error) {
	if event.Op != rule.Action {
		return nil, fmt.Errorf(
			"action mismatch. Can't apply rule for %s to operation %s on file %s",
			rule.Action,
			op,
			file,
			event.Op,
			event.Name,
		)
	}



@@ 49,7 45,7 @@ func Apply(rule *config.Rule, op, file string) ([]byte, error) {
			copy(args, origArgs)

			for i, arg := range args {
				args[i] = strings.Replace(arg, "$file", file, -1)
				args[i] = strings.Replace(arg, "$file", event.Name, -1)
			}

			cmd := exec.Command(prog, args...)


@@ 60,5 56,10 @@ func Apply(rule *config.Rule, op, file string) ([]byte, error) {
			}
		}
	}
	w, err := acme.Open(event.ID, nil)
	if err != nil {
		return output, err
	}
	_ = w.Ctl("get")
	return output, nil
}