Skip to content

Commit

Permalink
exec: remove text process
Browse files Browse the repository at this point in the history
 * rename objpipe -> pipe
  • Loading branch information
boz committed Feb 24, 2017
1 parent 7bacd95 commit 87637f0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 130 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
go test -v -race . ./component ./exec ./vars ./test
go test -race . ./component ./exec ./vars ./test

example:
(cd example && make)
Expand Down
10 changes: 6 additions & 4 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ func EXEC(name, cmd string, args ...string) exec.Cmd {
return exec.EXEC(name, cmd, args...)
}

func Columns(columns ...string) exec.ObjectPipe {
return exec.ParseColumns(columns...)
func Capture(columns ...string) exec.CmdFn {
return Columns(columns...).
EnsureCount(1).
CaptureAll()
}

func P() exec.TextPipe {
return exec.P()
func Columns(columns ...string) exec.Pipeline {
return exec.ParseColumns(columns...)
}

func Require(args ...string) vars.Meta {
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func DevServer() gestalt.Component {
// create a new farm
func GroupUp() gestalt.Component {
check := g.SH("check", "echo", "a", "{{group-name}}-id", "b").
FN(g.P().Capture("_", "group-host", "_")).
FN(g.Capture("_", "group-host", "_")).
WithMeta(g.Export("group-host"))
return g.Group("group-up").
Run(
Expand Down
52 changes: 26 additions & 26 deletions exec/objpipe.go → exec/pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ import (
"github.com/ovrclk/gestalt/vars"
)

type ObjectPipe interface {
type Pipeline interface {
Capture(...string) CmdFn
CaptureAll() CmdFn

GrepField(string, string) ObjectPipe
GrepWith(PipeObjGrepFn) ObjectPipe
GrepField(string, string) Pipeline
GrepWith(PipeFilter) Pipeline

EnsureCount(int) ObjectPipe
EnsureWith(PipeObjEnsureFn) ObjectPipe
EnsureCount(int) Pipeline
EnsureWith(PipeValidator) Pipeline
}

type PipeObject map[string]string
type PipeObjFn func([]PipeObject, gestalt.Evaluator) ([]PipeObject, error)
type PipeObjParseFn func(*bufio.Reader, gestalt.Evaluator) ([]PipeObject, error)
type PipeObjGrepFn func(PipeObject, gestalt.Evaluator) bool
type PipeObjEnsureFn func([]PipeObject) error

type objpipe struct {
pipe []PipeObjFn
parsefn PipeObjParseFn
type PipeStage func([]PipeObject, gestalt.Evaluator) ([]PipeObject, error)
type PipeParser func(*bufio.Reader, gestalt.Evaluator) ([]PipeObject, error)
type PipeFilter func(PipeObject, gestalt.Evaluator) bool
type PipeValidator func([]PipeObject) error

type pipeline struct {
pipe []PipeStage
parsefn PipeParser
}

func NewObjectPipe(fn PipeObjParseFn) ObjectPipe {
return &objpipe{make([]PipeObjFn, 0), fn}
func NewObjectPipe(fn PipeParser) Pipeline {
return &pipeline{make([]PipeStage, 0), fn}
}

func ParseColumns(columns ...string) ObjectPipe {
func ParseColumns(columns ...string) Pipeline {
return LineParser(func(line string, _ gestalt.Evaluator) (PipeObject, error) {
fields := strings.Fields(line)
obj := make(PipeObject)
Expand All @@ -47,7 +47,7 @@ func ParseColumns(columns ...string) ObjectPipe {
})
}

func LineParser(fn func(string, gestalt.Evaluator) (PipeObject, error)) ObjectPipe {
func LineParser(fn func(string, gestalt.Evaluator) (PipeObject, error)) Pipeline {
return NewObjectPipe(func(r *bufio.Reader, e gestalt.Evaluator) ([]PipeObject, error) {
results := make([]PipeObject, 0)

Expand All @@ -67,7 +67,7 @@ func LineParser(fn func(string, gestalt.Evaluator) (PipeObject, error)) ObjectPi
})
}

func (p *objpipe) EnsureCount(count int) ObjectPipe {
func (p *pipeline) EnsureCount(count int) Pipeline {
return p.EnsureWith(func(objs []PipeObject) error {
if len(objs) != count {
return fmt.Errorf("invalid count have:%v want:%v", len(objs), count)
Expand All @@ -76,7 +76,7 @@ func (p *objpipe) EnsureCount(count int) ObjectPipe {
})
}

func (p *objpipe) EnsureWith(fn PipeObjEnsureFn) ObjectPipe {
func (p *pipeline) EnsureWith(fn PipeValidator) Pipeline {
return p.Then(func(objs []PipeObject, _ gestalt.Evaluator) ([]PipeObject, error) {
if err := fn(objs); err != nil {
return objs, err
Expand All @@ -85,7 +85,7 @@ func (p *objpipe) EnsureWith(fn PipeObjEnsureFn) ObjectPipe {
})
}

func (p *objpipe) GrepField(key string, value string) ObjectPipe {
func (p *pipeline) GrepField(key string, value string) Pipeline {
return p.GrepWith(func(obj PipeObject, e gestalt.Evaluator) bool {
if v, ok := obj[key]; ok {
expanded := vars.Expand(e.Vars(), value)
Expand All @@ -95,7 +95,7 @@ func (p *objpipe) GrepField(key string, value string) ObjectPipe {
})
}

func (p *objpipe) GrepWith(fn PipeObjGrepFn) ObjectPipe {
func (p *pipeline) GrepWith(fn PipeFilter) Pipeline {
return p.Then(func(objs []PipeObject, e gestalt.Evaluator) ([]PipeObject, error) {
result := make([]PipeObject, 0)
for _, obj := range objs {
Expand All @@ -107,7 +107,7 @@ func (p *objpipe) GrepWith(fn PipeObjGrepFn) ObjectPipe {
})
}

func (p *objpipe) Capture(keys ...string) CmdFn {
func (p *pipeline) Capture(keys ...string) CmdFn {
return p.finally(func(objs []PipeObject, e gestalt.Evaluator) error {
for _, obj := range objs {
for _, k := range keys {
Expand All @@ -120,7 +120,7 @@ func (p *objpipe) Capture(keys ...string) CmdFn {
})
}

func (p *objpipe) CaptureAll() CmdFn {
func (p *pipeline) CaptureAll() CmdFn {
return p.finally(func(objs []PipeObject, e gestalt.Evaluator) error {
for _, obj := range objs {
for k, v := range obj {
Expand All @@ -131,12 +131,12 @@ func (p *objpipe) CaptureAll() CmdFn {
})
}

func (p *objpipe) Then(fn PipeObjFn) *objpipe {
func (p *pipeline) Then(fn PipeStage) *pipeline {
p.pipe = append(p.pipe, fn)
return p
}

func (p *objpipe) finally(fn func(objs []PipeObject, e gestalt.Evaluator) error) CmdFn {
func (p *pipeline) finally(fn func(objs []PipeObject, e gestalt.Evaluator) error) CmdFn {
return func(r *bufio.Reader, e gestalt.Evaluator) error {
objs, err := p.process(r, e)
if err != nil {
Expand All @@ -146,7 +146,7 @@ func (p *objpipe) finally(fn func(objs []PipeObject, e gestalt.Evaluator) error)
}
}

func (p *objpipe) process(r *bufio.Reader, e gestalt.Evaluator) ([]PipeObject, error) {
func (p *pipeline) process(r *bufio.Reader, e gestalt.Evaluator) ([]PipeObject, error) {
objs, err := p.parsefn(r, e)
if err != nil {
return objs, err
Expand Down
File renamed without changes.
96 changes: 0 additions & 96 deletions exec/textprocess.go

This file was deleted.

4 changes: 2 additions & 2 deletions test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestCliVars(t *testing.T) {

func TextExpand(t *testing.T) {
producer := g.SH("producer", "echo", "{{host}}", "bar", "baz").
FN(g.P().Capture("a", "b", "c")).
FN(g.Capture("a", "b", "c")).
WithMeta(g.Export("a", "b", "c").Require("host"))

suite := g.Suite("suite").
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestDump(t *testing.T) {

func producer(t *testing.T) gestalt.Component {
return g.SH("producer", "echo", "foo", "bar", "baz").
FN(g.P().Capture("a", "b", "c")).
FN(g.Capture("a", "b", "c")).
WithMeta(g.Export("a", "b", "c"))
}

Expand Down

0 comments on commit 87637f0

Please sign in to comment.