with queryier
This commit is contained in:
parent
56b0ca3f0b
commit
7a27e72876
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
95
internal/actions/querier/ripgrep.go
Normal file
95
internal/actions/querier/ripgrep.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user