Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
boz committed Jan 21, 2017
1 parent 82f77f1 commit ced2251
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 46 deletions.
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
test:
go test -v -race ./test ./vars

.PHONY: test
example:
(cd example && make)

clean:
(cd example && make clean)

.PHONY: test example clean
6 changes: 5 additions & 1 deletion evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ type evaluator struct {
}

func NewEvaluator() *evaluator {
return NewEvaluatorWithLogger(logrus.StandardLogger())
}

func NewEvaluatorWithLogger(logger *logrus.Logger) *evaluator {
ctx, cancel := context.WithCancel(context.TODO())
return &evaluator{
path: "",
ctx: ctx,
cancel: cancel,
log: logrus.StandardLogger(),
log: logger,
vars: vars.NewVars(),
}
}
Expand Down
1 change: 1 addition & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example
11 changes: 11 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FILES := $(wildcard *.go ../*.go ../*/*.go)

OBJECT := example

$(OBJECT): $(FILES)
go build -o $@

clean:
rm $(OBJECT) || true

.PHONY: $(OBJECT)
21 changes: 21 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"github.com/ovrclk/gestalt"
g "github.com/ovrclk/gestalt/builder"
)

func main() {
c := suite()
gestalt.Run(c)
}

func suite() gestalt.Component {
return g.Suite("integration").
Run(pingServer())
}

func pingServer() gestalt.Component {
return g.SH("ping", "echo", "ping")
// WithMeta(vars.NewMeta().Require("host"))
}
78 changes: 48 additions & 30 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import (
"fmt"
"os"

"github.com/Sirupsen/logrus"
"github.com/ovrclk/gestalt/vars"

kingpin "gopkg.in/alecthomas/kingpin.v2"
)

func Run(c Component) error {
return RunWith(c, os.Args[1:])
func Run(c Component) {
RunWith(c, os.Args[1:])
}

func RunWith(c Component, args []string) error {
return NewRunner().
func RunWith(c Component, args []string) {
NewRunner().
WithArgs(args).
WithComponent(c).
Run()
Expand All @@ -23,16 +24,19 @@ func RunWith(c Component, args []string) error {
type Runner interface {
WithArgs([]string) Runner
WithComponent(Component) Runner
Run() error
WithTerminate(func(status int)) Runner
Run()
}

type runner struct {
args []string
cmp Component

terminate func(status int)
}

func NewRunner() Runner {
return &runner{}
return &runner{terminate: os.Exit}
}

func (r *runner) WithArgs(args []string) Runner {
Expand All @@ -45,21 +49,28 @@ func (r *runner) WithComponent(cmp Component) Runner {
return r
}

func (r *runner) Run() error {
func (r *runner) WithTerminate(terminate func(int)) Runner {
r.terminate = terminate
return r
}

func (r *runner) Run() {

opts := newOptions(r)

if r.cmp == nil {
return fmt.Errorf("No component found")
opts.app.Fatalf("no component given")
return
}

opts := newOptions()

switch kingpin.MustParse(opts.app.Parse(r.args)) {
case opts.cmdShow.FullCommand():
return r.doShow(opts)
r.doShow(opts)
case opts.cmdEval.FullCommand():
return r.doEval(opts)
r.doEval(opts)
case opts.cmdValidate.FullCommand():
r.doValidate(opts)
}

return fmt.Errorf("unknown command")
}

type options struct {
Expand All @@ -81,10 +92,10 @@ func (opts *options) getVars() vars.Vars {
return v
}

func newOptions() *options {
func newOptions(r *runner) *options {
opts := &options{}

opts.app = kingpin.New("gestalt", "Run gestalt components")
opts.app = kingpin.New("gestalt", "Run gestalt components").Terminate(r.terminate)
opts.debug = opts.app.Flag("debug", "Display debug logging").Bool()

opts.vars = opts.app.Flag("set", "set variables").Short('s').StringMap()
Expand All @@ -96,32 +107,39 @@ func newOptions() *options {
return opts
}

func (r *runner) doEval(opts *options) error {
e := NewEvaluator()
e.Vars().Merge(opts.getVars())
func (r *runner) doEval(opts *options) {

logger := logrus.New()
if *opts.debug {
logger.Level = logrus.DebugLevel
}

e := NewEvaluatorWithLogger(logger)

missing := ValidateWith(r.cmp, e.Vars())
e.Vars().Merge(opts.getVars())

if len(missing) > 0 {
return r.showUnresolvedVars(opts, missing)
if err := r.showUnresolvedVars(opts, e.Vars()); err != nil {
opts.app.FatalIfError(err, "")
}

return e.Evaluate(r.cmp).Wait().Err()
err := e.Evaluate(r.cmp).Wait().Err()

opts.app.FatalIfError(err, "error evaluating components")
}

func (r *runner) doShow(opts *options) error {
func (r *runner) doShow(opts *options) {
Dump(r.cmp)
return nil
}

func (r *runner) doValidate(opts *options) error {
func (r *runner) doValidate(opts *options) {
err := r.showUnresolvedVars(opts, opts.getVars())
opts.app.FatalIfError(err, "")
}

missing := ValidateWith(r.cmp, opts.getVars())
func (r *runner) showUnresolvedVars(opts *options, vars vars.Vars) error {

return r.showUnresolvedVars(opts, missing)
}
unresolved := ValidateWith(r.cmp, vars)

func (r *runner) showUnresolvedVars(opts *options, unresolved []Unresolved) error {
if len(unresolved) == 0 {
return nil
}
Expand Down
48 changes: 34 additions & 14 deletions test/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ func TestEnsure(t *testing.T) {
return result.Complete()
}))

if err := gestalt.RunWith(c, []string{}); err == nil {
t.Errorf("expected error")
}
assertGestaltFails(t, c, []string{})

if ran != true {
t.Fatal("fnally block didn't run")
Expand All @@ -84,7 +82,7 @@ func TestCliVars(t *testing.T) {
"-sc=baz",
}

runComponentWith(t, consumer(t), args)
assertGestaltSuccess(t, consumer(t), args)
}

func TestDump(t *testing.T) {
Expand All @@ -97,16 +95,6 @@ func TestDump(t *testing.T) {
Run(g.Retry(10).Run(g.SH("x", "echo", "sup")))))
}

func runComponent(t *testing.T, c gestalt.Component) {
runComponentWith(t, c, []string{})
}

func runComponentWith(t *testing.T, c gestalt.Component, args []string) {
if err := gestalt.RunWith(c, args); err != nil {
t.Errorf("run failed: %v", err)
}
}

func producer(t *testing.T) gestalt.Component {
return g.SH("producer", "echo", "foo", "bar", "baz").
FN(g.P().Capture("a", "b", "c")).
Expand Down Expand Up @@ -143,6 +131,38 @@ func readFields(t *testing.T) gestalt.Action {
}
}

func runComponent(t *testing.T, c gestalt.Component) {
assertGestaltSuccess(t, c, []string{})
}

func assertGestaltSuccess(t *testing.T, c gestalt.Component, args []string) {
terminate := func(status int) {
if status != 0 {
t.Fatalf("gestalt exited with nonzero status (%v", status)
}
}
runner := gestalt.NewRunner().
WithComponent(c).
WithArgs(args).
WithTerminate(terminate)
runner.Run()
}

func assertGestaltFails(t *testing.T, c gestalt.Component, args []string) {
terminate := func(status int) {
if status == 0 {
t.Fail()
}
}

runner := gestalt.NewRunner().
WithComponent(c).
WithArgs(args).
WithTerminate(terminate)

runner.Run()
}

func TestMain(m *testing.M) {
//logrus.SetLevel(logrus.DebugLevel)
os.Exit(m.Run())
Expand Down

0 comments on commit ced2251

Please sign in to comment.