with queryier

This commit is contained in:
Kasper Juul Hermansen 2022-09-18 15:48:58 +02:00
parent 56b0ca3f0b
commit 7a27e72876
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
6 changed files with 146 additions and 8 deletions

View File

@ -22,8 +22,8 @@ func CreateKrakenProcessCmd() *cobra.Command {
Path string `json:"path"` Path string `json:"path"`
}{ }{
Repository: "git@git.front.kjuulh.io:kjuulh/kraken.git", Repository: "git@git.front.kjuulh.io:kjuulh/kraken.git",
Branch: "feature/docker-action", Branch: "feature/query-results",
Path: "_examples/actions/docker_action/", Path: "_examples/queries/scrabe_readme/",
}) })
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -5,6 +5,7 @@ import (
"errors" "errors"
"git.front.kjuulh.io/kjuulh/kraken/internal/actions/builders" "git.front.kjuulh.io/kjuulh/kraken/internal/actions/builders"
"git.front.kjuulh.io/kjuulh/kraken/internal/actions/querier"
"git.front.kjuulh.io/kjuulh/kraken/internal/schema" "git.front.kjuulh.io/kjuulh/kraken/internal/schema"
"git.front.kjuulh.io/kjuulh/kraken/internal/services/storage" "git.front.kjuulh.io/kjuulh/kraken/internal/services/storage"
"go.uber.org/zap" "go.uber.org/zap"
@ -49,3 +50,28 @@ func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
return nil return nil
} }
func (a *Action) Query(ctx context.Context, area *storage.Area) ([]string, error) {
for _, query := range a.Schema.Queries {
switch query.Type {
case "grep":
exe, err := querier.NewRipGrep(zap.L()).Build(ctx, a.SchemaPath, query.Query)
if err != nil {
return nil, err
}
output, err := exe(ctx, area.Path)
if err != nil {
return nil, err
}
zap.L().Debug("Execution done")
return output, nil
default:
return nil, errors.New("could not determine query type")
}
}
return nil, nil
}

View File

@ -0,0 +1,95 @@
package querier
import (
"context"
"fmt"
"io"
"os/exec"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapio"
)
type RipGrep struct {
logger *zap.Logger
}
func NewRipGrep(logger *zap.Logger) *RipGrep {
return &RipGrep{logger: logger}
}
type RipGrepCommand func(ctx context.Context, victimPath string) ([]string, error)
func (g *RipGrep) Build(ctx context.Context, modulePath, query string) (RipGrepCommand, error) {
g.logger.Debug("Pulling docker image", zap.String("actiondir", modulePath), zap.String("query", query))
pullDockerImage := "docker pull mbologna/docker-ripgrep"
g.logger.Debug("Running command", zap.String("command", pullDockerImage))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
pullDockerImage,
)
debugwriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer debugwriter.Close()
cmd.Stdout = debugwriter
cmd.Stderr = debugwriter
err := cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
g.logger.Debug("Docker image pulled")
return func(ctx context.Context, victimPath string) ([]string, error) {
g.logger.Debug("Executing script", zap.String("victim", victimPath))
cmd := exec.CommandContext(
ctx,
"/bin/bash",
"-c",
fmt.Sprintf("docker run --rm -v %s/:/data mbologna/docker-ripgrep rg -i %s", victimPath, query),
)
runDockerWriter := &zapio.Writer{
Log: g.logger,
Level: zap.DebugLevel,
}
defer runDockerWriter.Close()
builder := &strings.Builder{}
combinedWriter := io.MultiWriter(runDockerWriter, builder)
cmd.Stdout = combinedWriter
cmd.Stderr = runDockerWriter
err = cmd.Start()
if err != nil {
return nil, err
}
err = cmd.Wait()
if err != nil {
return nil, err
}
contents := strings.Split(builder.String(), "\n")
return contents, nil
}, nil
}

View File

@ -112,17 +112,29 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
return err return err
} }
err = action.Execute(ctx, area) if len(action.Schema.Queries) > 0 {
if err != nil { result, err := action.Query(ctx, area)
return err if err != nil {
return err
}
pr.logger.Info("Query result", zap.Strings("result", result))
} }
err = pr.commit(ctx, area, repo, repoUrl) if len(action.Schema.Actions) > 0 {
if err != nil { err = action.Execute(ctx, area)
return err if err != nil {
return err
}
err = pr.commit(ctx, area, repo, repoUrl)
if err != nil {
return err
}
} }
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl)) pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
return nil return nil
} }

View File

@ -16,6 +16,10 @@ type KrakenSchema struct {
Type string `yaml:"type"` Type string `yaml:"type"`
Entry string `yaml:"entry"` Entry string `yaml:"entry"`
} `yaml:"actions"` } `yaml:"actions"`
Queries []struct {
Type string `yaml:"type"`
Query string `yaml:"query"`
} `yaml:"queries"`
} }
func Unmarshal(raw string) (*KrakenSchema, error) { func Unmarshal(raw string) (*KrakenSchema, error) {

View File

@ -34,6 +34,7 @@
- [ ] Make configurable gpg keyset - [ ] Make configurable gpg keyset
- [ ] Make configurable git provider - [ ] Make configurable git provider
- [ ] Create templating function - [ ] Create templating function
- [ ] Add way to see progress of runners
## Version 1.x ## Version 1.x