feature/query-results #12

Merged
kjuulh merged 5 commits from feature/query-results into v0.1 2022-09-18 16:11:23 +02:00
6 changed files with 146 additions and 8 deletions
Showing only changes of commit 7a27e72876 - Show all commits

View File

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

View File

@ -5,6 +5,7 @@ import (
"errors"
"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/services/storage"
"go.uber.org/zap"
@ -49,3 +50,28 @@ func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
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
}
err = action.Execute(ctx, area)
if err != nil {
return err
if len(action.Schema.Queries) > 0 {
result, err := action.Query(ctx, area)
if err != nil {
return err
}
pr.logger.Info("Query result", zap.Strings("result", result))
}
err = pr.commit(ctx, area, repo, repoUrl)
if err != nil {
return err
if len(action.Schema.Actions) > 0 {
err = action.Execute(ctx, area)
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))
return nil
}

View File

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

View File

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