tInitial commit - cdb - Constant database manipulation utility
 (HTM) git clone git://git.z3bra.org/cdb.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit 80b018bc5af6f06e344934ce82ee8cdf7584407a
 (HTM) Author: Willy Goiffon <dev@z3bra.org>
       Date:   Wed, 31 Aug 2022 17:57:09 +0200
       
       Initial commit
       
       Diffstat:
         A cdb.1                               |      54 +++++++++++++++++++++++++++++++
         A cdb.go                              |      99 +++++++++++++++++++++++++++++++
         A config.mk                           |       2 ++
         A go.mod                              |       5 +++++
         A go.sum                              |       2 ++
         A mkfile                              |      21 +++++++++++++++++++++
       
       6 files changed, 183 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/cdb.1 b/cdb.1
       t@@ -0,0 +1,54 @@
       +.Dd 2022-08-31
       +.Dt CDB 1
       +.Os POSIX.1-2017
       +.Sh NAME
       +.Nm cdb
       +.Nd Constant database utility
       +.Sh SYNOPSIS
       +.Nm cdb
       +.Op Fl hdm
       +.Op Fl k Ar key Op Fl s Ar n
       +.Ar file
       +.Sh DESCRIPTION
       +.Nm
       +Utility to make, dump and query constant databases.
       +.Bl -tag -width Ds
       +.It Fl h
       +Print a short help text
       +.It Fl d
       +Dump records from database
       +.Pa file
       +to
       +.Pa stdout .
       +See
       +.Sx RECORD FORMAT
       +.It Fl m
       +Make database
       +.Pa file
       +from encoded records read from
       +.Pa stdin .
       +.It Fl k Ar key
       +Query database
       +.Pa file
       +for record
       +.Ar key .
       +Only the first match is returned. To search subsequent match, see
       +.Fl s .
       +.It Fl s Ar n
       +Skip the first
       +.Ar n
       +records from the database, and display the next one.
       +.El
       +.Ss RECORD FORMAT
       +A dump record is encoded as
       +.Li +klen,dlen:key->data
       +followed by a newline. Here
       +.Em klen
       +is the number of bytes in key and
       +.Em dlen
       +is the number of bytes in data. The end of data is indicated by an
       +extra newline.
       +.Sh SEE ALSO
       +.Xr http://cr.yp.to/cdb.html
       +.Sh AUTHOR
       +.An Willy Goiffon Aq Mt dev@z3bra.org
 (DIR) diff --git a/cdb.go b/cdb.go
       t@@ -0,0 +1,99 @@
       +package main
       +
       +import (
       +        "bufio"
       +        "flag"
       +        "fmt"
       +        "io/ioutil"
       +        "log"
       +        "os"
       +        "path"
       +
       +        "github.com/jbarham/cdb"
       +)
       +
       +func exitOnErr(err error) {
       +        if err != nil {
       +                log.Fatal(err)
       +        }
       +}
       +
       +func usage() {
       +        fmt.Fprint(os.Stderr, "usage: cdb [-dm] [-k key [-s n]] db\n")
       +        os.Exit(2)
       +}
       +
       +func dumpdb(out *bufio.Writer, in *bufio.Reader) {
       +        err := cdb.Dump(out, in)
       +        out.Flush()
       +        if err != nil {
       +                os.Exit(111)
       +        }
       +}
       +
       +
       +func makedb(db string, tmp *os.File) {
       +        tmpname := tmp.Name()
       +
       +        exitOnErr(cdb.Make(tmp, bufio.NewReader(os.Stdin)))
       +        exitOnErr(tmp.Sync())
       +        exitOnErr(tmp.Close())
       +        exitOnErr(os.Rename(tmpname, db))
       +}
       +
       +func get(db string, key string, skip int64) {
       +        c, err := cdb.Open(db)
       +        if err != nil {
       +                log.Fatal(err)
       +        }
       +        defer c.Close()
       +        data, err:= c.Data([]byte(key))
       +        if err != nil {
       +                log.Fatal(err)
       +        }
       +        fmt.Printf("%s\n", string(data))
       +}
       +
       +func main() {
       +        var tmp *os.File
       +        var err error
       +
       +        var key string
       +        var db string
       +        var skip int64
       +        var dump bool
       +        var make bool
       +
       +        flag.StringVar(&key, "k", "", "Print record with key k")
       +        flag.Int64Var(&skip, "s", 0, "Skip the first s matching records")
       +        flag.BoolVar(&dump, "d", false, "Print records suitable for cbdmake")
       +        flag.BoolVar(&make, "m", false, "Writes a constant database to f")
       +        flag.Parse()
       +
       +        args := flag.Args()
       +
       +        if (len(args) != 1) {
       +                usage()
       +        }
       +
       +        db = args[0]
       +
       +        if (make) {
       +                dir, _ := path.Split(db)
       +                tmp, err = ioutil.TempFile(dir, ".*.cdb")
       +                exitOnErr(err)
       +                makedb(db, tmp)
       +                os.Exit(0)
       +        } else if (dump) {
       +                in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
       +                f, err := os.OpenFile(db, os.O_RDWR|os.O_CREATE, 0644)
       +                if err != nil {
       +                        log.Fatal(err)
       +                }
       +                in = bufio.NewReader(f)
       +                dumpdb(out, in)
       +                os.Exit(0)
       +        }
       +
       +        get(db, key, skip)
       +}
 (DIR) diff --git a/config.mk b/config.mk
       t@@ -0,0 +1,2 @@
       +GO   = go
       +GOOS = `{uname -s | tr A-Z a-z}
 (DIR) diff --git a/go.mod b/go.mod
       t@@ -0,0 +1,5 @@
       +module git.z3bra.org/cdb
       +
       +go 1.19
       +
       +require github.com/jbarham/cdb v0.0.0-20200301055225-9d6f6caadef0
 (DIR) diff --git a/go.sum b/go.sum
       t@@ -0,0 +1,2 @@
       +github.com/jbarham/cdb v0.0.0-20200301055225-9d6f6caadef0 h1:UfCOnKxwt2dxueylTrrjjyMsaXPHTSJJygePRYaRntE=
       +github.com/jbarham/cdb v0.0.0-20200301055225-9d6f6caadef0/go.mod h1:ColEidrii1lqlFhoEckJfZsa0mWxC0I2+f7G/5hZWsw=
 (DIR) diff --git a/mkfile b/mkfile
       t@@ -0,0 +1,21 @@
       +<config.mk
       +
       +all:V: cdb
       +
       +%: %.go
       +        $GO build -o $stem $stem.go
       +
       +clean:V:
       +        rm -f cdb
       +
       +install:V: cdb
       +        mkdir -p ${DESTDIR}${PREFIX}/bin
       +        cp cdb ${DESTDIR}${PREFIX}/bin/cdb
       +        chmod 755 ${DESTDIR}${PREFIX}/bin/cdb
       +        mkdir -p ${DESTDIR}${MANDIR}/man1
       +        cp cdb.1 ${DESTDIR}${MANDIR}/man1/cdb.1
       +        chmod 644 ${DESTDIR}${MANDIR}/man1/cdb.1
       +
       +uninstall:V:
       +        rm ${DESTDIR}${PREFIX}/bin/cdb
       +        rm ${DESTDIR}${MANDIR}/man1/cdb.1