diff --git a/cmd/kraken/commands/process.go b/cmd/kraken/commands/process.go index 1ffd16e..83aa0eb 100644 --- a/cmd/kraken/commands/process.go +++ b/cmd/kraken/commands/process.go @@ -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) diff --git a/internal/actions/action.go b/internal/actions/action.go index bc9fdb4..a535ba0 100644 --- a/internal/actions/action.go +++ b/internal/actions/action.go @@ -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 +} diff --git a/internal/actions/querier/ripgrep.go b/internal/actions/querier/ripgrep.go new file mode 100644 index 0000000..671d6d5 --- /dev/null +++ b/internal/actions/querier/ripgrep.go @@ -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 +} diff --git a/internal/commands/process_repos.go b/internal/commands/process_repos.go index f801240..cbf6f6b 100644 --- a/internal/commands/process_repos.go +++ b/internal/commands/process_repos.go @@ -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 } diff --git a/internal/schema/kraken.go b/internal/schema/kraken.go index fb7d75e..b1c76f5 100644 --- a/internal/schema/kraken.go +++ b/internal/schema/kraken.go @@ -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) { diff --git a/roadmap.md b/roadmap.md index 5414be2..59573fd 100644 --- a/roadmap.md +++ b/roadmap.md @@ -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