You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package runner
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/aziis98/cabret"
|
|
"github.com/aziis98/cabret/config"
|
|
"github.com/aziis98/cabret/parse"
|
|
)
|
|
|
|
func RunConfig(cfg *config.Cabretfile) error {
|
|
files, err := cabret.FindFiles(cfg.Options.Excludes...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ctx := &cabret.Context{
|
|
Files: files,
|
|
}
|
|
|
|
for _, p := range cfg.Build {
|
|
ops, err := parse.ParsePipeline(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err := RunPipeline(ops, ctx, []cabret.Content{}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// RunPipeline runs the given pipeline "ops" with the context ctx and initial contents "contents"
|
|
func RunPipeline(operations []cabret.Operation, ctx *cabret.Context, contents []cabret.Content) ([]cabret.Content, error) {
|
|
for _, op := range operations {
|
|
var err error
|
|
contents, err = RunOperation(op, ctx, contents)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return contents, nil
|
|
}
|
|
|
|
func RunOperation(op cabret.Operation, ctx *cabret.Context, inputs []cabret.Content) ([]cabret.Content, error) {
|
|
switch op := op.(type) {
|
|
case cabret.ListOperation:
|
|
return op.ProcessList(ctx, inputs)
|
|
|
|
case cabret.ItemOperation:
|
|
outputs := []cabret.Content{}
|
|
for _, item := range inputs {
|
|
result, err := op.ProcessItem(ctx, item)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// skip terminal operations
|
|
if result == nil {
|
|
continue
|
|
}
|
|
outputs = append(outputs, *result)
|
|
}
|
|
return outputs, nil
|
|
default:
|
|
return nil, fmt.Errorf(`invalid operation type %T`, op)
|
|
}
|
|
}
|