From 896b28188b858c1465d0ca1ede786ff9f9c46f78 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Wed, 14 Sep 2022 23:29:54 +0200 Subject: [PATCH] with action executor --- _examples/actions/write_a_readme/main.go | 5 ++- cmd/kraken/commands/process.go | 4 +-- cmd/server/server.go | 2 ++ internal/actions/action.go | 20 ++++++++++- internal/actions/action_creator.go | 4 +-- internal/actions/builders/go.go | 46 ++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 internal/actions/builders/go.go diff --git a/_examples/actions/write_a_readme/main.go b/_examples/actions/write_a_readme/main.go index a5bd52f..5c24133 100644 --- a/_examples/actions/write_a_readme/main.go +++ b/_examples/actions/write_a_readme/main.go @@ -3,7 +3,10 @@ package main import "github.com/bitfield/script" func main() { - script. + _, err := script. Echo("# Readme"). WriteFile("README.md") + if err != nil { + panic(err) + } } diff --git a/cmd/kraken/commands/process.go b/cmd/kraken/commands/process.go index 193c085..274b299 100644 --- a/cmd/kraken/commands/process.go +++ b/cmd/kraken/commands/process.go @@ -11,7 +11,7 @@ import ( func CreateKrakenProcessCmd() *cobra.Command { cmd := &cobra.Command{ Use: "process", - Run: func(cmd *cobra.Command, args []string) { + Run: func(cmd *cobra.Command, _ []string) { client := http.Client{} var buf bytes.Buffer @@ -22,7 +22,7 @@ func CreateKrakenProcessCmd() *cobra.Command { Path string `json:"path"` }{ Repository: "git@git.front.kjuulh.io:kjuulh/kraken.git", - Branch: "v0.1", + Branch: "feature/add-actions", Path: "_examples/actions/write_a_readme/", }) if err != nil { diff --git a/cmd/server/server.go b/cmd/server/server.go index 49b713c..5c835ec 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -15,6 +15,8 @@ func main() { } _ = logger.Sync() + zap.ReplaceGlobals(logger) + Execute(logger) } diff --git a/internal/actions/action.go b/internal/actions/action.go index 3d7e6aa..277b790 100644 --- a/internal/actions/action.go +++ b/internal/actions/action.go @@ -2,15 +2,33 @@ package actions import ( "context" + "errors" + "git.front.kjuulh.io/kjuulh/kraken/internal/actions/builders" "git.front.kjuulh.io/kjuulh/kraken/internal/schema" "git.front.kjuulh.io/kjuulh/kraken/internal/services/storage" + "go.uber.org/zap" ) type Action struct { - Schema *schema.KrakenSchema + Schema *schema.KrakenSchema + SchemaPath string } func (a *Action) Execute(ctx context.Context, area *storage.Area) error { + for _, action := range a.Schema.Actions { + switch action.Type { + case "go": + exe, err := builders.NewGo(zap.L()).Build(ctx, a.SchemaPath, action.Entry) + if err != nil { + return err + } + exe(ctx, area.Path) + + default: + return errors.New("could not determine action type") + } + } + return nil } diff --git a/internal/actions/action_creator.go b/internal/actions/action_creator.go index c26517c..b731519 100644 --- a/internal/actions/action_creator.go +++ b/internal/actions/action_creator.go @@ -70,9 +70,9 @@ func (ac *ActionCreator) Prepare(ctx context.Context, ops *ActionCreatorOps) (*A } ac.logger.Debug("Action creator done") - return &Action{ - Schema: krakenSchema, + Schema: krakenSchema, + SchemaPath: executorUrl, }, nil } diff --git a/internal/actions/builders/go.go b/internal/actions/builders/go.go new file mode 100644 index 0000000..508eb94 --- /dev/null +++ b/internal/actions/builders/go.go @@ -0,0 +1,46 @@ +package builders + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + + "go.uber.org/zap" +) + +type Go struct { + logger *zap.Logger +} + +func NewGo(logger *zap.Logger) *Go { + return &Go{logger: logger} +} + +type GoExecutable func(ctx context.Context, victimPath string) error + +func (g *Go) Build(ctx context.Context, modulePath, entryPath string) (GoExecutable, error) { + g.logger.Debug("Building go binary", zap.String("actiondir", modulePath), zap.String("entry", entryPath)) + + if _, err := os.Stat(fmt.Sprintf("%s/%s", modulePath, entryPath)); os.IsNotExist(err) { + return nil, errors.New("could not find entry") + } + + err := exec.CommandContext( + ctx, + "/bin/bash", + "-c", + fmt.Sprintf("(cd %s; go build -o main %s)", modulePath, entryPath), + ).Run() + if err != nil { + return nil, err + } + + g.logger.Debug("Go binary built!") + + return func(ctx context.Context, victimPath string) error { + g.logger.Debug("Executing script", zap.String("victim", victimPath)) + return exec.CommandContext(ctx, fmt.Sprintf("(cd %s; %s/main)", victimPath, modulePath)).Run() + }, nil +}