2022-09-18 16:49:34 +02:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2022-09-18 17:02:47 +02:00
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/actions/builders"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/actions/querier"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/schema"
|
|
|
|
"git.front.kjuulh.io/kjuulh/octopush/internal/services/storage"
|
2022-09-18 16:49:34 +02:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Action struct {
|
2022-09-18 17:02:47 +02:00
|
|
|
Schema *schema.OctopushSchema
|
2022-09-18 16:49:34 +02:00
|
|
|
SchemaPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Action) Execute(ctx context.Context, area *storage.Area) error {
|
|
|
|
for _, action := range a.Schema.Actions {
|
|
|
|
switch action.Type {
|
|
|
|
case "go":
|
|
|
|
exe, err := builders.NewGo(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = exe(ctx, area.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
zap.L().Debug("Execution done")
|
|
|
|
|
|
|
|
case "docker-build":
|
|
|
|
zap.L().Debug("Building docker-build")
|
|
|
|
runCmd, err := builders.NewDockerBuild(zap.L()).Build(ctx, a.SchemaPath, action.Entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = runCmd(ctx, area.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return errors.New("could not determine action type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|