From 8e97a3adb5416f4daa7c5fb1be97786ceeece6fd Mon Sep 17 00:00:00 2001 From: Semyon Novikov Date: Tue, 24 Nov 2020 18:59:05 +0500 Subject: [PATCH] Even more useful now --- .gitignore | 2 +- cmd/amitm/main.go | 8 +++---- internal/{executor => amitm}/v1/executor.go | 23 +++++++++++---------- 3 files changed, 17 insertions(+), 16 deletions(-) rename internal/{executor => amitm}/v1/executor.go (74%) diff --git a/.gitignore b/.gitignore index 7533fc3..391e04f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -amitm \ No newline at end of file +/amitm \ No newline at end of file diff --git a/cmd/amitm/main.go b/cmd/amitm/main.go index e7ca2e5..b754676 100644 --- a/cmd/amitm/main.go +++ b/cmd/amitm/main.go @@ -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) } diff --git a/internal/executor/v1/executor.go b/internal/amitm/v1/executor.go similarity index 74% rename from internal/executor/v1/executor.go rename to internal/amitm/v1/executor.go index f477f91..3a47658 100644 --- a/internal/executor/v1/executor.go +++ b/internal/amitm/v1/executor.go @@ -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 } -- 2.30.1