Skip to content

Commit

Permalink
Support use minigo code in gohanscript value
Browse files Browse the repository at this point in the history
  • Loading branch information
nati committed Apr 5, 2016
1 parent 6fa7099 commit b85ea08
Show file tree
Hide file tree
Showing 25 changed files with 375 additions and 271 deletions.
47 changes: 29 additions & 18 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package cli
import (
"bytes"
"fmt"
"github.com/cloudwan/gohan/schema"
"github.com/cloudwan/gohan/util"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/cloudwan/gohan/schema"
"github.com/cloudwan/gohan/util"

"github.com/cloudwan/gohan/cli/client"
"github.com/cloudwan/gohan/db"
"github.com/cloudwan/gohan/db/sql"
Expand Down Expand Up @@ -390,22 +391,7 @@ Run gohan script code.`,
vm.Context.Set("args", args)
vm.Context.Set("flags", flags)
configFile := c.String("config-file")
if configFile != "" {
config := util.GetConfig()
err := config.ReadConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
err = l.SetUpLogging(config)
if err != nil {
fmt.Printf("Logging setup error: %s\n", err)
os.Exit(1)
return
}
log.Info("logging initialized")
}
loadConfig(configFile)
_, err := vm.RunFile(src)
if err != nil {
fmt.Println(err)
Expand All @@ -423,9 +409,34 @@ func getTestCommand() cli.Command {
Usage: "Run Gohan script Test",
Description: `
Run gohan script yaml code.`,
Flags: []cli.Flag{
cli.StringFlag{Name: "config-file,c", Value: "", Usage: "config file path"},
},
Action: func(c *cli.Context) {
dir := c.Args()[0]
configFile := c.String("config-file")
loadConfig(configFile)
gohanscript.RunTests(dir)
},
}
}

func loadConfig(configFile string) {
if configFile == "" {
return
}
config := util.GetConfig()
err := config.ReadConfig(configFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
err = l.SetUpLogging(config)
if err != nil {
fmt.Printf("Logging setup error: %s\n", err)
os.Exit(1)
return
}
log.Info("logging initialized")
}
8 changes: 4 additions & 4 deletions extension/gohanscript/autogen/lib_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func init() {
gohanscript.RegisterStmtParser("fetch_content",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
path, _ := stmt.Arg("path", context).(string)
var err error
result1, err := lib.FetchContent(path)
Expand All @@ -25,8 +25,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("save_content",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
path, _ := stmt.Arg("path", context).(string)
data, _ := stmt.Arg("data", context).(interface{})
err := lib.SaveContent(path, data)
Expand Down
72 changes: 36 additions & 36 deletions extension/gohanscript/autogen/lib_gohan.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

func init() {
gohanscript.RegisterStmtParser("gohan_schema",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
schemaID, _ := stmt.Arg("schema_id", context).(string)
var err error
result1, err := lib.GohanSchema(schemaID)
Expand All @@ -27,8 +27,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("gohan_schemas",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
result1 := lib.GohanSchemas()
return result1, nil
}, nil
Expand All @@ -39,8 +39,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("gohan_policies",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
result1 := lib.GohanPolicies()
return result1, nil
}, nil
Expand All @@ -51,8 +51,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("read_config",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
path, _ := stmt.Arg("path", context).(string)
err := lib.ReadConfig(path)
return nil, err
Expand All @@ -67,8 +67,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("get_config",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
key, _ := stmt.Arg("key", context).(string)
defaultValue, _ := stmt.Arg("default_value", context).(interface{})
result1 := lib.GetConfig(key, defaultValue)
Expand All @@ -86,8 +86,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("gohan_load_schema",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
src, _ := stmt.Arg("src", context).(string)
var err error
result1, err := lib.GohanLoadSchema(src)
Expand All @@ -103,8 +103,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("connect_db",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
dbType, _ := stmt.Arg("db_type", context).(string)
connection, _ := stmt.Arg("connection", context).(string)
var err error
Expand All @@ -123,8 +123,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("init_db",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
dbType, _ := stmt.Arg("db_type", context).(string)
connection, _ := stmt.Arg("connection", context).(string)
dropOnCreate, _ := stmt.Arg("drop_on_create", context).(bool)
Expand All @@ -148,8 +148,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_begin",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
connection, _ := stmt.Arg("connection", context).(db.DB)
var err error
result1, err := lib.DBBegin(connection)
Expand All @@ -165,8 +165,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("db_commit",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
err := lib.DBCommit(tx)
return nil, err
Expand All @@ -181,8 +181,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_close",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
err := lib.DBClose(tx)
return nil, err
Expand All @@ -197,8 +197,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_get",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
id, _ := stmt.Arg("id", context).(string)
Expand All @@ -223,8 +223,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("db_create",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
data, _ := stmt.Arg("data", context).(map[string]interface{})
Expand All @@ -245,8 +245,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_list",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
filter, _ := stmt.Arg("filter", context).(map[string]interface{})
Expand All @@ -268,8 +268,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("db_update",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
data, _ := stmt.Arg("data", context).(map[string]interface{})
Expand All @@ -290,8 +290,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_delete",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
id, _ := stmt.Arg("id", context).(string)
Expand All @@ -312,8 +312,8 @@ func init() {
return []interface{}{result1}
})
gohanscript.RegisterStmtParser("db_query",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
tx, _ := stmt.Arg("tx", context).(transaction.Transaction)
schemaID, _ := stmt.Arg("schema_id", context).(string)
sql, _ := stmt.Arg("sql", context).(string)
Expand All @@ -338,8 +338,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("db_column",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
schemaID, _ := stmt.Arg("schema_id", context).(string)
join, _ := stmt.Arg("join", context).(bool)
var err error
Expand Down
24 changes: 12 additions & 12 deletions extension/gohanscript/autogen/lib_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func init() {
gohanscript.RegisterStmtParser("http_get",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
var err error
Expand All @@ -28,8 +28,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("http_post",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
postData, _ := stmt.Arg("post_data", context).(map[string]interface{})
Expand All @@ -51,8 +51,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("http_put",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
postData, _ := stmt.Arg("post_data", context).(map[string]interface{})
Expand All @@ -74,8 +74,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("http_patch",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
postData, _ := stmt.Arg("post_data", context).(map[string]interface{})
Expand All @@ -97,8 +97,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("http_delete",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
var err error
Expand All @@ -117,8 +117,8 @@ func init() {
return []interface{}{result1, result2}
})
gohanscript.RegisterStmtParser("http_request",
func(stmt *gohanscript.Stmt) (func(*gohanscript.VM, *gohanscript.Context) (interface{}, error), error) {
return func(vm *gohanscript.VM, context *gohanscript.Context) (interface{}, error) {
func(stmt *gohanscript.Stmt) (func(*gohanscript.Context) (interface{}, error), error) {
return func(context *gohanscript.Context) (interface{}, error) {
url, _ := stmt.Arg("url", context).(string)
method, _ := stmt.Arg("method", context).(string)
headers, _ := stmt.Arg("headers", context).(map[string]interface{})
Expand Down
Loading

0 comments on commit b85ea08

Please sign in to comment.