go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit c18958992e9fefba6a1f269b4f0e4b86318173b6
parent 7bb678ca54afa29cd8a7073ef968384828c1614f
Author: lash <dev@holbrook.no>
Date:   Wed, 28 Aug 2024 00:26:09 +0100

Add gdbm populator

Diffstat:
Adev/gdbm/main.go | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mgo.mod | 3++-
Mgo.sum | 2++
3 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/dev/gdbm/main.go b/dev/gdbm/main.go @@ -0,0 +1,99 @@ +package main + +import ( + "flag" + "fmt" + "io" + "io/fs" + "log" + "os" + "path/filepath" + "path" + + gdbm "github.com/graygnuorg/go-gdbm" +) + +var ( + binaryPrefix = ".bin" + templatePrefix = "" + scan = make(map[string]string) +) + +const ( + RESOURCETYPE_UNKNOWN = iota + RESOURCETYPE_BIN + RESOURCETYPE_TEMPLATE +) + +type scanner struct { + db *gdbm.Database +} + +func newScanner(fp string) (*scanner, error) { + db, err := gdbm.Open(fp, gdbm.ModeNewdb) + if err != nil { + return nil, err + } + return &scanner{ + db: db, + }, nil +} + +func(sc *scanner) Close() error { + return sc.db.Close() +} + +func(sc *scanner) Scan(fp string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + typ := RESOURCETYPE_UNKNOWN + if d.IsDir() { + return nil + } + fx := path.Ext(fp) + fb := path.Base(fp) + switch fx { + case binaryPrefix: + typ = RESOURCETYPE_BIN + case templatePrefix: + typ = RESOURCETYPE_TEMPLATE + default: + log.Printf("skip foreign file: %s", fp) + return nil + } + f, err := os.Open(fp) + defer f.Close() + if err != nil{ + return err + } + v, err := io.ReadAll(f) + if err != nil{ + return err + } + + ft := path.Base(fb) + k := []byte{uint8(typ)} + k = append(k, []byte(ft)...) + return sc.db.Store(k, v, true) +} + +func main() { + var dir string + var dbPath string + flag.StringVar(&dbPath, "d", "vise.gdbm", "database file path") + flag.Parse() + + dir = flag.Arg(0) + + o, err := newScanner(dbPath) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to open scanner") + os.Exit(1) + } + err = filepath.WalkDir(dir, o.Scan) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to open scanner") + os.Exit(1) + } +} diff --git a/go.mod b/go.mod @@ -6,11 +6,12 @@ require ( github.com/alecthomas/participle/v2 v2.0.0 github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c github.com/fxamacker/cbor/v2 v2.4.0 + github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 github.com/peteole/testdata-loader v0.3.0 + gopkg.in/leonelquinteros/gotext.v1 v1.3.1 ) require ( github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a // indirect github.com/x448/float16 v0.8.4 // indirect - gopkg.in/leonelquinteros/gotext.v1 v1.3.1 // indirect ) diff --git a/go.sum b/go.sum @@ -6,6 +6,8 @@ github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YV github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U= github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY= github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk=