Skip to content
Permalink
Browse files

filetree,...: tweak the Lua API

  • Loading branch information...
tsileo committed May 30, 2019
1 parent bd5d33a commit b5ede4ed33cc1428fbdb6cb628266e123a644d83
Showing with 74 additions and 11 deletions.
  1. +1 −1 pkg/apps/apps.go
  2. +2 −2 pkg/docstore/docstore.go
  3. +4 −4 pkg/docstore/lua.go
  4. +67 −4 pkg/filetree/lua/lua.go
@@ -199,7 +199,7 @@ func (apps *Apps) newApp(appConf *config.AppConfig) (*App, error) {

docstore.SetLuaGlobals(L)
blobstoreLua.Setup(context.TODO(), L, apps.bs)
filetreeLua.Setup(L, apps.ft, apps.bs)
filetreeLua.Setup(L, apps.ft, apps.bs, apps.kvs)
docstoreLua.Setup(L, apps.docstore)
kvLua.Setup(L, apps.kvs, context.TODO())
gitserverLua.Setup(L, apps.gs)
@@ -187,11 +187,11 @@ func New(logger log.Logger, conf *config.Config, kvStore store.KvStore, blobStor
logger.Debug("indexes setup", "indexes", fmt.Sprintf("%+v", sortIndexes))
}

hooks, err := newLuaHooks(conf, ft, blobStore)
hooks, err := newLuaHooks(conf, ft, blobStore, kvStore)
if err != nil {
return nil, err
}
fdocs, err := newFDocs(conf, ft, blobStore)
fdocs, err := newFDocs(conf, ft, blobStore, kvStore)
if err != nil {
return nil, err
}
@@ -284,14 +284,14 @@ type FDocs struct {
sync.Mutex
}

func newFDocs(conf *config.Config, ft *filetree.FileTree, bs store.BlobStore) (*FDocs, error) {
func newFDocs(conf *config.Config, ft *filetree.FileTree, bs store.BlobStore, kv store.KvStore) (*FDocs, error) {
fdocs := &FDocs{
config: conf,
L: lua.NewState(),
}

// Load the "filetree" module
filetreeLua.Setup(fdocs.L, ft, bs)
filetreeLua.Setup(fdocs.L, ft, bs, kv)
// FIXME(tsileo): Setup the Glue "std" lib from gluapp with HTTP client,...
gluapp.SetupGlue(fdocs.L, &gluapp.Config{})

@@ -355,15 +355,15 @@ func setupCmd(cwd string) func(*lua.LState) int {
}
}

func newLuaHooks(conf *config.Config, ft *filetree.FileTree, bs store.BlobStore) (*LuaHooks, error) {
func newLuaHooks(conf *config.Config, ft *filetree.FileTree, bs store.BlobStore, kv store.KvStore) (*LuaHooks, error) {
hooks := &LuaHooks{
config: conf,
L: lua.NewState(),
hooks: map[string]map[string]*LuaHook{},
}

// Load the "filetree" module
filetreeLua.Setup(hooks.L, ft, bs)
filetreeLua.Setup(hooks.L, ft, bs, kv)
// FIXME(tsileo): better CWD
util.Setup(hooks.L, "/tmp")
if c := conf.Docstore; c != nil {
@@ -91,7 +91,7 @@ func convertNode(L *lua.LState, ft *filetree.FileTree, node *filetree.Node) *lua
return tbl
}

func setupFileTree(ft *filetree.FileTree, bs store.BlobStore) func(*lua.LState) int {
func setupFileTree(ft *filetree.FileTree, bs store.BlobStore, kv store.KvStore) func(*lua.LState) int {
return func(L *lua.LState) int {
// register functions to the table
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{
@@ -146,6 +146,24 @@ func setupFileTree(ft *filetree.FileTree, bs store.BlobStore) func(*lua.LState)
L.Push(convertNode(L, ft, node))
return 1

},
"fs_by_name_at": func(L *lua.LState) int {
fs, err := ft.FS(context.TODO(), L.ToString(1), filetree.FSKeyFmt, false, 0)
if err != nil {
panic(err)
}
node, _, _, err := fs.Path(context.TODO(), L.ToString(2), 1, false, 0)
if err != nil {
if err == blobsfile.ErrBlobNotFound {
L.Push(lua.LNil)
return 1
}

panic(err)
}
L.Push(convertNode(L, ft, node))
return 1

},
"fs": func(L *lua.LState) int {
fs := filetree.NewFS(L.ToString(1), ft)
@@ -165,13 +183,21 @@ func setupFileTree(ft *filetree.FileTree, bs store.BlobStore) func(*lua.LState)
if err != nil {
panic(err)
}
spath := ""
pathTable := L.CreateTable(len(path), 0)
for _, nodeInfo := range path {
if nodeInfo.Name != "_root" {
spath = spath + "/" + nodeInfo.Name
}
pathTable.Append(buildFSInfo(L, nodeInfo.Name, nodeInfo.Ref, ""))
}
if node.Name != "_root" {
spath = spath + "/" + node.Name
}
L.Push(convertNode(L, ft, node))
L.Push(pathTable)
return 2
L.Push(lua.LString(spath))
return 3
},
"put_file": func(L *lua.LState) int {
uploader := writer.NewUploader(filetree.NewBlobStoreCompat(bs, context.TODO()))
@@ -196,6 +222,43 @@ func setupFileTree(ft *filetree.FileTree, bs store.BlobStore) func(*lua.LState)
L.Push(lua.LString(ref))
return 1
},
"put_file_at": func(L *lua.LState) int {
uploader := writer.NewUploader(filetree.NewBlobStoreCompat(bs, context.TODO()))
name := L.ToString(1)
contents := L.ToString(2)
var ref string
node, err := uploader.PutReader(name, strings.NewReader(contents), nil)
if err != nil {
panic(err)
}

ref = node.Hash
ctx := context.TODO()

fs, err := ft.FS(ctx, L.ToString(3), filetree.FSKeyFmt, false, 0)
if err != nil {
panic(err)
}

t := time.Now().Unix()
parentNode, _, _, err := fs.Path(ctx, L.ToString(4), 1, true, t)
if err != nil {
panic(err)
}
if parentNode.Type != rnode.Dir {
panic("only dir can be patched")
}

newParent, _, err := ft.AddChild(ctx, parentNode, node, filetree.FSKeyFmt, t)
if err != nil {
panic(err)
}

fmt.Printf("newParent=%+v\nnew file ref=%s\n", newParent, ref)
L.Push(lua.LString(newParent.Hash))
L.Push(lua.LString(ref))
return 2
},
})
// returns the module
L.Push(mod)
@@ -204,6 +267,6 @@ func setupFileTree(ft *filetree.FileTree, bs store.BlobStore) func(*lua.LState)
}

// Setup loads the filetree Lua module
func Setup(L *lua.LState, ft *filetree.FileTree, bs store.BlobStore) {
L.PreloadModule("filetree", setupFileTree(ft, bs))
func Setup(L *lua.LState, ft *filetree.FileTree, bs store.BlobStore, kv store.KvStore) {
L.PreloadModule("filetree", setupFileTree(ft, bs, kv))
}

0 comments on commit b5ede4e

Please sign in to comment.
You can’t perform that action at this time.