refactor: changed the structure of things
parent
81490ac758
commit
0ee9389910
@ -1,22 +0,0 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"github.com/aziis98/cabret"
|
||||
"github.com/aziis98/cabret/config"
|
||||
"github.com/aziis98/cabret/pipeline"
|
||||
)
|
||||
|
||||
func Execute(cfg *config.Cabretfile) error {
|
||||
for _, p := range cfg.Build {
|
||||
ops, err := pipeline.Parse(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := pipeline.Process([]cabret.Content{}, ops); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package operation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestBuild(t *testing.T) {
|
||||
op, err := Build("categorize", map[string]any{
|
||||
"key": "tags",
|
||||
})
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, op, &Categorize{
|
||||
Key: "tags",
|
||||
})
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package operation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/aziis98/cabret/operation"
|
||||
"github.com/aziis98/cabret/util"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestSourceShortForm(t *testing.T) {
|
||||
t.Run("correct usage", func(t *testing.T) {
|
||||
op := &operation.Source{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
source: foo/bar/baz.txt
|
||||
`))
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, op, &operation.Source{
|
||||
Patterns: []string{
|
||||
"foo/bar/baz.txt",
|
||||
},
|
||||
})
|
||||
})
|
||||
t.Run("wrong usage", func(t *testing.T) {
|
||||
op := &operation.Source{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
source: 123
|
||||
`))
|
||||
|
||||
assert.Error(t, err, `expected a path pattern but got "123" of type int`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSourceWithPaths(t *testing.T) {
|
||||
t.Run("correct usage", func(t *testing.T) {
|
||||
op := &operation.Source{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
use: source
|
||||
paths:
|
||||
- foo/bar/baz-1.txt
|
||||
- foo/bar/baz-2.txt
|
||||
- foo/bar/baz-3.txt
|
||||
`))
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, op, &operation.Source{
|
||||
Patterns: []string{
|
||||
"foo/bar/baz-1.txt",
|
||||
"foo/bar/baz-2.txt",
|
||||
"foo/bar/baz-3.txt",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("wrong usage", func(t *testing.T) {
|
||||
op := &operation.Source{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
use: source
|
||||
paths:
|
||||
- foo/bar/baz-1.txt
|
||||
- foo/bar/baz-2.txt
|
||||
- 123
|
||||
`))
|
||||
|
||||
assert.Error(t, err, `expected a string but got "123" of type int`)
|
||||
})
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package operation_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/aziis98/cabret/operation"
|
||||
"github.com/aziis98/cabret/util"
|
||||
"gotest.tools/assert"
|
||||
)
|
||||
|
||||
func TestTargetShortForm(t *testing.T) {
|
||||
t.Run("correct usage", func(t *testing.T) {
|
||||
op := &operation.Target{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
target: foo/bar/baz.txt
|
||||
`))
|
||||
|
||||
assert.NilError(t, err)
|
||||
assert.DeepEqual(t, op, &operation.Target{
|
||||
PathTemplate: "foo/bar/baz.txt",
|
||||
})
|
||||
})
|
||||
t.Run("wrong usage", func(t *testing.T) {
|
||||
op := &operation.Target{}
|
||||
err := op.Configure(util.ParseYAML(`
|
||||
target: 123
|
||||
`))
|
||||
|
||||
assert.Error(t, err, `expected a path template but got "123" of type int`)
|
||||
})
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package runner
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/aziis98/cabret"
|
||||
"github.com/aziis98/cabret/config"
|
||||
"github.com/aziis98/cabret/parse"
|
||||
)
|
||||
|
||||
func RunConfig(cfg *config.Cabretfile) error {
|
||||
for _, p := range cfg.Build {
|
||||
ops, err := parse.ParsePipeline(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := RunPipeline([]cabret.Content{}, ops); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunPipeline(contents []cabret.Content, ops []cabret.Operation) ([]cabret.Content, error) {
|
||||
for _, op := range ops {
|
||||
var err error
|
||||
contents, err = RunOperation(op, contents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func RunOperation(op cabret.Operation, inputs []cabret.Content) ([]cabret.Content, error) {
|
||||
switch op := op.(type) {
|
||||
case cabret.ListOperation:
|
||||
return op.ProcessList(inputs)
|
||||
|
||||
case cabret.ItemOperation:
|
||||
outputs := []cabret.Content{}
|
||||
for _, item := range inputs {
|
||||
result, err := op.ProcessItem(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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue