Skip to content
This repository has been archived by the owner on Sep 28, 2020. It is now read-only.

Commit

Permalink
builder,cli-tests: var_exists() function.
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Hollensbe <github@hollensbe.org>
  • Loading branch information
Erik Hollensbe committed May 14, 2017
1 parent 7d5ba99 commit 3629e54
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
8 changes: 6 additions & 2 deletions builder/command/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ import (
"github.com/pkg/errors"
)

// VarExists corresponds to the `var_exists` func.
func (i *Interpreter) VarExists(key string) bool {
_, ok := i.vars[key]
return ok
}

// Var corresponds to the `var` func.
func (i *Interpreter) Var(key string) (string, error) {
val, ok := i.vars[key]
if !ok {
return "", fmt.Errorf("value for key %q does not exist", key)
}

fmt.Println(val)

return val, nil
}

Expand Down
29 changes: 21 additions & 8 deletions builder/evaluator/mruby/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ type funcFunc func(args []*gm.MrbValue, self *gm.MrbValue) (gm.Value, gm.Value)

func (m *MRuby) funcJumpTable() map[string]*funcDefinition {
return map[string]*funcDefinition{
"var": {m.varFunc, gm.ArgsReq(1)},
"import": {m.importFunc, gm.ArgsReq(1)},
"save": {m.saveFunc, gm.ArgsReq(1)},
"getenv": {m.getenv, gm.ArgsReq(1)},
"getuid": {m.getuid, gm.ArgsReq(1)},
"getgid": {m.getgid, gm.ArgsReq(1)},
"read": {m.read, gm.ArgsReq(1)},
"skip": {m.skip, gm.ArgsNone() | gm.ArgsBlock()},
"var_exists": {m.varExistsFunc, gm.ArgsReq(1)},
"var": {m.varFunc, gm.ArgsReq(1)},
"import": {m.importFunc, gm.ArgsReq(1)},
"save": {m.saveFunc, gm.ArgsReq(1)},
"getenv": {m.getenv, gm.ArgsReq(1)},
"getuid": {m.getuid, gm.ArgsReq(1)},
"getgid": {m.getgid, gm.ArgsReq(1)},
"read": {m.read, gm.ArgsReq(1)},
"skip": {m.skip, gm.ArgsNone() | gm.ArgsBlock()},
}
}

func (m *MRuby) varExistsFunc(args []*gm.MrbValue, self *gm.MrbValue) (gm.Value, gm.Value) {
if err := checkArgs(args, 1); err != nil {
return nil, m.createException(err)
}

if m.Interp.VarExists(args[0].String()) {
return m.mrb.TrueValue(), nil
}

return m.mrb.FalseValue(), nil
}

func (m *MRuby) varFunc(args []*gm.MrbValue, self *gm.MrbValue) (gm.Value, gm.Value) {
if err := checkArgs(args, 1); err != nil {
return nil, m.createException(err)
Expand Down
11 changes: 11 additions & 0 deletions cli-tests/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ func (s *cliSuite) TestVars(c *C) {

c.Assert(err, IsNil)
checkFailure(c, cmd)

cmd, err = build(`
from "debian"
copy ".", "."
if var_exists("testfile")
run "test -f #{var("testfile")}"
end
`, "-n")

c.Assert(err, IsNil)
checkSuccess(c, cmd)
}

0 comments on commit 3629e54

Please sign in to comment.