Compare commits
No commits in common. "e760eb48ff183cb5103ff336fb3d78241bd211d2" and "56b0ca3f0bfc27cdbefe5c0be5fe5ff98a724576" have entirely different histories.
e760eb48ff
...
56b0ca3f0b
@ -9,19 +9,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func CreateKrakenProcessCmd() *cobra.Command {
|
func CreateKrakenProcessCmd() *cobra.Command {
|
||||||
|
|
||||||
var (
|
|
||||||
actionsRepo string
|
|
||||||
branch string
|
|
||||||
path string
|
|
||||||
)
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "process",
|
Use: "process",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
Run: func(cmd *cobra.Command, _ []string) {
|
||||||
if err := cmd.ParseFlags(args); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
@ -31,9 +21,9 @@ func CreateKrakenProcessCmd() *cobra.Command {
|
|||||||
Branch string `json:"branch"`
|
Branch string `json:"branch"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
}{
|
}{
|
||||||
Repository: actionsRepo,
|
Repository: "git@git.front.kjuulh.io:kjuulh/kraken.git",
|
||||||
Branch: branch,
|
Branch: "feature/docker-action",
|
||||||
Path: path,
|
Path: "_examples/actions/docker_action/",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -56,18 +46,8 @@ func CreateKrakenProcessCmd() *cobra.Command {
|
|||||||
if resp.StatusCode >= 300 {
|
if resp.StatusCode >= 300 {
|
||||||
panic(resp.Status)
|
panic(resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pf := cmd.PersistentFlags()
|
|
||||||
|
|
||||||
pf.StringVar(&actionsRepo, "actions-repo", "", "actions repo is the location of your actions, not where to apply the actions themselves, that should be self contained")
|
|
||||||
cmd.MarkPersistentFlagRequired("actions-repo")
|
|
||||||
pf.StringVar(&branch, "branch", "main", "which branch to look for actions in, will default to main")
|
|
||||||
pf.StringVar(&path, "path", "", "the location of the path inside the repository")
|
|
||||||
cmd.MarkPersistentFlagRequired("path")
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@ 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"
|
||||||
@ -50,28 +49,3 @@ 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, bool, 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, false, err
|
|
||||||
}
|
|
||||||
output, found, err := exe(ctx, area.Path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
zap.L().Debug("Execution done")
|
|
||||||
|
|
||||||
return output, found, nil
|
|
||||||
|
|
||||||
default:
|
|
||||||
return nil, false, errors.New("could not determine query type")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false, nil
|
|
||||||
}
|
|
||||||
|
@ -1,106 +0,0 @@
|
|||||||
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, bool, 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, bool, error) {
|
|
||||||
g.logger.Debug("Executing script", zap.String("victim", victimPath))
|
|
||||||
|
|
||||||
runRipGrepCmd := fmt.Sprintf("docker run --rm -v %s/:/data:ro mbologna/docker-ripgrep rg -i '%s' || true", victimPath, query)
|
|
||||||
|
|
||||||
g.logger.Debug("Execute ripgrep query", zap.String("command", runRipGrepCmd))
|
|
||||||
|
|
||||||
cmd := exec.CommandContext(
|
|
||||||
ctx,
|
|
||||||
"/bin/bash",
|
|
||||||
"-c",
|
|
||||||
runRipGrepCmd,
|
|
||||||
)
|
|
||||||
|
|
||||||
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 = combinedWriter
|
|
||||||
|
|
||||||
err = cmd.Start()
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = cmd.Wait()
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
contents := strings.Split(builder.String(), "\n")
|
|
||||||
validatedOutput := make([]string, 0)
|
|
||||||
|
|
||||||
for _, c := range contents {
|
|
||||||
if !strings.Contains(c, "WARNING:") {
|
|
||||||
validatedOutput = append(validatedOutput, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
found := len(validatedOutput) > 0
|
|
||||||
|
|
||||||
return validatedOutput, found, nil
|
|
||||||
}, nil
|
|
||||||
}
|
|
@ -112,32 +112,17 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(action.Schema.Queries) > 0 {
|
err = action.Execute(ctx, area)
|
||||||
result, found, err := action.Query(ctx, area)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if found {
|
|
||||||
pr.logger.Info("Query result", zap.Strings("result", result))
|
|
||||||
// TODO: Append to real result, and return together
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(action.Schema.Actions) > 0 {
|
err = pr.commit(ctx, area, repo, repoUrl)
|
||||||
err = action.Execute(ctx, area)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,10 +16,6 @@ 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) {
|
||||||
|
@ -22,7 +22,7 @@
|
|||||||
- [x] Allow instantiation of actions, kraken template repo etc.
|
- [x] Allow instantiation of actions, kraken template repo etc.
|
||||||
- [x] Implement docker action
|
- [x] Implement docker action
|
||||||
- [x] Create pr for gitea provider
|
- [x] Create pr for gitea provider
|
||||||
- [x] Providing query results
|
- [ ] Providing query results
|
||||||
- [ ] Create CLI to trigger action
|
- [ ] Create CLI to trigger action
|
||||||
|
|
||||||
### Not in scope
|
### Not in scope
|
||||||
@ -34,7 +34,6 @@
|
|||||||
- [ ] 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
|
||||||
|
|
||||||
|
@ -2,7 +2,4 @@
|
|||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
current_branch=$(git branch --show-current)
|
go run cmd/kraken/kraken.go process
|
||||||
|
|
||||||
go run cmd/kraken/kraken.go process --actions-repo "git@git.front.kjuulh.io:kjuulh/kraken.git" --branch "$current_branch" --path "_examples/actions/write_a_readme"
|
|
||||||
go run cmd/kraken/kraken.go process --actions-repo "git@git.front.kjuulh.io:kjuulh/kraken.git" --branch "$current_branch" --path "_examples/queries/scrape_readme"
|
|
||||||
|
Loading…
Reference in New Issue
Block a user