Skip to content
Permalink
Browse files

lua,luascripts: manage Lua scripts in separate files

  • Loading branch information...
tsileo committed Oct 9, 2018
1 parent cbee5b2 commit 376e8750488d4c1d597476b7ddcfcf22c208cdb4
Showing with 109 additions and 39 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .travis.yml
  3. +4 −2 README.md
  4. +1 −0 blobstash.go
  5. +4 −0 lua/filetree_expr_search.lua
  6. +33 −0 lua/stash_gc.lua
  7. +3 −0 lua/test.lua
  8. +26 −0 pkg/luascripts/luascripts.go
  9. +29 −0 pkg/luascripts/luascripts_test.go
  10. +7 −37 pkg/stash/gc/gc.go
@@ -11,3 +11,4 @@ config.json
draft_*
tmp_*
ldb_*
pkg/luascripts/files.go
@@ -13,6 +13,7 @@ script:
# Run golang unit tests
# - go get -v a4.io/blobstash/...
# - cd $GOPATH/src/a4.io/blobstash
- go generate
- go install
- cp -r $GOPATH/bin/blobstash .
- go test $(go list ./... | grep pkg)
@@ -197,11 +197,13 @@ $ http --auth :apikey GET https://myinstance.com/api/git/myns

#### Extra module

- [`extra.glob(pattern, name)`](#extraglob)
- [`extra.glob(pattern, name)`](#extraglobpattern-name)

##### extra.glob(pattern, name)

Parse URL into a table of key/value components.
Parses the shell file name pattern/glob and reports wether the file name matches.

Uses go's [filepath.Match](https://godoc.org/path/filepath#Match).

**Attributes**

@@ -1,3 +1,4 @@
//go:generate go run tools/genluaindex.go
package main

import (
@@ -0,0 +1,4 @@
-- Used as a "match func" when searching within a FileTree tree
return function(node, contents)
if {{.expr}} then return true else return false end
end
@@ -0,0 +1,33 @@
local msgpack = require('msgpack')
local kvstore = require('kvstore')
local blobstore = require('blobstore')
local node = require('node')

-- Setup the `mark_kv` and `mark_filetree` global helper for the GC API
function mark_kv (key, version)
local h = kvstore.get_meta_blob(key, version)
if h ~= nil then
mark(h)
local _, ref, _ = kvstore.get(key, version)
if ref ~= '' then
mark(ref)
end
end
end
_G.mark_kv = mark_kv

function mark_filetree_node (ref)
local data = blobstore.get(ref)
local cnode = node.decode(data)
mark(ref)
if cnode.t == 'dir' then
for _, childRef in ipairs(cnode.r) do
mark_filetree_node(childRef)
end
else
for _, contentRef in ipairs(cnode.r) do
mark(contentRef[2])
end
end
end
_G.mark_filetree_node = mark_filetree_node
@@ -0,0 +1,3 @@
return function()
return {{.expr}}
end
@@ -0,0 +1,26 @@
package luascripts // import "a4.io/blobstash/pkg/luascripts"

import (
"bytes"
"fmt"
"text/template"
)

func Get(name string) string {
dat, ok := files[name]
if !ok {
panic(fmt.Sprintf("missing file %s", name))
}
return dat
}

type Ctx map[string]interface{}

func Tpl(name string, ctx Ctx) string {
tpl := template.Must(template.New("").Parse(Get(name)))
var buf bytes.Buffer
if err := tpl.Execute(&buf, ctx); err != nil {
panic(err)
}
return buf.String()
}
@@ -0,0 +1,29 @@
package luascripts

import (
"testing"
)

const testLuaFile = `return function()
return {{.expr}}
end
`

const testLuaFileExecuted = `return function()
return true
end
`

func TestGet(t *testing.T) {
dat := Get("test.lua")
if dat != testLuaFile {
t.Errorf("failed to get test.lua, %q", dat)
}
}

func TestTpl(t *testing.T) {
dat := Tpl("test.lua", Ctx{"expr": "true"})
if dat != testLuaFileExecuted {
t.Errorf("failed to get test.lua, %q", dat)
}
}
@@ -10,9 +10,11 @@ import (
"a4.io/blobstash/pkg/apps/luautil"
"a4.io/blobstash/pkg/blob"
bsLua "a4.io/blobstash/pkg/blobstore/lua"
"a4.io/blobstash/pkg/extra"
"a4.io/blobstash/pkg/filetree/filetreeutil/node"
"a4.io/blobstash/pkg/hub"
kvsLua "a4.io/blobstash/pkg/kvstore/lua"
"a4.io/blobstash/pkg/luascripts"
"a4.io/blobstash/pkg/stash"
)

@@ -39,50 +41,18 @@ func GC(ctx context.Context, h *hub.Hub, s *stash.Stash, script string, remoteRe
L.PreloadModule("node", loadNode)
kvsLua.Setup(L, s.KvStore(), ctx)
bsLua.Setup(ctx, L, s.BlobStore())
extra.Setup(L)

if err := L.DoString(`
local msgpack = require('msgpack')
local kvstore = require('kvstore')
local blobstore = require('blobstore')
local node = require('node')
function mark_kv (key, version)
local h = kvstore.get_meta_blob(key, version)
if h ~= nil then
mark(h)
local _, ref, _ = kvstore.get(key, version)
if ref ~= '' then
mark(ref)
end
end
end
_G.mark_kv = mark_kv
function mark_filetree_node (ref)
local data = blobstore.get(ref)
local cnode = node.decode(data)
mark(ref)
if cnode.t == 'dir' then
for _, childRef in ipairs(cnode.r) do
mark_filetree_node(childRef)
end
else
for _, contentRef in ipairs(cnode.r) do
mark(contentRef[2])
end
end
end
_G.mark_filetree_node = mark_filetree_node
`); err != nil {
// Setup two global:
// - mark_kv(key, version) -- version must be a String because we use nano ts
// - mark_filetree_node(ref)
if err := L.DoString(luascripts.Get("stash_gc.lua")); err != nil {
return err
}
// FIXME(tsileo): do like in the docstore, export code _G.mark_kv(key, version), _G.mark_fs_ref(ref)...
// and the option to load custom GC script from the filesystem like stored queries

if err := L.DoString(script); err != nil {
return err
}
fmt.Printf("Remote refs=%+v\n\nrefs=%+v\n\n", remoteRefs, refs)
for ref, _ := range refs {
// FIXME(tsileo): stat before get/put

0 comments on commit 376e875

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