44 Commits

Author SHA1 Message Date
9afca91159 fixed crash in api list, added filter support to api list 2025-01-01 18:08:43 +01:00
T.v.Dein
675d10d8fa Merge pull request #10 from TLINDEN/proto
Use protobuf for internal data storage instead of json
2024-12-30 12:33:03 +01:00
659e3472bb fixed copylocks linter warning 2024-12-30 12:23:13 +01:00
1eb5efae0c finalized conversion to protobuf:
- fixed import+export
- generalized file options
- always store keys as lowercase
- fixed+enhanced docs
- fixed tests
2024-12-30 12:12:02 +01:00
Thomas von Dein
bb5c268ca8 todo 2024-12-29 23:29:23 +01:00
a4b6a3cfdf restructured data storage, values now have their own sub bucket 2024-12-29 18:29:43 +01:00
c144e99b41 switch to use protobuf for internal data structure in DB 2024-12-29 13:01:16 +01:00
Thomas von Dein
2a6a651b91 fix pod bug 2024-12-25 23:53:34 +01:00
Thomas von Dein
33d638aff0 fix typos and formatting 2024-12-23 23:51:17 +01:00
8a8888a3cf +badges 2024-12-23 20:02:38 +01:00
a590eaf903 +todo 2024-12-23 20:00:48 +01:00
cb6abecaf9 +man page badge 2024-12-23 19:58:51 +01:00
a777c9cab4 added -i option to list to search case insensitive 2024-12-23 19:49:42 +01:00
2f652dc57d todo 2024-12-23 19:31:50 +01:00
T.v.Dein
f5f3760e67 Merge pull request #9 from TLINDEN/edit
Updated manual
2024-12-23 16:23:12 +01:00
T.v.Dein
2253acf19d Merge branch 'main' into edit 2024-12-23 16:21:26 +01:00
b7e3267695 updated manual 2024-12-23 16:19:27 +01:00
Thomas von Dein
6adeb618b7 add dbtool note 2024-12-22 21:12:16 +01:00
b262c73746 added edit command, fixed set -o - 2024-12-22 17:50:40 +01:00
66d6bd2a41 added edit command, fixed set -o - 2024-12-22 17:46:44 +01:00
fe3951f3c2 more doc, fix format str 2024-12-22 13:28:08 +01:00
249c3f1cfb bump version 2024-12-22 13:28:08 +01:00
8687e084bf fixes and additions:
- add ANYDB_PASSWORD env var
- add config file support, including buckets dict
- finalized custom bucket support
- fine tuned info support
2024-12-22 13:28:08 +01:00
Thomas von Dein
24240b85f2 fix error checking 2024-12-22 13:28:08 +01:00
Thomas von Dein
8e400c6831 added db info command 2024-12-22 13:28:08 +01:00
Thomas von Dein
3de65aa1c3 add custom bucket support 2024-12-22 13:28:08 +01:00
be79886e89 refactor Import() 2024-12-22 11:29:12 +01:00
T.v.Dein
dc328afa44 Update TODO.md 2024-12-21 09:55:52 +01:00
T.v.Dein
cfa739ac83 Update TODO.md 2024-12-21 07:57:35 +01:00
Thomas von Dein
6de8a6168a add GPL headers 2024-12-21 00:08:16 +01:00
Thomas von Dein
8d04f798e0 done 2024-12-20 23:27:16 +01:00
Thomas von Dein
c414a8972a bump version 2024-12-20 23:27:16 +01:00
Thomas von Dein
e6f20f5623 add rest sample 2024-12-20 23:27:16 +01:00
Thomas von Dein
41d9bd6e07 remove requestid, nonsense in our context 2024-12-20 23:27:16 +01:00
Thomas von Dein
e392aa924f fix serve return error, rename to crud.go 2024-12-20 23:27:16 +01:00
Thomas von Dein
a49aa5b8d0 add set method, add list json req support 2024-12-20 23:27:16 +01:00
Thomas von Dein
6e3fb4ef91 add set method, add list json req support 2024-12-20 23:27:16 +01:00
7bc30da609 started with serve command, yet incomplete 2024-12-20 23:27:16 +01:00
37ca653461 updated docker howto 2024-12-20 12:20:53 +01:00
1119b06330 fix formatting 2024-12-20 12:08:25 +01:00
f93b7bec08 fix error reporting 2024-12-20 11:55:07 +01:00
d94868132d added -N flag, added -m template support to get and list commands 2024-12-20 11:53:09 +01:00
ba39e3f8cd added -N flag, added -m template support to get and list commands 2024-12-20 11:51:57 +01:00
b7111002f1 add docker doc 2024-12-19 11:30:35 +01:00
36 changed files with 3689 additions and 354 deletions

View File

@@ -1,3 +1,19 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
FROM golang:1.22-alpine as builder
RUN apk update

View File

@@ -29,7 +29,7 @@ BUILD = $(shell date +%Y.%m.%d.%H%M%S)
VERSION := $(if $(filter $(BRANCH), development),$(version)-$(BRANCH)-$(COMMIT)-$(BUILD),$(version))
HAVE_POD := $(shell pod2text -h 2>/dev/null)
all: $(tool).1 cmd/$(tool).go buildlocal
all: $(tool).1 cmd/$(tool).go app/dbentry.pb.go buildlocal
%.1: %.pod
ifdef HAVE_POD
@@ -49,6 +49,11 @@ endif
# awk '/SYNOPS/{f=1;next} /DESCR/{f=0} f' $*.pod | sed 's/^ //' >> cmd/$*.go
# echo "\`" >> cmd/$*.go
app/dbentry.pb.go: app/dbentry.proto
protoc -I=. --go_out=app app/dbentry.proto
mv app/github.com/tlinden/anydb/app/dbentry.pb.go app/dbentry.pb.go
rm -rf app/github.com
buildlocal:
go build -ldflags "-X 'github.com/tlinden/anydb/cfg.VERSION=$(VERSION)'"
@@ -66,11 +71,11 @@ clean:
rm -rf $(tool) releases coverage.out
test:
go test -v ./...
ANYDB_PASSWORD=test go test -v ./...
singletest:
@echo "Call like this: ''make singletest TEST=TestPrepareColumns MOD=lib"
go test -run $(TEST) github.com/tlinden/anydb/$(MOD)
ANYDB_PASSWORD=test go test -run $(TEST) github.com/tlinden/anydb/$(MOD)
cover-report:
go test ./... -cover -coverprofile=coverage.out

112
README.md
View File

@@ -2,7 +2,10 @@
[![Actions](https://github.com/tlinden/anydb/actions/workflows/ci.yaml/badge.svg)](https://github.com/tlinden/anydb/actions)
[![License](https://img.shields.io/badge/license-GPL-blue.svg)](https://github.com/tlinden/anydb/blob/master/LICENSE)
[![Go Coverage](https://github.com/tlinden/anydb/wiki/coverage.svg)](https://raw.githack.com/wiki/tlinden/anydb/coverage.html)
[![Go Report Card](https://goreportcard.com/badge/github.com/tlinden/anydb)](https://goreportcard.com/report/github.com/tlinden/anydb)
[![GitHub release](https://img.shields.io/github/v/release/tlinden/anydb?color=%2300a719)](https://github.com/TLINDEN/anydb/releases/latest)
[![Documentation](https://img.shields.io/badge/manpage-documentation-blue)](https://github.com/TLINDEN/anydb/blob/master/anydb.pod)
Anydb is a simple to use commandline tool to store anything you'd
like, even binary files etc. It is a re-implementation of
@@ -16,6 +19,7 @@ reasons:
often, which is not good for a tool intended to be used for many
years.
- more features:
- output table in list mode uses TAB separator
- better STDIN + pipe support
- supports JSON output
- supports more verbose tabular output
@@ -23,6 +27,10 @@ reasons:
- tagging
- filtering using tags
- encryption of entries
- templates for custom output for maximum flexibility
- includes a tiny web server, which serves a restful API
And I wrote a very similar [tool](https://www.daemon.de/projects/dbtool/) 24 years ago and wanted to do it again wit go.
**anydb** can do all the things you can do with skate:
@@ -68,33 +76,101 @@ anydb set foo bar -t note,important
anydb list -t important
# beside tags filtering you can also use regexps for searching
# note, by default the list command only searches through keys
anydb list '[a-z]+\d'
# do a full text search
anydb list '[a-z]+\d' -s
# anydb also supports a wide output
anydb list -o wide
anydb list -m wide
KEY TAGS SIZE AGE VALUE
blah important 4 B 7 seconds ago haha
foo 3 B 15 seconds ago bar
猫咪 3 B 3 seconds ago 喵
foo 3 B 15 seconds ago bar
猫咪 3 B 3 seconds ago 喵
# there are shortcuts as well
anydb ls -l
anydb /
# other outputs are possible as well
anydb list -o json
anydb list -m json
# you can backup your database
anydb export -o backup.json
# and import it somewhere else
anydb import -r backup.json
anydb import -i backup.json
# you can encrypt entries. anydb asks for a passphrase
# and will do the same when you retrieve the key using the
# get command.
# get command. anydb will ask you interactively for a password
anydb set mypassword -e
# but you can provide it via an environment variable too
ANYDB_PASSWORD=foo anydb set -e secretkey blahblah
# too tiresome to add -e every time you add an entry?
# use a per bucket config
cat ~/.config/anydb/anydb.toml
[buckets.data]
encrypt = true
anydb set foo bar # will be encrypted
# speaking of buckets, you can use different buckets
anydb -b test set foo bar
# and speaking of configs, you can place a config file at these places:
# ~/.config/anydb/anydb.toml
# ~/.anydb.toml
# anydb.toml (current directory)
# or specify one using -c <filename>
# look at example.toml
# using template output mode you can freely design how to print stuff
# here, we print the values in CSV format ONLY if they have some tag
anydb ls -m template -T "{{ if .Tags }}{{ .Key }},{{ .Value }},{{ .Created}}{{ end }}"
# or, to simulate skate's -k or -v
anydb ls -m template -T "{{ .Key }}"
anydb ls -m template -T "{{ .Value }}"
# maybe you want to digest the item in a shell script? also
# note, that both the list and get commands support templates
eval $(anydb get foo -m template -T "key='{{ .Key }}' value='{{ .Value }}' ts='{{ .Created}}'")
echo "$key: $value"
# run the restful api server
anydb serve
# post a new key
curl -X PUT localhost:8787/anydb/v1/ \
-H 'Content-Type: application/json' \
-d '{"key":"foo","val":"bar"}'
# retrieve it
curl localhost:8787/anydb/v1/foo
# list keys
curl localhost:8787/anydb/v1/
# same, but do a full text search by content, searching for "foo"
curl -X POST http://127.0.0.1:8787/anydb/v1/ \
-H 'Content-Type: application/json'
-d '{"key":"foo", "fulltext": true}'
# as you might correctly suspect you can store multi-line values or
# the content of text files. but what to do if you want to change it?
# here's one way:
anydb get contract24 > file.txt && vi file.txt && anydb set contract24 -r file.txt
# annoying. better do this
anydb edit contract24
# sometimes you need to know some details about the current database
# add -d for more details
anydb info
# it comes with a manpage builtin
anydb man
```
@@ -130,6 +206,30 @@ There are multiple ways to install **anydb**:
If you do not find a binary release for your platform, please don't
hesitate to ask me about it, I'll add it.
### Using the docker image
A pre-built docker image is available, which you can use to test the
app without installing it. To download:
```shell
docker pull ghcr.io/tlinden/anydb:latest
```
To execute anydb inside the image do something like this:
```shell
mkdir mydb
docker run -ti -v mydb:/db -u `id -u $USER` -e HOME=/db ghcr.io/tlinden/anydb:latest set foo bar
docker run -ti -v mydb:/db -u `id -u $USER` -e HOME=/db ghcr.io/tlinden/anydb:latest list -o wide
```
Here, we operate in a local directory `mydb`, which we'll use as HOME
inside the docker container. anydb will store its database in
`mydb/.config/anydb/default.db`.
A list of available images is [here](https://github.com/tlinden/anydb/pkgs/container/anydb/versions?filters%5Bversion_type%5D=tagged)
## Documentation
The documentation is provided as a unix man-page. It will be

19
TODO.md Normal file
View File

@@ -0,0 +1,19 @@
## Features
- repl
- mime-type => exec app + value
- add waitgroup to db.go funcs
- RestList does not support any params?
## DB Structure
- put tags into sub bucket see #1
tags bucket
key/tag => tag/key
tag/key => tag
A delete would just delete all keys from all values and then:
lookup in tags bucket for all key/*, then iterate over the values and
remove all tag/key's. Then deleting a key would not leave any residue
behind.

588
anydb.1
View File

@@ -1,4 +1,4 @@
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40)
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.42)
.\"
.\" Standard preamble:
.\" ========================================================================
@@ -133,7 +133,7 @@
.\" ========================================================================
.\"
.IX Title "ANYDB 1"
.TH ANYDB 1 "2024-12-18" "1" "User Commands"
.TH ANYDB 1 "2025-01-01" "1" "User Commands"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
@@ -151,14 +151,20 @@ anydb \- a personal key value store
\& Available Commands:
\& completion Generate the autocompletion script for the specified shell
\& del Delete key
\& edit Edit a key
\& export Export database to json
\& get Retrieve value for a key
\& help Help about any command
\& import Import database dump
\& info info
\& list List database contents
\& man show manual page
\& serve run REST API listener
\& set Insert key/value pair
\&
\& Flags:
\& \-b, \-\-bucket string use other bucket (default: data) (default "data")
\& \-c, \-\-config string toml config file
\& \-f, \-\-dbfile string DB file to use (default "/home/scip/.config/anydb/default.db")
\& \-d, \-\-debug Enable debugging
\& \-h, \-\-help help for anydb
@@ -168,9 +174,581 @@ anydb \- a personal key value store
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Anydb is a simple to use commandline tool to store anything you'd
like, even binary files etc. It uses a key/value store (bbolt) in your
home directory.
Anydb is a commandline personal key value store, it is simple to use
and can be used to store anything you'd like, even binary files
etc. It uses a key/value store (bbolt) in your home directory.
.PP
The tool provides a number of subcommands to use it, there are global
options and each subcommand has its own set of options.
.SH "GLOBAL OPTIONS"
.IX Header "GLOBAL OPTIONS"
.ie n .IP """\-f, \-\-dbfile filename""" 4
.el .IP "\f(CW\-f, \-\-dbfile filename\fR" 4
.IX Item "-f, --dbfile filename"
The default location of your databas is
\&\f(CW\*(C`$HOME/.config/anydb/default.db\*(C'\fR. You can change this with the \f(CW\*(C`\-f\*(C'\fR
option.
.ie n .IP """\-b, \-\-bucket name""" 4
.el .IP "\f(CW\-b, \-\-bucket name\fR" 4
.IX Item "-b, --bucket name"
Data in a bbolt key-value-store are managed in so called
buckets. These are kind of namespaces, where each key must be
unique. However, a database may contain more than one bucket.
.Sp
By default anydb uses a bucket named \*(L"data\*(R", but you can change this
using the option \f(CW\*(C`\-b\*(C'\fR.
.Sp
Buckets can be configured to always encrypt values, see \s-1ENCRYTPTION\s0.
.ie n .IP """\-c, \-\-config filename""" 4
.el .IP "\f(CW\-c, \-\-config filename\fR" 4
.IX Item "-c, --config filename"
Under normal circumstances you don't need a configuration file. But if
you want, you can provide one using the option \f(CW\*(C`\-c\*(C'\fR.
.Sp
Anydb looks for a couple of default locations for a config file. You
only need this option if you want to supply a configuration on a
non-standard location. See \s-1CONFIGURATION\s0 for more details.
.ie n .IP """\-d, \-\-debug""" 4
.el .IP "\f(CW\-d, \-\-debug\fR" 4
.IX Item "-d, --debug"
Enable debug output.
.ie n .IP """\-h, \-\-help""" 4
.el .IP "\f(CW\-h, \-\-help\fR" 4
.IX Item "-h, --help"
Show the usage of anydb.
.ie n .IP """\-v, \-\-version""" 4
.el .IP "\f(CW\-v, \-\-version\fR" 4
.IX Item "-v, --version"
Show the program version.
.PP
All of these options can be used with subcommands as well.
.SH "SUBCOMMANDS"
.IX Header "SUBCOMMANDS"
.SS "completion"
.IX Subsection "completion"
The \fBcompletion\fR command can be used to setup completion for
anydb. Just put something like this into your shell's configuration
file:
.PP
.Vb 1
\& source <(anydb completion bash)
.Ve
.PP
If you use another shell, specify it instead of bash, of course.
.SS "set"
.IX Subsection "set"
The \fBset\fR command is being used to insert or update a key-value pair.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb set <key> [<value> | \-r <file>] [\-t <tag>] [flags]
\&
\& Aliases:
\& set, add, s, +
\&
\& Flags:
\& \-e, \-\-encrypt encrypt value
\& \-r, \-\-file string Filename or \- for STDIN
\& \-h, \-\-help help for set
\& \-t, \-\-tags tag,tag,... tags, multiple allowed
.Ve
.PP
The standard way to insert a new entry is really simple:
.PP
.Vb 1
\& anydb set key value
.Ve
.PP
If you don't specify a value, anydb expects you to feed it some data
via \s-1STDIN.\s0 For example:
.PP
.Vb 1
\& anydb set key < file
.Ve
.PP
You might as well specify a file directly using the \f(CW\*(C`\-f\*(C'\fR option:
.PP
.Vb 1
\& anydb set key \-f file
.Ve
.PP
Values can be encrypted using \fBChaCha20Poly1305\fR when you specify the
\&\f(CW\*(C`\-e\*(C'\fR option. Anydb will ask you interactively for a passphrase. You
can also provide the passphrase using the environment variable
\&\f(CW\*(C`ANYDB_PASSWORD\*(C'\fR. To encrypt the value, a cryptographically secure
key will be derived from the passphrase using the ArgonID2
algorithm. Each value can be encrypted with another passphrase. So,
the database itself is not encrypted, just the values.
.PP
You can supply tags by using the option \f(CW\*(C`\-t\*(C'\fR. Multiple tags can be
provided either by separating them with a comma or by using multiple
\&\f(CW\*(C`\-t\*(C'\fR parameters:
.PP
.Vb 2
\& anydb set key value \-t tag1,tag2
\& anydb set key value \-t tag1 \-t tag2
.Ve
.PP
You can later filter entries by tag or by a combination of tags.
.PP
To edit or modify an entry, just use the \fBset\fR command with the same
key, the value in the database will be overwritten with the new
value. An alternative option is the \fBedit\fR command, see below.
.SS "get"
.IX Subsection "get"
To retrieve the value of a key, use the \fBget\fR subcommand.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb get <key> [\-o <file>] [\-m <mode>] [\-n \-N] [\-T <tpl>] [flags]
\&
\& Aliases:
\& get, show, g, .
\&
\& Flags:
\& \-h, \-\-help help for get
\& \-m, \-\-mode string output format (simple|wide|json|template) (default \*(Aqsimple\*(Aq)
\& \-n, \-\-no\-headers omit headers in tables
\& \-N, \-\-no\-human do not translate to human readable values
\& \-o, \-\-output string output value to file (ignores \-m)
\& \-T, \-\-template string go template for \*(Aq\-m template\*(Aq
.Ve
.PP
In its simplest form you just call the \fBget\fR subcommand with the key
you want to have the value for. The value is being printed to \s-1STDOUT\s0
by default:
.PP
.Vb 1
\& anydb get key
.Ve
.PP
If the value is binary content, it will not just being printed. In
those cases you need to either redirect output into a file or use the
option \f(CW\*(C`\-o\*(C'\fR to write to a file:
.PP
.Vb 2
\& anydb get key > file
\& anydb get key \-o file
.Ve
.PP
If the value is encrypted, you will be asked for the passphrase to
decrypt it. If the environment variable \f(CW\*(C`ANYDB_PASSWORD\*(C'\fR is set, its
value will be used instead.
.PP
There are different output modes you can choose from: simple, wide and
json. The \*(L"simple\*(R" mode is the default one, it just prints the value
as is. The \*(L"wide\*(R" mode prints a tabular output similar to the \fBlist\fR
subcommand, see there for more details. The options \f(CW\*(C`\-n\*(C'\fR and \f(CW\*(C`\-N\*(C'\fR
have the same meaning as in the list command. The \*(L"json\*(R" mode prints
the raw \s-1JSON\s0 representation of the whole database entry. Decryption
will only take place in \*(L"simple\*(R" and \*(L"json\*(R" mode. The \*(L"template\*(R" mode
provides the most flexibily, it is detailed in the section
\&\s-1TEMPLATES\s0.
.SS "list"
.IX Subsection "list"
The \fBlist\fR subcommand displays a list of all database entries.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb list [<filter\-regex> | \-t <tag> ] [\-m <mode>] [\-nNif] [\-T <tpl>] [flags]
\&
\& Aliases:
\& list, ls, /, find, search
\&
\& Flags:
\& \-i, \-\-case\-insensitive filter case insensitive
\& \-h, \-\-help help for list
\& \-m, \-\-mode string output format (table|wide|json|template), wide is a verbose table. (default \*(Aqtable\*(Aq)
\& \-n, \-\-no\-headers omit headers in tables
\& \-N, \-\-no\-human do not translate to human readable values
\& \-s, \-\-search\-fulltext perform a full text search
\& \-t, \-\-tags stringArray tags, multiple allowed
\& \-T, \-\-template string go template for \*(Aq\-m template\*(Aq
\& \-l, \-\-wide\-output output mode: wide
.Ve
.PP
In its simplest form \- without any options \- , the \fBlist\fR command
just prints all keys with their values to \s-1STDOUT.\s0 Values are being
truncated to maximum of 60 characters, that is, multiline values are
not completely shown in order to keep the tabular view readable.
.PP
To get more informations about each entry, use the \f(CW\*(C`\-o wide\*(C'\fR or \f(CW\*(C`\-l\*(C'\fR
option. In addition to the key and value also the size, update
timestamp and tags will be printed. Time and size values are converted
into a human readable form, you can suppress this behavior with the
\&\f(CW\*(C`\-N\*(C'\fR option. You may omit the headers using the option \f(CW\*(C`\-n\*(C'\fR
.PP
Sometimes you might want to filter the list of entries. Either because
your database grew too large or because you're searching for
something. In that case you have two options: You may supply one or
more tags or provide a filter regexp. To filter by tag, do:
.PP
.Vb 3
\& anydb list \-t tag1
\& anydb list \-t tag1,tag2
\& anydb list \-t tag1 \-t tag2
.Ve
.PP
To filter using a regular expression, do:
.PP
.Vb 1
\& anydb list "foo.*bar"
.Ve
.PP
Regular expressions follow the golang \fBre2\fR syntax. For more details
about the syntax, refer to
<https://github.com/google/re2/wiki/Syntax>. Please note, that this
regexp dialect is not \s-1PCRE\s0 compatible, but supports most of its
features.
.PP
If you want to search case insensitive, add the option \f(CW\*(C`\-i\*(C'\fR.
.PP
By default anydb only searches through the keys. If you want to search
through the values as well, then use the \f(CW\*(C`\-s\*(C'\fR option, which enables
full-text search.
.PP
You can \- as with the \fBget\fR command \- use other output modes. The
default mode is \*(L"table\*(R". The \*(L"wide\*(R" mode is, as already mentioned, a
more detailed table. Also supported is \*(L"json\*(R" mode and \*(L"template\*(R"
mode. For details about using templates see \s-1TEMPLATES\s0.
.SS "del"
.IX Subsection "del"
Use the \fBdel\fR command to delete database entries.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb del <key> [flags]
\&
\& Aliases:
\& del, d, rm
\&
\& Flags:
\& \-h, \-\-help help for del
.Ve
.PP
The subcommand \fBdel\fR does not provide any further options, it just
deletes the entry referred to by the given key. No questions are being
asked.
.SS "edit"
.IX Subsection "edit"
The \fBedit\fR command makes it easier to modify larger entries.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb edit <key> [flags]
\&
\& Aliases:
\& edit, modify, mod, ed, vi
\&
\& Flags:
\& \-h, \-\-help help for edit
.Ve
.PP
The subcommand \fBedit\fR does not provide any further options. It
works like this:
.IP "1. Write the value info a temporary file." 4
.IX Item "1. Write the value info a temporary file."
.PD 0
.IP "2. Execute the editor (which one, see below!) with that file." 4
.IX Item "2. Execute the editor (which one, see below!) with that file."
.IP "3. Now you can edit the file and save+close it when done." 4
.IX Item "3. Now you can edit the file and save+close it when done."
.IP "4. Anydb picks up the file and if the content has changed, puts its value into the \s-1DB.\s0" 4
.IX Item "4. Anydb picks up the file and if the content has changed, puts its value into the DB."
.PD
.PP
By default anydb executes the \f(CW\*(C`vi\*(C'\fR command. You can modify this
behavior by setting the environment variable \f(CW\*(C`EDITOR\*(C'\fR appropriately.
.PP
Please note, that this does not work with binary content!
.SS "export"
.IX Subsection "export"
Since the bbolt database file is not portable across platforms (it is
bound to the endianess of the \s-1CPU\s0 it was being created on), you might
want to create a backup file of your database. You can do this with
the \fBexport\fR subcommand.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb export \-o <json filename> [flags]
\&
\& Aliases:
\& export, dump, backup
\&
\& Flags:
\& \-h, \-\-help help for export
\& \-o, \-\-output string output to file
.Ve
.PP
The database dump is a \s-1JSON\s0 representation of the whole database and
will be printed to the file specified with the \f(CW\*(C`\-o\*(C'\fR option. If you
specify \*(L"\-\*(R" as the filename, it will be written to \s-1STDIN.\s0
.PP
.Vb 2
\& anydb export \-o dump.json
\& anydb export \-o \- > dump.json
.Ve
.PP
Please note, that encrypted values will not be decrypted. This might
change in a future version of anydb.
.SS "import"
.IX Subsection "import"
The \fBimport\fR subcommand can be used to restore a database from a \s-1JSON\s0
dump.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb import \-i <json file> [flags]
\&
\& Aliases:
\& import, restore
\&
\& Flags:
\& \-r, \-\-file string Filename or \- for STDIN
\& \-h, \-\-help help for import
\& \-t, \-\-tags stringArray tags, multiple allowed
.Ve
.PP
The \f(CW\*(C`import\*(C'\fR subcommand reads the \s-1JSON\s0 contents from
the file specified with the \f(CW\*(C`\-i\*(C'\fR option. If you specify \*(L"\-\*(R" as the
filename, it will be read from \s-1STDIN.\s0
.PP
.Vb 3
\& anydb import \-i \- < dump.json
\& anydb import \-i dump.json
\& cat dump.json | anydb import \-i \-
.Ve
.PP
If there is already a database, it will be saved by appending a
timestamp and a new database with the contents of the dump will be
created.
.SS "serve"
.IX Subsection "serve"
Anydb provides a RESTful \s-1API,\s0 which you can use to manage the database
from somewhere else. The \s-1API\s0 does not provide any authentication or
any other security measures, so better only use it on localhost.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb serve [\-l host:port] [flags]
\&
\& Flags:
\& \-h, \-\-help help for serve
\& \-l, \-\-listen string host:port (default "localhost:8787")
.Ve
.PP
To start the listener, just execute the \fBserve\fR subcommand. You can
tweak the ip address and tcp port using the \f(CW\*(C`\-l\*(C'\fR option. The listener
will not fork and run in the foreground. Logs are being printed to
\&\s-1STDOUT\s0 as long as the listener runs.
.PP
For more details about the \s-1API,\s0 please see the \*(L"\s-1REST API\*(R"\s0 section.
.SS "info"
.IX Subsection "info"
The \fBinfo\fR subcommand shows you some information about your current
database.
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb info [flags]
\&
\& Flags:
\& \-h, \-\-help help for info
\& \-N, \-\-no\-human do not translate to human readable values
.Ve
.PP
Data being shown are: filename and size, number of keys per bucket. If
you supply the \f(CW\*(C`\-d\*(C'\fR option (debug), some bbolt internals are being
displayed as well.
.SS "man"
.IX Subsection "man"
The \fBman\fR subcommand shows an unformatted text variant of the manual
page (which are currently reading).
.PP
Usage:
.PP
.Vb 2
\& Usage:
\& anydb man [flags]
\&
\& Flags:
\& \-h, \-\-help help for man
.Ve
.PP
The manual is being piped into the \f(CW\*(C`more\*(C'\fR command, which is being
expected to exist according to the \s-1POSIX\s0 standard on all supported
unix platforms. It might not work on Windows.
.SH "TEMPLATES"
.IX Header "TEMPLATES"
The \fBget\fR and \fBlist\fR commands support a template feature, which is
very handy to create you own kind of formatting. The template syntax
being used is the \s-1GO\s0 template language, refer to
<https://pkg.go.dev/text/template> for details.
.PP
Each template operates on one or more entries, no loop construct is
required, the template provided applies to every matching entry
separatley.
.PP
The following template variables can be used:
.IP "\fBKey\fR \- string" 4
.IX Item "Key - string"
.PD 0
.IP "\fBValue\fR \- string" 4
.IX Item "Value - string"
.IP "\fBBin\fR \- []byte" 4
.IX Item "Bin - []byte"
.IP "\fBCreated\fR \- time.Time" 4
.IX Item "Created - time.Time"
.IP "\fBTags\fR \- []string" 4
.IX Item "Tags - []string"
.IP "\fBEncrypted\fR bool" 4
.IX Item "Encrypted bool"
.PD
.PP
Prepend a single dot (\*(L".\*(R") before each variable name.
.PP
Here are some examples how to use the feature:
.PP
Only show the keys of all entries:
.PP
.Vb 1
\& anydb list \-m template \-T "{{ .Key }}"
.Ve
.PP
Format the list in a way so that is possible to evaluate it in a
shell:
.PP
.Vb 2
\& eval $(anydb get foo \-m template \-T "key=\*(Aq{{ .Key }}\*(Aq value=\*(Aq{{ .Value }}\*(Aq ts=\*(Aq{{ .Created}}\*(Aq")
\& echo "Key: $key, Value: $value"
.Ve
.PP
Print the values in \s-1CSV\s0 format \s-1ONLY\s0 if they have some tag:
.PP
.Vb 1
\& anydb list \-m template \-T "{{ if .Tags }}{{ .Key }},{{ .Value }},{{ .Created}}{{ end }}"
.Ve
.SH "CONFIGURATION"
.IX Header "CONFIGURATION"
Anydb looks at the following locations for a configuration file, in
that order:
.ie n .IP """$HOME/.config/anydb/anydb.toml""" 4
.el .IP "\f(CW$HOME/.config/anydb/anydb.toml\fR" 4
.IX Item "$HOME/.config/anydb/anydb.toml"
.PD 0
.ie n .IP """$HOME/.anydb.toml""" 4
.el .IP "\f(CW$HOME/.anydb.toml\fR" 4
.IX Item "$HOME/.anydb.toml"
.ie n .IP """anydb.toml"" in the current directory" 4
.el .IP "\f(CWanydb.toml\fR in the current directory" 4
.IX Item "anydb.toml in the current directory"
.ie n .IP "or specify one using ""\-c""" 4
.el .IP "or specify one using \f(CW\-c\fR" 4
.IX Item "or specify one using -c"
.PD
.PP
The configuration format uses the \s-1TOML\s0 language, refer to
<https://toml.io/en/> for more details. The key names correspond to
the commandline options in most cases.
.PP
Configuration follows a certain precedence: the files are tried to be
read in the given order, followed by commandline options. That is, the
last configuration file wins, unless the user provides a commandline
option, then this setting will be taken.
.PP
A complete configuration file might look like this:
.PP
.Vb 7
\& # defaults
\& dbfile = "~/.config/anydb/default.db"
\& dbbucket = "data"
\& noheaders = false
\& nohumanize = false
\& encrypt = false
\& listen = "localhost:8787"
\&
\& # different setups for different buckets
\& [buckets.data]
\& encrypt = true
\&
\& [buckets.test]
\& encrypt = false
.Ve
.PP
Under normal circumstances you don't need a configuration
file. However, if you want to use different buckets, then this might
be a handy option. Buckets are being configured in ini-style with the
term \*(L"bucket.\*(R" followed by the bucket name. In the example above we
enable encryption for the default bucket \*(L"data\*(R" and disable it for a
bucket \*(L"test\*(R". To use different buckets, use the \f(CW\*(C`\-b\*(C'\fR option.
.SH "REST API"
.IX Header "REST API"
The subcommand \fBserve\fR starts a simple \s-1HTTP\s0 service, which responds
to RESTful \s-1HTTP\s0 requests. The listener responds to all requests with a
\&\s-1JSON\s0 encoded response. The response contains the status and the
content \- if any \- of the requested resource.
.PP
The following requests are supported:
.IP "\fB\s-1GET\s0 /anydb/v1/\fR" 4
.IX Item "GET /anydb/v1/"
Returns a \s-1JSON\s0 encoded list of all entries.
.IP "\fB\s-1GET\s0 /anydb/v1/key\fR" 4
.IX Item "GET /anydb/v1/key"
Returns the \s-1JSON\s0 encoded entry, if found.
.IP "\fB\s-1PUT\s0 /anydb/v1/\fR" 4
.IX Item "PUT /anydb/v1/"
Create an entry. Expects a \s-1JSON\s0 encoded request object in \s-1POST\s0 data.
.IP "\fB\s-1DELETE\s0 /anydb/v1/key\fR" 4
.IX Item "DELETE /anydb/v1/key"
Delete an entry.
.PP
Some curl example calls to the \s-1API:\s0
.PP
Post a new key:
curl \-X \s-1PUT\s0 localhost:8787/anydb/v1/ \e
\-H 'Content\-Type: application/json' \e
\-d '{\*(L"key\*(R":\*(L"foo\*(R",\*(L"val\*(R":\*(L"bar\*(R"}'
.PP
Retrieve the value:
.PP
.Vb 1
\& curl localhost:8787/anydb/v1/foo
.Ve
.PP
List all keys:
.PP
.Vb 1
\& curl localhost:8787/anydb/v1/
.Ve
.SH "BUGS"
.IX Header "BUGS"
In order to report a bug, unexpected behavior, feature requests
or to submit a patch, please open an issue on github:
<https://github.com/TLINDEN/anydb/issues>.
.PP
Please repeat the failing command with debugging enabled \f(CW\*(C`\-d\*(C'\fR and
include the output in the issue.
.SH "LIMITATIONS"
.IX Header "LIMITATIONS"
The \s-1REST API\s0 list request doesn't provide any filtering capabilities yet.
.SH "LICENSE"
.IX Header "LICENSE"
This software is licensed under the \s-1GNU GENERAL PUBLIC LICENSE\s0 version 3.

563
anydb.pod
View File

@@ -11,14 +11,20 @@ anydb - a personal key value store
Available Commands:
completion Generate the autocompletion script for the specified shell
del Delete key
edit Edit a key
export Export database to json
get Retrieve value for a key
help Help about any command
import Import database dump
info info
list List database contents
man show manual page
serve run REST API listener
set Insert key/value pair
Flags:
-b, --bucket string use other bucket (default: data) (default "data")
-c, --config string toml config file
-f, --dbfile string DB file to use (default "/home/scip/.config/anydb/default.db")
-d, --debug Enable debugging
-h, --help help for anydb
@@ -26,11 +32,562 @@ anydb - a personal key value store
Use "anydb [command] --help" for more information about a command.
=head1 DESCRIPTION
Anydb is a simple to use commandline tool to store anything you'd
like, even binary files etc. It uses a key/value store (bbolt) in your
home directory.
Anydb is a commandline personal key value store, it is simple to use
and can be used to store anything you'd like, even binary files
etc. It uses a key/value store (bbolt) in your home directory.
The tool provides a number of subcommands to use it, there are global
options and each subcommand has its own set of options.
=head1 GLOBAL OPTIONS
=over
=item C<-f, --dbfile filename>
The default location of your databas is
C<$HOME/.config/anydb/default.db>. You can change this with the C<-f>
option.
=item C<-b, --bucket name>
Data in a bbolt key-value-store are managed in so called
buckets. These are kind of namespaces, where each key must be
unique. However, a database may contain more than one bucket.
By default anydb uses a bucket named "data", but you can change this
using the option C<-b>.
Buckets can be configured to always encrypt values, see L<ENCRYTPTION>.
=item C<-c, --config filename>
Under normal circumstances you don't need a configuration file. But if
you want, you can provide one using the option C<-c>.
Anydb looks for a couple of default locations for a config file. You
only need this option if you want to supply a configuration on a
non-standard location. See L<CONFIGURATION> for more details.
=item C<-d, --debug>
Enable debug output.
=item C<-h, --help>
Show the usage of anydb.
=item C<-v, --version>
Show the program version.
=back
All of these options can be used with subcommands as well.
=head1 SUBCOMMANDS
=head2 completion
The B<completion> command can be used to setup completion for
anydb. Just put something like this into your shell's configuration
file:
source <(anydb completion bash)
If you use another shell, specify it instead of bash, of course.
=head2 set
The B<set> command is being used to insert or update a key-value pair.
Usage:
Usage:
anydb set <key> [<value> | -r <file>] [-t <tag>] [flags]
Aliases:
set, add, s, +
Flags:
-e, --encrypt encrypt value
-r, --file string Filename or - for STDIN
-h, --help help for set
-t, --tags tag,tag,... tags, multiple allowed
The standard way to insert a new entry is really simple:
anydb set key value
If you don't specify a value, anydb expects you to feed it some data
via STDIN. For example:
anydb set key < file
You might as well specify a file directly using the C<-f> option:
anydb set key -f file
Values can be encrypted using B<ChaCha20Poly1305> when you specify the
C<-e> option. Anydb will ask you interactively for a passphrase. You
can also provide the passphrase using the environment variable
C<ANYDB_PASSWORD>. To encrypt the value, a cryptographically secure
key will be derived from the passphrase using the ArgonID2
algorithm. Each value can be encrypted with another passphrase. So,
the database itself is not encrypted, just the values.
You can supply tags by using the option C<-t>. Multiple tags can be
provided either by separating them with a comma or by using multiple
C<-t> parameters:
anydb set key value -t tag1,tag2
anydb set key value -t tag1 -t tag2
You can later filter entries by tag or by a combination of tags.
To edit or modify an entry, just use the B<set> command with the same
key, the value in the database will be overwritten with the new
value. An alternative option is the B<edit> command, see below.
=head2 get
To retrieve the value of a key, use the B<get> subcommand.
Usage:
Usage:
anydb get <key> [-o <file>] [-m <mode>] [-n -N] [-T <tpl>] [flags]
Aliases:
get, show, g, .
Flags:
-h, --help help for get
-m, --mode string output format (simple|wide|json|template) (default 'simple')
-n, --no-headers omit headers in tables
-N, --no-human do not translate to human readable values
-o, --output string output value to file (ignores -m)
-T, --template string go template for '-m template'
In its simplest form you just call the B<get> subcommand with the key
you want to have the value for. The value is being printed to STDOUT
by default:
anydb get key
If the value is binary content, it will not just being printed. In
those cases you need to either redirect output into a file or use the
option C<-o> to write to a file:
anydb get key > file
anydb get key -o file
If the value is encrypted, you will be asked for the passphrase to
decrypt it. If the environment variable C<ANYDB_PASSWORD> is set, its
value will be used instead.
There are different output modes you can choose from: simple, wide and
json. The "simple" mode is the default one, it just prints the value
as is. The "wide" mode prints a tabular output similar to the B<list>
subcommand, see there for more details. The options C<-n> and C<-N>
have the same meaning as in the list command. The "json" mode prints
the raw JSON representation of the whole database entry. Decryption
will only take place in "simple" and "json" mode. The "template" mode
provides the most flexibily, it is detailed in the section
L<TEMPLATES>.
=head2 list
The B<list> subcommand displays a list of all database entries.
Usage:
Usage:
anydb list [<filter-regex> | -t <tag> ] [-m <mode>] [-nNif] [-T <tpl>] [flags]
Aliases:
list, ls, /, find, search
Flags:
-i, --case-insensitive filter case insensitive
-h, --help help for list
-m, --mode string output format (table|wide|json|template), wide is a verbose table. (default 'table')
-n, --no-headers omit headers in tables
-N, --no-human do not translate to human readable values
-s, --search-fulltext perform a full text search
-t, --tags stringArray tags, multiple allowed
-T, --template string go template for '-m template'
-l, --wide-output output mode: wide
In its simplest form - without any options - , the B<list> command
just prints all keys with their values to STDOUT. Values are being
truncated to maximum of 60 characters, that is, multiline values are
not completely shown in order to keep the tabular view readable.
To get more informations about each entry, use the C<-o wide> or C<-l>
option. In addition to the key and value also the size, update
timestamp and tags will be printed. Time and size values are converted
into a human readable form, you can suppress this behavior with the
C<-N> option. You may omit the headers using the option C<-n>
Sometimes you might want to filter the list of entries. Either because
your database grew too large or because you're searching for
something. In that case you have two options: You may supply one or
more tags or provide a filter regexp. To filter by tag, do:
anydb list -t tag1
anydb list -t tag1,tag2
anydb list -t tag1 -t tag2
To filter using a regular expression, do:
anydb list "foo.*bar"
Regular expressions follow the golang B<re2> syntax. For more details
about the syntax, refer to
L<https://github.com/google/re2/wiki/Syntax>. Please note, that this
regexp dialect is not PCRE compatible, but supports most of its
features.
If you want to search case insensitive, add the option C<-i>.
By default anydb only searches through the keys. If you want to search
through the values as well, then use the C<-s> option, which enables
full-text search.
You can - as with the B<get> command - use other output modes. The
default mode is "table". The "wide" mode is, as already mentioned, a
more detailed table. Also supported is "json" mode and "template"
mode. For details about using templates see L<TEMPLATES>.
=head2 del
Use the B<del> command to delete database entries.
Usage:
Usage:
anydb del <key> [flags]
Aliases:
del, d, rm
Flags:
-h, --help help for del
The subcommand B<del> does not provide any further options, it just
deletes the entry referred to by the given key. No questions are being
asked.
=head2 edit
The B<edit> command makes it easier to modify larger entries.
Usage:
Usage:
anydb edit <key> [flags]
Aliases:
edit, modify, mod, ed, vi
Flags:
-h, --help help for edit
The subcommand B<edit> does not provide any further options. It
works like this:
=over
=item 1. Write the value info a temporary file.
=item 2. Execute the editor (which one, see below!) with that file.
=item 3. Now you can edit the file and save+close it when done.
=item 4. Anydb picks up the file and if the content has changed, puts its value into the DB.
=back
By default anydb executes the C<vi> command. You can modify this
behavior by setting the environment variable C<EDITOR> appropriately.
Please note, that this does not work with binary content!
=head2 export
Since the bbolt database file is not portable across platforms (it is
bound to the endianess of the CPU it was being created on), you might
want to create a backup file of your database. You can do this with
the B<export> subcommand.
Usage:
Usage:
anydb export -o <json filename> [flags]
Aliases:
export, dump, backup
Flags:
-h, --help help for export
-o, --output string output to file
The database dump is a JSON representation of the whole database and
will be printed to the file specified with the C<-o> option. If you
specify "-" as the filename, it will be written to STDIN.
anydb export -o dump.json
anydb export -o - > dump.json
Please note, that encrypted values will not be decrypted. This might
change in a future version of anydb.
=head2 import
The B<import> subcommand can be used to restore a database from a JSON
dump.
Usage:
Usage:
anydb import -i <json file> [flags]
Aliases:
import, restore
Flags:
-r, --file string Filename or - for STDIN
-h, --help help for import
-t, --tags stringArray tags, multiple allowed
The C<import> subcommand reads the JSON contents from
the file specified with the C<-i> option. If you specify "-" as the
filename, it will be read from STDIN.
anydb import -i - < dump.json
anydb import -i dump.json
cat dump.json | anydb import -i -
If there is already a database, it will be saved by appending a
timestamp and a new database with the contents of the dump will be
created.
=head2 serve
Anydb provides a RESTful API, which you can use to manage the database
from somewhere else. The API does not provide any authentication or
any other security measures, so better only use it on localhost.
Usage:
Usage:
anydb serve [-l host:port] [flags]
Flags:
-h, --help help for serve
-l, --listen string host:port (default "localhost:8787")
To start the listener, just execute the B<serve> subcommand. You can
tweak the ip address and tcp port using the C<-l> option. The listener
will not fork and run in the foreground. Logs are being printed to
STDOUT as long as the listener runs.
For more details about the API, please see the L<REST API> section.
=head2 info
The B<info> subcommand shows you some information about your current
database.
Usage:
Usage:
anydb info [flags]
Flags:
-h, --help help for info
-N, --no-human do not translate to human readable values
Data being shown are: filename and size, number of keys per bucket. If
you supply the C<-d> option (debug), some bbolt internals are being
displayed as well.
=head2 man
The B<man> subcommand shows an unformatted text variant of the manual
page (which are currently reading).
Usage:
Usage:
anydb man [flags]
Flags:
-h, --help help for man
The manual is being piped into the C<more> command, which is being
expected to exist according to the POSIX standard on all supported
unix platforms. It might not work on Windows.
=head1 TEMPLATES
The B<get> and B<list> commands support a template feature, which is
very handy to create you own kind of formatting. The template syntax
being used is the GO template language, refer to
L<https://pkg.go.dev/text/template> for details.
Each template operates on one or more entries, no loop construct is
required, the template provided applies to every matching entry
separatley.
The following template variables can be used:
=over
=item B<Key> - string
=item B<Value> - string
=item B<Bin> - []byte
=item B<Created> - time.Time
=item B<Tags> - []string
=item B<Encrypted> bool
=back
Prepend a single dot (".") before each variable name.
Here are some examples how to use the feature:
Only show the keys of all entries:
anydb list -m template -T "{{ .Key }}"
Format the list in a way so that is possible to evaluate it in a
shell:
eval $(anydb get foo -m template -T "key='{{ .Key }}' value='{{ .Value }}' ts='{{ .Created}}'")
echo "Key: $key, Value: $value"
Print the values in CSV format ONLY if they have some tag:
anydb list -m template -T "{{ if .Tags }}{{ .Key }},{{ .Value }},{{ .Created}}{{ end }}"
=head1 CONFIGURATION
Anydb looks at the following locations for a configuration file, in
that order:
=over
=item C<$HOME/.config/anydb/anydb.toml>
=item C<$HOME/.anydb.toml>
=item C<anydb.toml> in the current directory
=item or specify one using C<-c>
=back
The configuration format uses the TOML language, refer to
L<https://toml.io/en/> for more details. The key names correspond to
the commandline options in most cases.
Configuration follows a certain precedence: the files are tried to be
read in the given order, followed by commandline options. That is, the
last configuration file wins, unless the user provides a commandline
option, then this setting will be taken.
A complete configuration file might look like this:
# defaults
dbfile = "~/.config/anydb/default.db"
dbbucket = "data"
noheaders = false
nohumanize = false
encrypt = false
listen = "localhost:8787"
# different setups for different buckets
[buckets.data]
encrypt = true
[buckets.test]
encrypt = false
Under normal circumstances you don't need a configuration
file. However, if you want to use different buckets, then this might
be a handy option. Buckets are being configured in ini-style with the
term "bucket." followed by the bucket name. In the example above we
enable encryption for the default bucket "data" and disable it for a
bucket "test". To use different buckets, use the C<-b> option.
=head1 REST API
The subcommand B<serve> starts a simple HTTP service, which responds
to RESTful HTTP requests. The listener responds to all requests with a
JSON encoded response. The response contains the status and the
content - if any - of the requested resource.
The following requests are supported:
=over
=item B<GET /anydb/v1/>
Returns a JSON encoded list of all entries.
=item B<GET /anydb/v1/key>
Returns the JSON encoded entry, if found.
=item B<PUT /anydb/v1/>
Create an entry. Expects a JSON encoded request object in POST data.
=item B<DELETE /anydb/v1/key>
Delete an entry.
=back
Some curl example calls to the API:
Post a new key:
curl -X PUT localhost:8787/anydb/v1/ \
-H 'Content-Type: application/json' \
-d '{"key":"foo","val":"bar"}'
Retrieve the value:
curl localhost:8787/anydb/v1/foo
List all keys:
curl localhost:8787/anydb/v1/
=head1 BUGS
In order to report a bug, unexpected behavior, feature requests
or to submit a patch, please open an issue on github:
L<https://github.com/TLINDEN/anydb/issues>.
Please repeat the failing command with debugging enabled C<-d> and
include the output in the issue.
=head1 LIMITATIONS
The REST API list request doesn't provide any filtering capabilities yet.
=head1 LICENSE

View File

@@ -1,33 +1,56 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package app
import (
"fmt"
"io"
"os"
"strings"
"unicode/utf8"
)
type DbAttr struct {
Key string
Val string
Bin []byte
Preview string
Val []byte
Args []string
Tags []string
File string
Encrypted bool
Binary bool
// conf flags, needed for incoming rest requests
Fulltext bool
}
// check if value is to be read from a file or stdin, setup preview
// text according to flags, lowercase key
func (attr *DbAttr) ParseKV() error {
attr.Key = strings.ToLower(attr.Args[0])
switch len(attr.Args) {
case 1:
// 1 arg = key + read from file or stdin
attr.Key = attr.Args[0]
if attr.File == "" {
attr.File = "-"
}
case 2:
attr.Key = attr.Args[0]
attr.Val = attr.Args[1]
attr.Val = []byte(attr.Args[1])
if attr.Args[1] == "-" {
attr.File = "-"
@@ -35,7 +58,29 @@ func (attr *DbAttr) ParseKV() error {
}
if attr.File != "" {
return attr.GetFileValue()
if err := attr.GetFileValue(); err != nil {
return err
}
}
switch {
case attr.Binary:
attr.Preview = "<binary-content>"
case attr.Encrypted:
attr.Preview = "<encrypted-content>"
default:
if len(attr.Val) > MaxValueWidth {
attr.Preview = string(attr.Val)[0:MaxValueWidth] + "..."
if strings.Contains(attr.Preview, "\n") {
parts := strings.Split(attr.Preview, "\n")
if len(parts) > 0 {
attr.Preview = parts[0]
}
}
} else {
attr.Preview = string(attr.Val)
}
}
return nil
@@ -66,11 +111,12 @@ func (attr *DbAttr) GetFileValue() error {
}
// poor man's text file test
sdata := string(data)
if utf8.ValidString(sdata) {
attr.Val = sdata
attr.Val = data
if utf8.ValidString(string(data)) {
attr.Binary = false
} else {
attr.Bin = data
attr.Binary = true
}
} else {
// read from console stdin
@@ -85,7 +131,7 @@ func (attr *DbAttr) GetFileValue() error {
data += input + "\n"
}
attr.Val = data
attr.Val = []byte(data)
}
return nil

View File

@@ -1,8 +1,23 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package app
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"os"
@@ -88,7 +103,7 @@ func GetRandom(size int, capacity int) ([]byte, error) {
// modifying it.
//
// The cipher text consists of:
// base64(password-salt) + base64(12 byte nonce + ciphertext + 16 byte mac)
// password-salt) + (12 byte nonce + ciphertext + 16 byte mac)
func Encrypt(pass []byte, attr *DbAttr) error {
key, err := DeriveKey(pass, nil)
if err != nil {
@@ -100,25 +115,17 @@ func Encrypt(pass []byte, attr *DbAttr) error {
return fmt.Errorf("failed to create AEAD cipher: %w", err)
}
var plain []byte
if attr.Val != "" {
plain = []byte(attr.Val)
} else {
plain = attr.Bin
}
total := aead.NonceSize() + len(plain) + aead.Overhead()
total := aead.NonceSize() + len(attr.Val) + aead.Overhead()
nonce, err := GetRandom(aead.NonceSize(), total)
if err != nil {
return err
}
cipher := aead.Seal(nonce, nonce, plain, nil)
cipher := aead.Seal(nonce, nonce, attr.Val, nil)
attr.Bin = nil
attr.Val = base64.RawStdEncoding.EncodeToString(key.Salt) +
base64.RawStdEncoding.EncodeToString(cipher)
attr.Val = append(attr.Val, key.Salt...)
attr.Val = append(attr.Val, cipher...)
attr.Encrypted = true
@@ -126,21 +133,17 @@ func Encrypt(pass []byte, attr *DbAttr) error {
}
// Do the reverse
func Decrypt(pass []byte, cipherb64 string) ([]byte, error) {
salt, err := base64.RawStdEncoding.Strict().DecodeString(cipherb64[0:B64SaltLen])
if err != nil {
return nil, fmt.Errorf("failed to encode to base64: %w", err)
func Decrypt(pass []byte, cipherb []byte) ([]byte, error) {
if len(cipherb) < B64SaltLen {
return nil, fmt.Errorf("encrypted cipher block too small")
}
key, err := DeriveKey(pass, salt)
key, err := DeriveKey(pass, cipherb[0:B64SaltLen])
if err != nil {
return nil, err
}
cipher, err := base64.RawStdEncoding.Strict().DecodeString(cipherb64[B64SaltLen:])
if err != nil {
return nil, fmt.Errorf("failed to encode to base64: %w", err)
}
cipher := cipherb[B64SaltLen:]
aead, err := chacha20poly1305.New(key.Key)
if err != nil {

346
app/db.go
View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package app
import (
@@ -11,25 +27,33 @@ import (
"time"
bolt "go.etcd.io/bbolt"
"google.golang.org/protobuf/proto"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
const MaxValueWidth int = 60
type DB struct {
Debug bool
Dbfile string
Bucket string
DB *bolt.DB
}
type DbEntry struct {
Id string `json:"id"`
Key string `json:"key"`
Value string `json:"value"`
Encrypted bool `json:"encrypted"`
Bin []byte `json:"bin"`
Tags []string `json:"tags"`
Created time.Time `json:"created"`
type BucketInfo struct {
Name string
Keys int
Size int
Sequence uint64
Stats bolt.BucketStats
}
type DbEntries []DbEntry
type DbInfo struct {
Buckets []BucketInfo
Path string
}
type DbEntries []*DbEntry
type DbTag struct {
Keys []string `json:"key"`
@@ -37,8 +61,8 @@ type DbTag struct {
const BucketData string = "data"
func New(file string, debug bool) (*DB, error) {
return &DB{Debug: debug, Dbfile: file}, nil
func New(file string, bucket string, debug bool) (*DB, error) {
return &DB{Debug: debug, Dbfile: file, Bucket: bucket}, nil
}
func (db *DB) Open() error {
@@ -61,7 +85,7 @@ func (db *DB) Close() error {
return db.DB.Close()
}
func (db *DB) List(attr *DbAttr) (DbEntries, error) {
func (db *DB) List(attr *DbAttr, fulltext bool) (DbEntries, error) {
if err := db.Open(); err != nil {
return nil, err
}
@@ -71,31 +95,59 @@ func (db *DB) List(attr *DbAttr) (DbEntries, error) {
var filter *regexp.Regexp
if len(attr.Args) > 0 {
// via cli
filter = regexp.MustCompile(attr.Args[0])
}
err := db.DB.View(func(tx *bolt.Tx) error {
if len(attr.Key) > 0 {
// via api
filter = regexp.MustCompile(attr.Key)
}
bucket := tx.Bucket([]byte(BucketData))
err := db.DB.View(func(tx *bolt.Tx) error {
root := tx.Bucket([]byte(db.Bucket))
if root == nil {
return nil
}
bucket := root.Bucket([]byte("meta"))
if bucket == nil {
return nil
}
err := bucket.ForEach(func(key, jsonentry []byte) error {
databucket := root.Bucket([]byte("data"))
if databucket == nil {
return fmt.Errorf("failed to retrieve data sub bucket")
}
err := bucket.ForEach(func(key, pbentry []byte) error {
var entry DbEntry
if err := json.Unmarshal(jsonentry, &entry); err != nil {
return fmt.Errorf("failed to unmarshal from json: %w", err)
if err := proto.Unmarshal(pbentry, &entry); err != nil {
return fmt.Errorf("failed to unmarshal from protobuf: %w", err)
}
if fulltext {
// avoid crash due to access fault
value := databucket.Get([]byte(entry.Key)) // empty is ok
vc := make([]byte, len(value))
copy(vc, value)
entry.Value = vc
}
var include bool
switch {
case filter != nil:
if filter.MatchString(entry.Value) ||
filter.MatchString(entry.Key) ||
if filter.MatchString(entry.Key) ||
filter.MatchString(strings.Join(entry.Tags, " ")) {
include = true
}
if !entry.Binary && !include && fulltext {
if filter.MatchString(string(entry.Value)) {
include = true
}
}
case len(attr.Tags) > 0:
for _, search := range attr.Tags {
for _, tag := range entry.Tags {
@@ -114,7 +166,7 @@ func (db *DB) List(attr *DbAttr) (DbEntries, error) {
}
if include {
entries = append(entries, entry)
entries = append(entries, &entry)
}
return nil
@@ -122,6 +174,7 @@ func (db *DB) List(attr *DbAttr) (DbEntries, error) {
return err
})
return entries, err
}
@@ -133,30 +186,36 @@ func (db *DB) Set(attr *DbAttr) error {
entry := DbEntry{
Key: attr.Key,
Value: attr.Val,
Bin: attr.Bin,
Binary: attr.Binary,
Tags: attr.Tags,
Encrypted: attr.Encrypted,
Created: time.Now(),
Created: timestamppb.Now(),
Size: uint64(len(attr.Val)),
Preview: attr.Preview,
}
// check if the entry already exists and if yes, check if it has
// any tags. if so, we initialize our update struct with these
// tags unless it has new tags configured.
err := db.DB.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(BucketData))
root := tx.Bucket([]byte(db.Bucket))
if root == nil {
return nil
}
bucket := root.Bucket([]byte("meta"))
if bucket == nil {
return nil
}
jsonentry := bucket.Get([]byte(entry.Key))
if jsonentry == nil {
pbentry := bucket.Get([]byte(entry.Key))
if pbentry == nil {
return nil
}
var oldentry DbEntry
if err := json.Unmarshal(jsonentry, &oldentry); err != nil {
return fmt.Errorf("failed to unmarshal from json: %w", err)
if err := proto.Unmarshal(pbentry, &oldentry); err != nil {
return fmt.Errorf("failed to unmarshal from protobuf: %w", err)
}
if len(oldentry.Tags) > 0 && len(entry.Tags) == 0 {
@@ -171,19 +230,39 @@ func (db *DB) Set(attr *DbAttr) error {
return err
}
// marshall our data
pbentry, err := proto.Marshal(&entry)
if err != nil {
return fmt.Errorf("failed to marshall protobuf: %w", err)
}
err = db.DB.Update(func(tx *bolt.Tx) error {
// insert data
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketData))
// create root bucket
root, err := tx.CreateBucketIfNotExists([]byte(db.Bucket))
if err != nil {
return fmt.Errorf("failed to create DB bucket: %w", err)
}
jsonentry, err := json.Marshal(entry)
// create meta bucket
bucket, err := root.CreateBucketIfNotExists([]byte("meta"))
if err != nil {
return fmt.Errorf("failed to marshall json: %w", err)
return fmt.Errorf("failed to create DB meta sub bucket: %w", err)
}
err = bucket.Put([]byte(entry.Key), []byte(jsonentry))
// write meta data
err = bucket.Put([]byte(entry.Key), []byte(pbentry))
if err != nil {
return fmt.Errorf("failed to insert data: %w", err)
}
// create data bucket
databucket, err := root.CreateBucketIfNotExists([]byte("data"))
if err != nil {
return fmt.Errorf("failed to create DB data sub bucket: %w", err)
}
// write value
err = databucket.Put([]byte(entry.Key), attr.Val)
if err != nil {
return fmt.Errorf("failed to insert data: %w", err)
}
@@ -207,20 +286,48 @@ func (db *DB) Get(attr *DbAttr) (*DbEntry, error) {
entry := DbEntry{}
err := db.DB.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(BucketData))
// root bucket
root := tx.Bucket([]byte(db.Bucket))
if root == nil {
return nil
}
// get meta sub bucket
bucket := root.Bucket([]byte("meta"))
if bucket == nil {
return nil
}
jsonentry := bucket.Get([]byte(attr.Key))
if jsonentry == nil {
return nil
// retrieve meta data
pbentry := bucket.Get([]byte(attr.Key))
if pbentry == nil {
return fmt.Errorf("no such key: %s", attr.Key)
}
if err := json.Unmarshal(jsonentry, &entry); err != nil {
return fmt.Errorf("failed to unmarshal from json: %w", err)
// put into struct
if err := proto.Unmarshal(pbentry, &entry); err != nil {
return fmt.Errorf("failed to unmarshal from protobuf: %w", err)
}
// get data sub bucket
databucket := root.Bucket([]byte("data"))
if databucket == nil {
return fmt.Errorf("failed to retrieve data sub bucket")
}
// retrieve actual data value
value := databucket.Get([]byte(attr.Key))
if len(value) == 0 {
return fmt.Errorf("no such key: %s", attr.Key)
}
// we need to make a copy of it, otherwise we'll get an
// "unexpected fault address" error
vc := make([]byte, len(value))
copy(vc, value)
entry.Value = vc
return nil
})
@@ -238,7 +345,7 @@ func (db *DB) Del(attr *DbAttr) error {
defer db.Close()
err := db.DB.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket([]byte(BucketData))
bucket := tx.Bucket([]byte(db.Bucket))
if bucket == nil {
return nil
@@ -250,14 +357,14 @@ func (db *DB) Del(attr *DbAttr) error {
return err
}
func (db *DB) Import(attr *DbAttr) error {
func (db *DB) Import(attr *DbAttr) (string, error) {
// open json file into attr.Val
if err := attr.GetFileValue(); err != nil {
return err
return "", err
}
if attr.Val == "" {
return errors.New("empty json file")
if len(attr.Val) == 0 {
return "", errors.New("empty json file")
}
var entries DbEntries
@@ -265,69 +372,166 @@ func (db *DB) Import(attr *DbAttr) error {
newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05")
if err := json.Unmarshal([]byte(attr.Val), &entries); err != nil {
return cleanError(newfile, fmt.Errorf("failed to unmarshal json: %w", err))
return "", cleanError(newfile, fmt.Errorf("failed to unmarshal json: %w", err))
}
if fileExists(db.Dbfile) {
// backup the old file
err := os.Rename(db.Dbfile, newfile)
if err != nil {
return fmt.Errorf("failed to rename file %s to %s: %w", db.Dbfile, newfile, err)
return "", fmt.Errorf("failed to rename file %s to %s: %w", db.Dbfile, newfile, err)
}
}
// should now be a new db file
if err := db.Open(); err != nil {
return cleanError(newfile, err)
return "", cleanError(newfile, err)
}
defer db.Close()
err := db.DB.Update(func(tx *bolt.Tx) error {
// insert data
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketData))
// create root bucket
root, err := tx.CreateBucketIfNotExists([]byte(db.Bucket))
if err != nil {
return fmt.Errorf("failed to create bucket: %w", err)
return fmt.Errorf("failed to create DB bucket: %w", err)
}
// create meta bucket
bucket, err := root.CreateBucketIfNotExists([]byte("meta"))
if err != nil {
return fmt.Errorf("failed to create DB meta sub bucket: %w", err)
}
for _, entry := range entries {
jsonentry, err := json.Marshal(entry)
pbentry, err := proto.Marshal(entry)
if err != nil {
return fmt.Errorf("failed to marshall json: %w", err)
return fmt.Errorf("failed to marshall protobuf: %w", err)
}
err = bucket.Put([]byte(entry.Key), []byte(jsonentry))
// write meta data
err = bucket.Put([]byte(entry.Key), []byte(pbentry))
if err != nil {
return fmt.Errorf("failed to insert data into DB: %w", err)
}
// create data bucket
databucket, err := root.CreateBucketIfNotExists([]byte("data"))
if err != nil {
return fmt.Errorf("failed to create DB data sub bucket: %w", err)
}
// write value
err = databucket.Put([]byte(entry.Key), entry.Value)
if err != nil {
return fmt.Errorf("failed to insert data: %w", err)
}
}
return nil
})
if err != nil {
return cleanError(newfile, err)
return "", cleanError(newfile, err)
}
fmt.Printf("backed up database file to %s\n", newfile)
fmt.Printf("imported %d database entries\n", len(entries))
return nil
return fmt.Sprintf("backed up database file to %s\nimported %d database entries\n",
newfile, len(entries)), nil
}
func cleanError(file string, err error) error {
// remove given [backup] file and forward the given error
os.Remove(file)
return err
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
// return false on any error
return false
func (db *DB) Info() (*DbInfo, error) {
if err := db.Open(); err != nil {
return nil, err
}
defer db.Close()
return !info.IsDir()
info := &DbInfo{Path: db.Dbfile}
err := db.DB.View(func(tx *bolt.Tx) error {
err := tx.ForEach(func(name []byte, bucket *bolt.Bucket) error {
stats := bucket.Stats()
binfo := BucketInfo{
Name: string(name),
Sequence: bucket.Sequence(),
Keys: stats.KeyN,
Stats: bucket.Stats(),
}
err := bucket.ForEach(func(key, entry []byte) error {
binfo.Size += len(entry) + len(key)
return nil
})
if err != nil {
return fmt.Errorf("failed to read keys: %w", err)
}
info.Buckets = append(info.Buckets, binfo)
return nil
})
if err != nil {
return fmt.Errorf("failed to read from DB: %w", err)
}
return nil
})
return info, err
}
func (db *DB) Getall(attr *DbAttr) (DbEntries, error) {
if err := db.Open(); err != nil {
return nil, err
}
defer db.Close()
var entries DbEntries
err := db.DB.View(func(tx *bolt.Tx) error {
// root bucket
root := tx.Bucket([]byte(db.Bucket))
if root == nil {
return nil
}
// get meta sub bucket
bucket := root.Bucket([]byte("meta"))
if bucket == nil {
return nil
}
// get data sub bucket
databucket := root.Bucket([]byte("data"))
if databucket == nil {
return fmt.Errorf("failed to retrieve data sub bucket")
}
// iterate over all db entries in meta sub bucket
err := bucket.ForEach(func(key, pbentry []byte) error {
var entry DbEntry
if err := proto.Unmarshal(pbentry, &entry); err != nil {
return fmt.Errorf("failed to unmarshal from protobuf: %w", err)
}
// retrieve the value from the data sub bucket
value := databucket.Get([]byte(entry.Key))
// we need to make a copy of it, otherwise we'll get an
// "unexpected fault address" error
vc := make([]byte, len(value))
copy(vc, value)
entry.Value = vc
entries = append(entries, &entry)
return nil
})
return err
})
return entries, err
}

210
app/dbentry.pb.go Normal file
View File

@@ -0,0 +1,210 @@
// -*-c++-*-
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.1
// protoc v3.21.12
// source: app/dbentry.proto
package app
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type DbEntry struct {
state protoimpl.MessageState `protogen:"open.v1"`
Id string `protobuf:"bytes,1,opt,name=Id,proto3" json:"Id,omitempty"`
Key string `protobuf:"bytes,2,opt,name=Key,proto3" json:"Key,omitempty"`
Preview string `protobuf:"bytes,3,opt,name=Preview,proto3" json:"Preview,omitempty"`
Tags []string `protobuf:"bytes,4,rep,name=Tags,proto3" json:"Tags,omitempty"`
Created *timestamppb.Timestamp `protobuf:"bytes,5,opt,name=Created,proto3" json:"Created,omitempty"`
Size uint64 `protobuf:"varint,6,opt,name=Size,proto3" json:"Size,omitempty"`
Encrypted bool `protobuf:"varint,7,opt,name=Encrypted,proto3" json:"Encrypted,omitempty"`
Binary bool `protobuf:"varint,8,opt,name=Binary,proto3" json:"Binary,omitempty"`
Value []byte `protobuf:"bytes,9,opt,name=Value,proto3" json:"Value,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *DbEntry) Reset() {
*x = DbEntry{}
mi := &file_app_dbentry_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *DbEntry) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*DbEntry) ProtoMessage() {}
func (x *DbEntry) ProtoReflect() protoreflect.Message {
mi := &file_app_dbentry_proto_msgTypes[0]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use DbEntry.ProtoReflect.Descriptor instead.
func (*DbEntry) Descriptor() ([]byte, []int) {
return file_app_dbentry_proto_rawDescGZIP(), []int{0}
}
func (x *DbEntry) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *DbEntry) GetKey() string {
if x != nil {
return x.Key
}
return ""
}
func (x *DbEntry) GetPreview() string {
if x != nil {
return x.Preview
}
return ""
}
func (x *DbEntry) GetTags() []string {
if x != nil {
return x.Tags
}
return nil
}
func (x *DbEntry) GetCreated() *timestamppb.Timestamp {
if x != nil {
return x.Created
}
return nil
}
func (x *DbEntry) GetSize() uint64 {
if x != nil {
return x.Size
}
return 0
}
func (x *DbEntry) GetEncrypted() bool {
if x != nil {
return x.Encrypted
}
return false
}
func (x *DbEntry) GetBinary() bool {
if x != nil {
return x.Binary
}
return false
}
func (x *DbEntry) GetValue() []byte {
if x != nil {
return x.Value
}
return nil
}
var File_app_dbentry_proto protoreflect.FileDescriptor
var file_app_dbentry_proto_rawDesc = []byte{
0x0a, 0x11, 0x61, 0x70, 0x70, 0x2f, 0x64, 0x62, 0x65, 0x6e, 0x74, 0x72, 0x79, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x12, 0x03, 0x61, 0x70, 0x70, 0x1a, 0x1f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74,
0x61, 0x6d, 0x70, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xef, 0x01, 0x0a, 0x07, 0x44, 0x62,
0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x0e, 0x0a, 0x02, 0x49, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28,
0x09, 0x52, 0x02, 0x49, 0x64, 0x12, 0x10, 0x0a, 0x03, 0x4b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01,
0x28, 0x09, 0x52, 0x03, 0x4b, 0x65, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x50, 0x72, 0x65, 0x76, 0x69,
0x65, 0x77, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x50, 0x72, 0x65, 0x76, 0x69, 0x65,
0x77, 0x12, 0x12, 0x0a, 0x04, 0x54, 0x61, 0x67, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52,
0x04, 0x54, 0x61, 0x67, 0x73, 0x12, 0x34, 0x0a, 0x07, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64,
0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1a, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61,
0x6d, 0x70, 0x52, 0x07, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x53,
0x69, 0x7a, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x04, 0x52, 0x04, 0x53, 0x69, 0x7a, 0x65, 0x12,
0x1c, 0x0a, 0x09, 0x45, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x18, 0x07, 0x20, 0x01,
0x28, 0x08, 0x52, 0x09, 0x45, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x12, 0x16, 0x0a,
0x06, 0x42, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, 0x42,
0x69, 0x6e, 0x61, 0x72, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x09,
0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x1e, 0x5a, 0x1c, 0x67,
0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x74, 0x6c, 0x69, 0x6e, 0x64, 0x65,
0x6e, 0x2f, 0x61, 0x6e, 0x79, 0x64, 0x62, 0x2f, 0x61, 0x70, 0x70, 0x62, 0x06, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x33,
}
var (
file_app_dbentry_proto_rawDescOnce sync.Once
file_app_dbentry_proto_rawDescData = file_app_dbentry_proto_rawDesc
)
func file_app_dbentry_proto_rawDescGZIP() []byte {
file_app_dbentry_proto_rawDescOnce.Do(func() {
file_app_dbentry_proto_rawDescData = protoimpl.X.CompressGZIP(file_app_dbentry_proto_rawDescData)
})
return file_app_dbentry_proto_rawDescData
}
var file_app_dbentry_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_app_dbentry_proto_goTypes = []any{
(*DbEntry)(nil), // 0: app.DbEntry
(*timestamppb.Timestamp)(nil), // 1: google.protobuf.Timestamp
}
var file_app_dbentry_proto_depIdxs = []int32{
1, // 0: app.DbEntry.Created:type_name -> google.protobuf.Timestamp
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_app_dbentry_proto_init() }
func file_app_dbentry_proto_init() {
if File_app_dbentry_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_app_dbentry_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_app_dbentry_proto_goTypes,
DependencyIndexes: file_app_dbentry_proto_depIdxs,
MessageInfos: file_app_dbentry_proto_msgTypes,
}.Build()
File_app_dbentry_proto = out.File
file_app_dbentry_proto_rawDesc = nil
file_app_dbentry_proto_goTypes = nil
file_app_dbentry_proto_depIdxs = nil
}

20
app/dbentry.proto Normal file
View File

@@ -0,0 +1,20 @@
// -*-c++-*-
syntax = "proto3";
package app;
import "google/protobuf/timestamp.proto";
option go_package = "github.com/tlinden/anydb/app";
message DbEntry {
string Id = 1;
string Key = 2;
string Preview = 3;
repeated string Tags = 4;
google.protobuf.Timestamp Created = 5;
uint64 Size = 6;
bool Encrypted = 7;
bool Binary = 8;
bytes Value = 9;
}

View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package app
// look if a key in a map exists, generic variant

36
app/io.go Normal file
View File

@@ -0,0 +1,36 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package app
import "os"
func cleanError(file string, err error) error {
// remove given [backup] file and forward the given error
os.Remove(file)
return err
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
// return false on any error
return false
}
return !info.IsDir()
}

View File

@@ -1,16 +1,116 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cfg
import "github.com/tlinden/anydb/app"
import (
"fmt"
"io"
"os"
var Version string = "v0.0.2"
"github.com/pelletier/go-toml"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/common"
)
var Version string = "v0.1.1"
type BucketConfig struct {
Encrypt bool
}
type Config struct {
Debug bool
Dbfile string
Mode string // wide, table, yaml, json
NoHeaders bool
Encrypt bool
DB *app.DB
File string
Tags []string
Debug bool
Dbfile string
Dbbucket string
Template string
Mode string // wide, table, yaml, json
NoHeaders bool
NoHumanize bool
Encrypt bool // one entry
CaseInsensitive bool
Fulltext bool
Listen string
Buckets map[string]BucketConfig // config file only
Tags []string // internal
DB *app.DB // internal
File string // internal
}
func (conf *Config) GetConfig(files []string) error {
for _, file := range files {
if err := conf.ParseConfigFile(file); err != nil {
return err
}
}
return nil
}
func (conf *Config) ParseConfigFile(file string) error {
if !common.FileExists(file) {
return nil
}
fd, err := os.OpenFile(file, os.O_RDONLY, 0600)
if err != nil {
return fmt.Errorf("failed to open config file %s: %w", file, err)
}
data, err := io.ReadAll(fd)
if err != nil {
return fmt.Errorf("failed to read from config file: %w", err)
}
add := Config{}
err = toml.Unmarshal(data, &add)
if err != nil {
return fmt.Errorf("failed to unmarshall toml: %w", err)
}
// merge new values into existing config
switch {
case add.Debug != conf.Debug:
conf.Debug = add.Debug
case add.Dbfile != "":
conf.Dbfile = add.Dbfile
case add.Dbbucket != "":
conf.Dbbucket = add.Dbbucket
case add.Template != "":
conf.Template = add.Template
case add.NoHeaders != conf.NoHeaders:
conf.NoHeaders = add.NoHeaders
case add.NoHumanize != conf.NoHumanize:
conf.NoHumanize = add.NoHumanize
case add.Encrypt != conf.Encrypt:
conf.Encrypt = add.Encrypt
case add.Listen != "":
conf.Listen = add.Listen
}
// only supported in config files
conf.Buckets = add.Buckets
// determine bucket encryption mode
for name, bucket := range conf.Buckets {
if name == conf.Dbbucket {
conf.Encrypt = bucket.Encrypt
}
}
return nil
}

View File

@@ -12,14 +12,20 @@ SYNOPSIS
Available Commands:
completion Generate the autocompletion script for the specified shell
del Delete key
edit Edit a key
export Export database to json
get Retrieve value for a key
help Help about any command
import Import database dump
info info
list List database contents
man show manual page
serve run REST API listener
set Insert key/value pair
Flags:
-b, --bucket string use other bucket (default: data) (default "data")
-c, --config string toml config file
-f, --dbfile string DB file to use (default "/home/scip/.config/anydb/default.db")
-d, --debug Enable debugging
-h, --help help for anydb
@@ -28,9 +34,493 @@ SYNOPSIS
Use "anydb [command] --help" for more information about a command.
DESCRIPTION
Anydb is a simple to use commandline tool to store anything you'd like,
even binary files etc. It uses a key/value store (bbolt) in your home
directory.
Anydb is a commandline personal key value store, it is simple to use and
can be used to store anything you'd like, even binary files etc. It uses
a key/value store (bbolt) in your home directory.
The tool provides a number of subcommands to use it, there are global
options and each subcommand has its own set of options.
GLOBAL OPTIONS
"-f, --dbfile filename"
The default location of your databas is
"$HOME/.config/anydb/default.db". You can change this with the "-f"
option.
"-b, --bucket name"
Data in a bbolt key-value-store are managed in so called buckets.
These are kind of namespaces, where each key must be unique.
However, a database may contain more than one bucket.
By default anydb uses a bucket named "data", but you can change this
using the option "-b".
Buckets can be configured to always encrypt values, see ENCRYTPTION.
"-c, --config filename"
Under normal circumstances you don't need a configuration file. But
if you want, you can provide one using the option "-c".
Anydb looks for a couple of default locations for a config file. You
only need this option if you want to supply a configuration on a
non-standard location. See CONFIGURATION for more details.
"-d, --debug"
Enable debug output.
"-h, --help"
Show the usage of anydb.
"-v, --version"
Show the program version.
All of these options can be used with subcommands as well.
SUBCOMMANDS
completion
The completion command can be used to setup completion for anydb. Just
put something like this into your shell's configuration file:
source <(anydb completion bash)
If you use another shell, specify it instead of bash, of course.
set
The set command is being used to insert or update a key-value pair.
Usage:
Usage:
anydb set <key> [<value> | -r <file>] [-t <tag>] [flags]
Aliases:
set, add, s, +
Flags:
-e, --encrypt encrypt value
-r, --file string Filename or - for STDIN
-h, --help help for set
-t, --tags tag,tag,... tags, multiple allowed
The standard way to insert a new entry is really simple:
anydb set key value
If you don't specify a value, anydb expects you to feed it some data via
STDIN. For example:
anydb set key < file
You might as well specify a file directly using the "-f" option:
anydb set key -f file
Values can be encrypted using ChaCha20Poly1305 when you specify the "-e"
option. Anydb will ask you interactively for a passphrase. You can also
provide the passphrase using the environment variable "ANYDB_PASSWORD".
To encrypt the value, a cryptographically secure key will be derived
from the passphrase using the ArgonID2 algorithm. Each value can be
encrypted with another passphrase. So, the database itself is not
encrypted, just the values.
You can supply tags by using the option "-t". Multiple tags can be
provided either by separating them with a comma or by using multiple
"-t" parameters:
anydb set key value -t tag1,tag2
anydb set key value -t tag1 -t tag2
You can later filter entries by tag or by a combination of tags.
To edit or modify an entry, just use the set command with the same key,
the value in the database will be overwritten with the new value. An
alternative option is the edit command, see below.
get
To retrieve the value of a key, use the get subcommand.
Usage:
Usage:
anydb get <key> [-o <file>] [-m <mode>] [-n -N] [-T <tpl>] [flags]
Aliases:
get, show, g, .
Flags:
-h, --help help for get
-m, --mode string output format (simple|wide|json|template) (default 'simple')
-n, --no-headers omit headers in tables
-N, --no-human do not translate to human readable values
-o, --output string output value to file (ignores -m)
-T, --template string go template for '-m template'
In its simplest form you just call the get subcommand with the key you
want to have the value for. The value is being printed to STDOUT by
default:
anydb get key
If the value is binary content, it will not just being printed. In those
cases you need to either redirect output into a file or use the option
"-o" to write to a file:
anydb get key > file
anydb get key -o file
If the value is encrypted, you will be asked for the passphrase to
decrypt it. If the environment variable "ANYDB_PASSWORD" is set, its
value will be used instead.
There are different output modes you can choose from: simple, wide and
json. The "simple" mode is the default one, it just prints the value as
is. The "wide" mode prints a tabular output similar to the list
subcommand, see there for more details. The options "-n" and "-N" have
the same meaning as in the list command. The "json" mode prints the raw
JSON representation of the whole database entry. Decryption will only
take place in "simple" and "json" mode. The "template" mode provides the
most flexibily, it is detailed in the section TEMPLATES.
list
The list subcommand displays a list of all database entries.
Usage:
Usage:
anydb list [<filter-regex> | -t <tag> ] [-m <mode>] [-nNif] [-T <tpl>] [flags]
Aliases:
list, ls, /, find, search
Flags:
-i, --case-insensitive filter case insensitive
-h, --help help for list
-m, --mode string output format (table|wide|json|template), wide is a verbose table. (default 'table')
-n, --no-headers omit headers in tables
-N, --no-human do not translate to human readable values
-s, --search-fulltext perform a full text search
-t, --tags stringArray tags, multiple allowed
-T, --template string go template for '-m template'
-l, --wide-output output mode: wide
In its simplest form - without any options - , the list command just
prints all keys with their values to STDOUT. Values are being truncated
to maximum of 60 characters, that is, multiline values are not
completely shown in order to keep the tabular view readable.
To get more informations about each entry, use the "-o wide" or "-l"
option. In addition to the key and value also the size, update timestamp
and tags will be printed. Time and size values are converted into a
human readable form, you can suppress this behavior with the "-N"
option. You may omit the headers using the option "-n"
Sometimes you might want to filter the list of entries. Either because
your database grew too large or because you're searching for something.
In that case you have two options: You may supply one or more tags or
provide a filter regexp. To filter by tag, do:
anydb list -t tag1
anydb list -t tag1,tag2
anydb list -t tag1 -t tag2
To filter using a regular expression, do:
anydb list "foo.*bar"
Regular expressions follow the golang re2 syntax. For more details about
the syntax, refer to <https://github.com/google/re2/wiki/Syntax>. Please
note, that this regexp dialect is not PCRE compatible, but supports most
of its features.
If you want to search case insensitive, add the option "-i".
By default anydb only searches through the keys. If you want to search
through the values as well, then use the "-s" option, which enables
full-text search.
You can - as with the get command - use other output modes. The default
mode is "table". The "wide" mode is, as already mentioned, a more
detailed table. Also supported is "json" mode and "template" mode. For
details about using templates see TEMPLATES.
del
Use the del command to delete database entries.
Usage:
Usage:
anydb del <key> [flags]
Aliases:
del, d, rm
Flags:
-h, --help help for del
The subcommand del does not provide any further options, it just deletes
the entry referred to by the given key. No questions are being asked.
edit
The edit command makes it easier to modify larger entries.
Usage:
Usage:
anydb edit <key> [flags]
Aliases:
edit, modify, mod, ed, vi
Flags:
-h, --help help for edit
The subcommand edit does not provide any further options. It works like
this:
1. Write the value info a temporary file.
2. Execute the editor (which one, see below!) with that file.
3. Now you can edit the file and save+close it when done.
4. Anydb picks up the file and if the content has changed, puts its
value into the DB.
By default anydb executes the "vi" command. You can modify this behavior
by setting the environment variable "EDITOR" appropriately.
Please note, that this does not work with binary content!
export
Since the bbolt database file is not portable across platforms (it is
bound to the endianess of the CPU it was being created on), you might
want to create a backup file of your database. You can do this with the
export subcommand.
Usage:
Usage:
anydb export -o <json filename> [flags]
Aliases:
export, dump, backup
Flags:
-h, --help help for export
-o, --output string output to file
The database dump is a JSON representation of the whole database and
will be printed to the file specified with the "-o" option. If you
specify "-" as the filename, it will be written to STDIN.
anydb export -o dump.json
anydb export -o - > dump.json
Please note, that encrypted values will not be decrypted. This might
change in a future version of anydb.
import
The import subcommand can be used to restore a database from a JSON
dump.
Usage:
Usage:
anydb import -i <json file> [flags]
Aliases:
import, restore
Flags:
-r, --file string Filename or - for STDIN
-h, --help help for import
-t, --tags stringArray tags, multiple allowed
The "import" subcommand reads the JSON contents from the file specified
with the "-i" option. If you specify "-" as the filename, it will be
read from STDIN.
anydb import -i - < dump.json
anydb import -i dump.json
cat dump.json | anydb import -i -
If there is already a database, it will be saved by appending a
timestamp and a new database with the contents of the dump will be
created.
serve
Anydb provides a RESTful API, which you can use to manage the database
from somewhere else. The API does not provide any authentication or any
other security measures, so better only use it on localhost.
Usage:
Usage:
anydb serve [-l host:port] [flags]
Flags:
-h, --help help for serve
-l, --listen string host:port (default "localhost:8787")
To start the listener, just execute the serve subcommand. You can tweak
the ip address and tcp port using the "-l" option. The listener will not
fork and run in the foreground. Logs are being printed to STDOUT as long
as the listener runs.
For more details about the API, please see the "REST API" section.
info
The info subcommand shows you some information about your current
database.
Usage:
Usage:
anydb info [flags]
Flags:
-h, --help help for info
-N, --no-human do not translate to human readable values
Data being shown are: filename and size, number of keys per bucket. If
you supply the "-d" option (debug), some bbolt internals are being
displayed as well.
man
The man subcommand shows an unformatted text variant of the manual page
(which are currently reading).
Usage:
Usage:
anydb man [flags]
Flags:
-h, --help help for man
The manual is being piped into the "more" command, which is being
expected to exist according to the POSIX standard on all supported unix
platforms. It might not work on Windows.
TEMPLATES
The get and list commands support a template feature, which is very
handy to create you own kind of formatting. The template syntax being
used is the GO template language, refer to
<https://pkg.go.dev/text/template> for details.
Each template operates on one or more entries, no loop construct is
required, the template provided applies to every matching entry
separatley.
The following template variables can be used:
Key - string
Value - string
Bin - []byte
Created - time.Time
Tags - []string
Encrypted bool
Prepend a single dot (".") before each variable name.
Here are some examples how to use the feature:
Only show the keys of all entries:
anydb list -m template -T "{{ .Key }}"
Format the list in a way so that is possible to evaluate it in a shell:
eval $(anydb get foo -m template -T "key='{{ .Key }}' value='{{ .Value }}' ts='{{ .Created}}'")
echo "Key: $key, Value: $value"
Print the values in CSV format ONLY if they have some tag:
anydb list -m template -T "{{ if .Tags }}{{ .Key }},{{ .Value }},{{ .Created}}{{ end }}"
CONFIGURATION
Anydb looks at the following locations for a configuration file, in that
order:
"$HOME/.config/anydb/anydb.toml"
"$HOME/.anydb.toml"
"anydb.toml" in the current directory
or specify one using "-c"
The configuration format uses the TOML language, refer to
<https://toml.io/en/> for more details. The key names correspond to the
commandline options in most cases.
Configuration follows a certain precedence: the files are tried to be
read in the given order, followed by commandline options. That is, the
last configuration file wins, unless the user provides a commandline
option, then this setting will be taken.
A complete configuration file might look like this:
# defaults
dbfile = "~/.config/anydb/default.db"
dbbucket = "data"
noheaders = false
nohumanize = false
encrypt = false
listen = "localhost:8787"
# different setups for different buckets
[buckets.data]
encrypt = true
[buckets.test]
encrypt = false
Under normal circumstances you don't need a configuration file. However,
if you want to use different buckets, then this might be a handy option.
Buckets are being configured in ini-style with the term "bucket."
followed by the bucket name. In the example above we enable encryption
for the default bucket "data" and disable it for a bucket "test". To use
different buckets, use the "-b" option.
REST API
The subcommand serve starts a simple HTTP service, which responds to
RESTful HTTP requests. The listener responds to all requests with a JSON
encoded response. The response contains the status and the content - if
any - of the requested resource.
The following requests are supported:
GET /anydb/v1/
Returns a JSON encoded list of all entries.
GET /anydb/v1/key
Returns the JSON encoded entry, if found.
PUT /anydb/v1/
Create an entry. Expects a JSON encoded request object in POST data.
DELETE /anydb/v1/key
Delete an entry.
Some curl example calls to the API:
Post a new key: curl -X PUT localhost:8787/anydb/v1/ \ -H 'Content-Type:
application/json' \ -d '{"key":"foo","val":"bar"}'
Retrieve the value:
curl localhost:8787/anydb/v1/foo
List all keys:
curl localhost:8787/anydb/v1/
BUGS
In order to report a bug, unexpected behavior, feature requests or to
submit a patch, please open an issue on github:
<https://github.com/TLINDEN/anydb/issues>.
Please repeat the failing command with debugging enabled "-d" and
include the output in the issue.
LIMITATIONS
The REST API list request doesn't provide any filtering capabilities
yet.
LICENSE
This software is licensed under the GNU GENERAL PUBLIC LICENSE version

View File

@@ -1,13 +1,25 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"unicode/utf8"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
@@ -48,7 +60,7 @@ func Set(conf *cfg.Config) *cobra.Command {
// encrypt?
if conf.Encrypt {
pass, err := app.AskForPassword()
pass, err := getPassword()
if err != nil {
return err
}
@@ -80,7 +92,7 @@ func Get(conf *cfg.Config) *cobra.Command {
)
var cmd = &cobra.Command{
Use: "get <key> [-o <file>] [-m <mode>] [-n]",
Use: "get <key> [-o <file>] [-m <mode>] [-n -N] [-T <tpl>]",
Short: "Retrieve value for a key",
Long: `Retrieve value for a key`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -101,7 +113,7 @@ func Get(conf *cfg.Config) *cobra.Command {
}
if entry.Encrypted {
pass, err := app.AskForPassword()
pass, err := getPassword()
if err != nil {
return err
}
@@ -111,12 +123,7 @@ func Get(conf *cfg.Config) *cobra.Command {
return err
}
if utf8.ValidString(string(clear)) {
entry.Value = string(clear)
} else {
entry.Bin = clear
}
entry.Value = clear
entry.Encrypted = false
}
@@ -124,9 +131,11 @@ func Get(conf *cfg.Config) *cobra.Command {
},
}
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file (ignores -m)")
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (simple|wide|json) (default 'simple')")
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output value to file (ignores -m)")
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (simple|wide|json|template) (default 'simple')")
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
cmd.Aliases = append(cmd.Aliases, "show")
cmd.Aliases = append(cmd.Aliases, "g")
@@ -166,38 +175,6 @@ func Del(conf *cfg.Config) *cobra.Command {
return cmd
}
func Export(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
)
var cmd = &cobra.Command{
Use: "export [-o <json filename>]",
Short: "Export database to json",
Long: `Export database to json`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
conf.Mode = "json"
entries, err := conf.DB.List(&attr)
if err != nil {
return err
}
return output.WriteFile(&attr, conf, entries)
},
}
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file")
cmd.Aliases = append(cmd.Aliases, "dump")
cmd.Aliases = append(cmd.Aliases, "backup")
return cmd
}
func List(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
@@ -205,7 +182,7 @@ func List(conf *cfg.Config) *cobra.Command {
)
var cmd = &cobra.Command{
Use: "list [-t <tag>] [-o <mode>] [<filter-regex>]",
Use: "list [<filter-regex> | -t <tag> ] [-m <mode>] [-nNis] [-T <tpl>]",
Short: "List database contents",
Long: `List database contents`,
RunE: func(cmd *cobra.Command, args []string) error {
@@ -213,7 +190,11 @@ func List(conf *cfg.Config) *cobra.Command {
cmd.SilenceUsage = true
if len(args) > 0 {
attr.Args = args
if conf.CaseInsensitive {
attr.Args = []string{"(?i)" + args[0]}
} else {
attr.Args = args
}
}
// turn comma list into slice, if needed
@@ -225,7 +206,7 @@ func List(conf *cfg.Config) *cobra.Command {
conf.Mode = "wide"
}
entries, err := conf.DB.List(&attr)
entries, err := conf.DB.List(&attr, conf.Fulltext)
if err != nil {
return err
}
@@ -234,76 +215,38 @@ func List(conf *cfg.Config) *cobra.Command {
},
}
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (table|wide|json), wide is a verbose table. (default 'table')")
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (table|wide|json|template), wide is a verbose table. (default 'table')")
cmd.PersistentFlags().StringVarP(&conf.Template, "template", "T", "", "go template for '-m template'")
cmd.PersistentFlags().BoolVarP(&wide, "wide-output", "l", false, "output mode: wide")
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
cmd.PersistentFlags().BoolVarP(&conf.CaseInsensitive, "case-insensitive", "i", false, "filter case insensitive")
cmd.PersistentFlags().BoolVarP(&conf.Fulltext, "search-fulltext", "s", false, "perform a full text search")
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
cmd.Aliases = append(cmd.Aliases, "/")
cmd.Aliases = append(cmd.Aliases, "ls")
cmd.Aliases = append(cmd.Aliases, "/")
cmd.Aliases = append(cmd.Aliases, "find")
cmd.Aliases = append(cmd.Aliases, "search")
return cmd
}
func Import(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
)
func getPassword() ([]byte, error) {
var pass []byte
var cmd = &cobra.Command{
Use: "import [<json file>]",
Short: "Import database dump",
Long: `Import database dump`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
envpass := os.Getenv("ANYDB_PASSWORD")
return conf.DB.Import(&attr)
},
if envpass == "" {
readpass, err := app.AskForPassword()
if err != nil {
return nil, err
}
pass = readpass
} else {
pass = []byte(envpass)
}
cmd.PersistentFlags().StringVarP(&attr.File, "file", "r", "", "Filename or - for STDIN")
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
cmd.Aliases = append(cmd.Aliases, "add")
cmd.Aliases = append(cmd.Aliases, "s")
cmd.Aliases = append(cmd.Aliases, "+")
return cmd
}
func Help(conf *cfg.Config) *cobra.Command {
return nil
}
func Man(conf *cfg.Config) *cobra.Command {
var cmd = &cobra.Command{
Use: "man",
Short: "show manual page",
Long: `show manual page`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
man := exec.Command("less", "-")
var b bytes.Buffer
b.WriteString(manpage)
man.Stdout = os.Stdout
man.Stdin = &b
man.Stderr = os.Stderr
err := man.Run()
if err != nil {
return fmt.Errorf("failed to execute 'less': %w", err)
}
return nil
},
}
return cmd
return pass, nil
}

326
cmd/extra.go Normal file
View File

@@ -0,0 +1,326 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
"github.com/tlinden/anydb/output"
"github.com/tlinden/anydb/rest"
)
func Export(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
)
var cmd = &cobra.Command{
Use: "export -o <json filename>",
Short: "Export database to json file",
Long: `Export database to json file`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
conf.Mode = "json"
entries, err := conf.DB.Getall(&attr)
if err != nil {
return err
}
return output.WriteJSON(&attr, conf, entries)
},
}
cmd.PersistentFlags().StringVarP(&attr.File, "output-file", "o", "", "filename or - for STDIN")
if err := cmd.MarkPersistentFlagRequired("output-file"); err != nil {
panic(err)
}
cmd.Aliases = append(cmd.Aliases, "dump")
cmd.Aliases = append(cmd.Aliases, "backup")
return cmd
}
func Import(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
)
var cmd = &cobra.Command{
Use: "import -i <json file>",
Short: "Import database dump",
Long: `Import database dump`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
out, err := conf.DB.Import(&attr)
if err != nil {
return err
}
fmt.Print(out)
return nil
},
}
cmd.PersistentFlags().StringVarP(&attr.File, "import-file", "i", "", "filename or - for STDIN")
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
if err := cmd.MarkPersistentFlagRequired("import-file"); err != nil {
panic(err)
}
cmd.Aliases = append(cmd.Aliases, "restore")
return cmd
}
func Help(conf *cfg.Config) *cobra.Command {
return nil
}
func Man(conf *cfg.Config) *cobra.Command {
var cmd = &cobra.Command{
Use: "man",
Short: "show manual page",
Long: `show manual page`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
man := exec.Command("less", "-")
var b bytes.Buffer
b.WriteString(manpage)
man.Stdout = os.Stdout
man.Stdin = &b
man.Stderr = os.Stderr
err := man.Run()
if err != nil {
return fmt.Errorf("failed to execute 'less': %w", err)
}
return nil
},
}
return cmd
}
func Serve(conf *cfg.Config) *cobra.Command {
var cmd = &cobra.Command{
Use: "serve [-l host:port]",
Short: "run REST API listener",
Long: `run REST API listener`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
return rest.Runserver(conf, nil)
},
}
cmd.PersistentFlags().StringVarP(&conf.Listen, "listen", "l", "localhost:8787", "host:port")
return cmd
}
func Info(conf *cfg.Config) *cobra.Command {
var cmd = &cobra.Command{
Use: "info",
Short: "info",
Long: `show info about database`,
RunE: func(cmd *cobra.Command, args []string) error {
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
info, err := conf.DB.Info()
if err != nil {
return err
}
return output.Info(os.Stdout, conf, info)
},
}
cmd.PersistentFlags().BoolVarP(&conf.NoHumanize, "no-human", "N", false, "do not translate to human readable values")
return cmd
}
func Edit(conf *cfg.Config) *cobra.Command {
var (
attr app.DbAttr
)
var cmd = &cobra.Command{
Use: "edit <key>",
Short: "Edit a key",
Long: `Edit a key`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("no key specified")
}
// errors at this stage do not cause the usage to be shown
cmd.SilenceUsage = true
password := []byte{}
if len(args) > 0 {
attr.Key = args[0]
}
// fetch entry
entry, err := conf.DB.Get(&attr)
if err != nil {
return err
}
if len(entry.Value) == 0 && entry.Binary {
return errors.New("key contains binary uneditable content")
}
// decrypt if needed
if entry.Encrypted {
pass, err := getPassword()
if err != nil {
return err
}
password = pass
clear, err := app.Decrypt(pass, entry.Value)
if err != nil {
return err
}
entry.Value = clear
entry.Encrypted = false
}
// determine editor, vi is default
editor := getEditor()
// save file to a temp file, call the editor with it, read
// it back in and compare the content with the original
// one
newcontent, err := editContent(editor, string(entry.Value))
if err != nil {
return err
}
// all is valid, fill our DB feeder
newattr := app.DbAttr{
Key: attr.Key,
Tags: attr.Tags,
Encrypted: attr.Encrypted,
Val: []byte(newcontent),
}
// encrypt if needed
if conf.Encrypt {
err = app.Encrypt(password, &attr)
if err != nil {
return err
}
}
// done
return conf.DB.Set(&newattr)
},
}
cmd.Aliases = append(cmd.Aliases, "modify")
cmd.Aliases = append(cmd.Aliases, "mod")
cmd.Aliases = append(cmd.Aliases, "ed")
cmd.Aliases = append(cmd.Aliases, "vi")
return cmd
}
func getEditor() string {
editor := "vi"
enveditor, present := os.LookupEnv("EDITOR")
if present {
if editor != "" {
editor = enveditor
}
}
return editor
}
// taken from github.com/tlinden/rpn/ (my own program)
func editContent(editor string, content string) (string, error) {
// create a temp file
tmp, err := os.CreateTemp("", "stack")
if err != nil {
return "", fmt.Errorf("failed to create templ file: %w", err)
}
defer os.Remove(tmp.Name())
// put the content into a tmp file
_, err = tmp.WriteString(content)
if err != nil {
return "", fmt.Errorf("failed to write value to temp file: %w", err)
}
// execute editor with our tmp file containing current stack
cmd := exec.Command(editor, tmp.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return "", fmt.Errorf("failed to run editor command %s: %w", editor, err)
}
// read the file back in
modified, err := os.Open(tmp.Name())
if err != nil {
return "", fmt.Errorf("failed to open temp file: %w", err)
}
defer modified.Close()
newcontent, err := io.ReadAll(modified)
if err != nil {
return "", fmt.Errorf("failed to read from temp file: %w", err)
}
newcontentstr := string(newcontent)
if content == newcontentstr {
return "", fmt.Errorf("content not modified, aborting")
}
return newcontentstr, nil
}

View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
@@ -6,6 +22,7 @@ import (
"os"
"path/filepath"
"github.com/alecthomas/repr"
"github.com/spf13/cobra"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
@@ -29,22 +46,49 @@ func completion(cmd *cobra.Command, mode string) error {
func Execute() {
var (
conf cfg.Config
configfile string
ShowVersion bool
ShowCompletion string
)
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
SearchConfigs := []string{
filepath.Join(home, ".config", "anydb", "anydb.toml"),
filepath.Join(home, ".anydb.toml"),
"anydb.toml",
}
var rootCmd = &cobra.Command{
Use: "anydb <command> [options]",
Short: "anydb",
Long: `A personal key value store`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
db, err := app.New(conf.Dbfile, conf.Debug)
db, err := app.New(conf.Dbfile, conf.Dbbucket, conf.Debug)
if err != nil {
return err
}
conf.DB = db
var configs []string
if configfile != "" {
configs = []string{configfile}
} else {
configs = SearchConfigs
}
if err := conf.GetConfig(configs); err != nil {
return err
}
if conf.Debug {
repr.Println(conf)
}
return nil
},
@@ -66,24 +110,32 @@ func Execute() {
},
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
// options
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "v", false, "Print program version")
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f",
filepath.Join(home, ".config", "anydb", "default.db"), "DB file to use")
rootCmd.PersistentFlags().StringVarP(&conf.Dbbucket, "bucket", "b",
app.BucketData, "use other bucket (default: "+app.BucketData+")")
rootCmd.PersistentFlags().StringVarP(&configfile, "config", "c", "", "toml config file")
// CRUD
rootCmd.AddCommand(Set(&conf))
rootCmd.AddCommand(List(&conf))
rootCmd.AddCommand(Get(&conf))
rootCmd.AddCommand(Del(&conf))
// backup
rootCmd.AddCommand(Export(&conf))
rootCmd.AddCommand(Import(&conf))
// REST API
rootCmd.AddCommand(Serve(&conf))
// auxiliary
rootCmd.AddCommand(Man(&conf))
rootCmd.AddCommand(Info(&conf))
rootCmd.AddCommand(Edit(&conf))
err = rootCmd.Execute()
if err != nil {

36
common/io.go Normal file
View File

@@ -0,0 +1,36 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package common
import "os"
func CleanError(file string, err error) error {
// remove given [backup] file and forward the given error
os.Remove(file)
return err
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
// return false on any error
return false
}
return !info.IsDir()
}

View File

@@ -1,3 +1,19 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
version: "3.9"
services:
init:

14
example.toml Normal file
View File

@@ -0,0 +1,14 @@
# defaults
dbfile = "~/.config/anydb/default.db"
dbbucket = "data"
noheaders = false
nohumanize = false
encrypt = false
listen = "localhost:8787"
# different setups for different buckets
[buckets.data]
encrypt = true
[buckets.test]
encrypt = false

16
go.mod
View File

@@ -4,16 +4,30 @@ go 1.22.1
require (
github.com/alecthomas/repr v0.4.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gofiber/fiber/v2 v2.52.5 // indirect
github.com/gofiber/fiber/v3 v3.0.0-beta.3 // indirect
github.com/gofiber/utils/v2 v2.0.0-beta.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.55.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
go.etcd.io/bbolt v1.3.11 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
)

41
go.sum
View File

@@ -2,25 +2,54 @@ github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t
github.com/Sereal/Sereal v0.0.0-20190618215532-0b8ac451a863/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/asdine/storm/v3 v3.2.1 h1:I5AqhkPK6nBZ/qJXySdI7ot5BlXSZ7qvDY1zAn5ZJac=
github.com/asdine/storm/v3 v3.2.1/go.mod h1:LEpXwGt4pIqrE/XcTvCnZHT5MgZCV6Ub9q7yQzOFWr0=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/fiber/v3 v3.0.0-beta.3 h1:7Q2I+HsIqnIEEDB+9oe7Gadpakh6ZLhXpTYz/L20vrg=
github.com/gofiber/fiber/v3 v3.0.0-beta.3/go.mod h1:kcMur0Dxqk91R7p4vxEpJfDWZ9u5IfvrtQc8Bvv/JmY=
github.com/gofiber/utils/v2 v2.0.0-beta.4 h1:1gjbVFFwVwUb9arPcqiB6iEjHBwo7cHsyS41NeIW3co=
github.com/gofiber/utils/v2 v2.0.0-beta.4/go.mod h1:sdRsPU1FXX6YiDGGxd+q2aPJRMzpsxdzCXo9dz+xtOY=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJwgrqM=
github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -29,6 +58,14 @@ github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3k
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA=
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
@@ -40,6 +77,8 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20191105084925-a882066a44e0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
@@ -50,6 +89,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA=
golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

16
main.go
View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (

View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (

View File

@@ -1,6 +1,6 @@
#!/bin/bash
# Copyright © 2023 Thomas von Dein
# Copyright © 2024 Thomas von Dein
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by

50
output/export.go Normal file
View File

@@ -0,0 +1,50 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package output
import (
"encoding/json"
"fmt"
"os"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
func WriteJSON(attr *app.DbAttr, conf *cfg.Config, entries app.DbEntries) error {
jsonentries, err := json.Marshal(entries)
if err != nil {
return fmt.Errorf("failed to marshall json: %w", err)
}
if attr.File == "-" {
fmt.Println(string(jsonentries))
} else {
fd, err := os.OpenFile(attr.File, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
if _, err := fd.Write(jsonentries); err != nil {
return fmt.Errorf("failed writing to file %s: %w", attr.File, err)
}
fmt.Printf("database contents exported to %s\n", attr.File)
}
return nil
}

View File

@@ -1,11 +1,30 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package output
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"strings"
tpl "text/template"
"github.com/dustin/go-humanize"
"github.com/olekukonko/tablewriter"
@@ -14,16 +33,13 @@ import (
)
func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
// FIXME: call sort here
switch conf.Mode {
case "wide":
fallthrough
case "":
fallthrough
case "table":
case "wide", "", "table":
return ListTable(writer, conf, entries)
case "json":
return ListJson(writer, conf, entries)
case "template":
return ListTemplate(writer, conf, entries)
default:
return errors.New("unsupported mode")
}
@@ -39,45 +55,64 @@ func ListJson(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
return nil
}
func ListTemplate(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
tmpl, err := tpl.New("list").Parse(conf.Template)
if err != nil {
return fmt.Errorf("failed to parse output template: %w", err)
}
buf := bytes.Buffer{}
for _, row := range entries {
buf.Reset()
err = tmpl.Execute(&buf, row)
if err != nil {
return fmt.Errorf("failed to execute output template: %w", err)
}
if buf.Len() > 0 {
fmt.Fprintln(writer, buf.String())
}
}
return nil
}
func ListTable(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
if !conf.NoHeaders {
if conf.Mode == "wide" {
table.SetHeader([]string{"KEY", "TAGS", "SIZE", "AGE", "VALUE"})
table.SetHeader([]string{"KEY", "TAGS", "SIZE", "UPDATED", "VALUE"})
} else {
table.SetHeader([]string{"KEY", "VALUE"})
}
}
for _, row := range entries {
size := len(row.Value)
if row.Encrypted {
row.Value = "<encrypted-content>"
}
if len(row.Bin) > 0 {
row.Value = "<binary-content>"
size = len(row.Bin)
}
if len(row.Value) > 60 {
row.Value = row.Value[0:60] + "..."
}
if conf.Mode == "wide" {
table.Append([]string{
row.Key,
strings.Join(row.Tags, ","),
humanize.Bytes(uint64(size)),
//row.Created.Format("02.01.2006T03:04.05"),
humanize.Time(row.Created),
row.Value,
})
switch conf.NoHumanize {
case true:
table.Append([]string{
row.Key,
strings.Join(row.Tags, ","),
strconv.FormatUint(row.Size, 10),
row.Created.AsTime().Format("02.01.2006T03:04.05"),
row.Preview,
})
default:
table.Append([]string{
row.Key,
strings.Join(row.Tags, ","),
humanize.Bytes(uint64(row.Size)),
humanize.Time(row.Created.AsTime()),
row.Preview,
})
}
} else {
table.Append([]string{row.Key, row.Value})
table.Append([]string{row.Key, row.Preview})
}
}

View File

@@ -1,3 +1,19 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package output
import (
@@ -5,57 +21,33 @@ import (
"fmt"
"io"
"os"
"strings"
"reflect"
"github.com/dustin/go-humanize"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
"golang.org/x/term"
//"github.com/alecthomas/repr"
)
func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
if attr.File != "" {
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
defer fd.Close()
if len(entry.Bin) > 0 {
// binary file content
_, err = fd.Write(entry.Bin)
} else {
val := entry.Value
if !strings.HasSuffix(val, "\n") {
// always add a terminal newline
val += "\n"
}
_, err = fd.Write([]byte(val))
}
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", attr.File, err)
}
return nil
return WriteFile(writer, conf, attr, entry)
}
isatty := term.IsTerminal(int(os.Stdout.Fd()))
switch conf.Mode {
case "simple":
fallthrough
case "":
if len(entry.Bin) > 0 {
case "simple", "":
if entry.Binary {
if isatty {
fmt.Println("binary data omitted")
} else {
os.Stdout.Write(entry.Bin)
os.Stdout.Write(entry.Value)
}
} else {
fmt.Print(entry.Value)
if !strings.HasSuffix(entry.Value, "\n") {
fmt.Print(string(entry.Value))
if entry.Value[entry.Size-1] != '\n' {
// always add a terminal newline
fmt.Println()
}
@@ -68,8 +60,76 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn
fmt.Println(string(jsonentry))
case "wide":
return ListTable(writer, conf, app.DbEntries{*entry})
return ListTable(writer, conf, app.DbEntries{entry})
case "template":
return ListTemplate(writer, conf, app.DbEntries{entry})
}
return nil
}
func WriteFile(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
var fileHandle *os.File
var err error
if attr.File == "-" {
fileHandle = os.Stdout
} else {
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
defer fd.Close()
fileHandle = fd
}
// actually write file content
_, err = fileHandle.Write(entry.Value)
if !entry.Binary {
if entry.Value[entry.Size-1] != '\n' {
// always add a terminal newline
_, err = fileHandle.Write([]byte{'\n'})
}
}
if err != nil {
return fmt.Errorf("failed to write to file %s: %w", attr.File, err)
}
return nil
}
func Info(writer io.Writer, conf *cfg.Config, info *app.DbInfo) error {
fmt.Fprintf(writer, "Database: %s\n", info.Path)
for _, bucket := range info.Buckets {
if conf.NoHumanize {
fmt.Fprintf(
writer,
"%19s: %s\n%19s: %d\n%19s: %d\n%19s: %t\n",
"Bucket", bucket.Name,
"Size", bucket.Size,
"Keys", bucket.Keys,
"Encrypted", conf.Encrypt)
} else {
fmt.Fprintf(
writer,
"%19s: %s\n%19s: %s\n%19s: %d\n",
"Bucket", bucket.Name,
"Size", humanize.Bytes(uint64(bucket.Size)),
"Keys", bucket.Keys)
}
if conf.Debug {
val := reflect.ValueOf(&bucket.Stats).Elem()
for i := 0; i < val.NumField(); i++ {
fmt.Fprintf(writer, "%19s: %v\n", val.Type().Field(i).Name, val.Field(i))
}
}
fmt.Fprintln(writer)
}
return nil
}

View File

@@ -1,34 +0,0 @@
package output
import (
"encoding/json"
"fmt"
"os"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
func WriteFile(attr *app.DbAttr, conf *cfg.Config, entries app.DbEntries) error {
jsonentries, err := json.Marshal(entries)
if err != nil {
return fmt.Errorf("failed to marshall json: %w", err)
}
if attr.File == "-" {
fmt.Println(string(jsonentries))
} else {
fd, err := os.OpenFile(attr.File, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to open file %s for writing: %w", attr.File, err)
}
if _, err := fd.Write(jsonentries); err != nil {
return fmt.Errorf("failed writing to file %s: %w", attr.File, err)
}
fmt.Printf("database contents exported to %s\n", attr.File)
}
return nil
}

140
rest/handlers.go Normal file
View File

@@ -0,0 +1,140 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package rest
import (
//"github.com/alecthomas/repr"
"github.com/gofiber/fiber/v2"
"github.com/tlinden/anydb/app"
"github.com/tlinden/anydb/cfg"
)
type SetContext struct {
Query string `json:"query" form:"query"`
}
type ListResponse struct {
Success bool
Code int
Entries app.DbEntries
}
type SingleResponse struct {
Success bool
Code int
Entry *app.DbEntry
}
func RestList(c *fiber.Ctx, conf *cfg.Config) error {
attr := new(app.DbAttr)
if len(c.Body()) > 0 {
if err := c.BodyParser(attr); err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"errors": err.Error(),
})
}
}
// get list
entries, err := conf.DB.List(attr, attr.Fulltext)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to list keys: "+err.Error())
}
return c.Status(fiber.StatusOK).JSON(
ListResponse{
Success: true,
Code: fiber.StatusOK,
Entries: entries,
},
)
}
func RestGet(c *fiber.Ctx, conf *cfg.Config) error {
if c.Params("key") == "" {
return JsonStatus(c, fiber.StatusForbidden,
"key not provided")
}
// get list
entry, err := conf.DB.Get(&app.DbAttr{Key: c.Params("key")})
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to get key: "+err.Error())
}
if entry.Key == "" {
return JsonStatus(c, fiber.StatusForbidden,
"Key does not exist")
}
return c.Status(fiber.StatusOK).JSON(
SingleResponse{
Success: true,
Code: fiber.StatusOK,
Entry: entry,
},
)
}
func RestDelete(c *fiber.Ctx, conf *cfg.Config) error {
if c.Params("key") == "" {
return JsonStatus(c, fiber.StatusForbidden,
"key not provided")
}
// get list
err := conf.DB.Del(&app.DbAttr{Key: c.Params("key")})
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to delete key: "+err.Error())
}
return c.Status(fiber.StatusOK).JSON(
Result{
Success: true,
Code: fiber.StatusOK,
Message: "key deleted",
},
)
}
func RestSet(c *fiber.Ctx, conf *cfg.Config) error {
attr := new(app.DbAttr)
if err := c.BodyParser(attr); err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"errors": err.Error(),
})
}
err := conf.DB.Set(attr)
if err != nil {
return JsonStatus(c, fiber.StatusForbidden,
"Unable to set key: "+err.Error())
}
return c.Status(fiber.StatusOK).JSON(
Result{
Success: true,
Code: fiber.StatusOK,
},
)
}

118
rest/serve.go Normal file
View File

@@ -0,0 +1,118 @@
/*
Copyright © 2024 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package rest
import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/tlinden/anydb/cfg"
)
// used to return to the api client
type Result struct {
Success bool `json:"success"`
Message string `json:"message"`
Code int `json:"code"`
}
func Runserver(conf *cfg.Config, args []string) error {
// setup api server
router := SetupServer(conf)
// public rest api routes
api := router.Group("/anydb/v1")
{
api.Get("/", func(c *fiber.Ctx) error {
return RestList(c, conf)
})
api.Post("/", func(c *fiber.Ctx) error {
// same thing as above but allows to supply parameters, see app.Dbattr{}
return RestList(c, conf)
})
api.Get("/:key", func(c *fiber.Ctx) error {
return RestGet(c, conf)
})
api.Delete("/:key", func(c *fiber.Ctx) error {
return RestDelete(c, conf)
})
api.Put("/", func(c *fiber.Ctx) error {
return RestSet(c, conf)
})
}
// public routes
{
router.Get("/", func(c *fiber.Ctx) error {
return c.Send([]byte("Use the REST API"))
})
}
return router.Listen(conf.Listen)
}
func SetupServer(conf *cfg.Config) *fiber.App {
// disable colors
fiber.DefaultColors = fiber.Colors{}
router := fiber.New(fiber.Config{
CaseSensitive: true,
StrictRouting: true,
Immutable: true,
ServerHeader: "anydb serve",
AppName: "anydb",
})
router.Use(logger.New(logger.Config{
Format: "${pid} ${ip}:${port} ${status} - ${method} ${path}\n",
DisableColors: true,
}))
router.Use(cors.New(cors.Config{
AllowMethods: "GET,PUT,POST,DELETE",
ExposeHeaders: "Content-Type,Accept",
}))
router.Use(compress.New(compress.Config{
Level: compress.LevelBestSpeed,
}))
return router
}
/*
Wrapper to respond with proper json status, message and code,
shall be prepared and called by the handlers directly.
*/
func JsonStatus(c *fiber.Ctx, code int, msg string) error {
success := true
if code != fiber.StatusOK {
success = false
}
return c.Status(code).JSON(Result{
Code: code,
Message: msg,
Success: success,
})
}

View File

@@ -1,3 +1,20 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# simple file, we cannot use redirection here, so dd is our friend
exec dd if=/dev/random of=file.txt count=5 bs=10
@@ -15,3 +32,20 @@ stdout 50
# look if it's inside the db
exec anydb -f test.db ls
stdout datum.*binary-content
# do the same thing with text content, start with a new text entry
exec anydb -f test.db set feed alpha
# which we write to a file
exec anydb -f test.db get feed -o out2.txt
exists out2.txt
# check if its filled (5 bytes + newline)
exec ls -l out2.txt
stdout 6
# compare content
exec cat out2.txt
stdout alpha

View File

@@ -1,3 +1,20 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# check default outputs
exec anydb -v

View File

@@ -1,3 +1,20 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# setup simple db
exec anydb -f test.db set foo bar
@@ -6,10 +23,10 @@ exec anydb -f test.db export -o backup.json
stdout 'database contents exported to backup.json'
# import into new db
exec anydb -f new.db import -r backup.json
exec anydb -f new.db import -i backup.json
stdout 'imported.*entries'
# check contents
exec anydb -f new.db list
exec anydb -f new.db list bar -s
stdout foo.*bar

View File

@@ -1,6 +1,26 @@
#
# Copyright © 2024 Thomas von Dein
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# simple entry
exec anydb -f test.db set foo bar
# single entry uc()
exec anydb -f test.db set MUCHAS gracias
# entry with tags
exec anydb -f test.db set color grey -t flower,plant
@@ -17,9 +37,13 @@ exec anydb -f test.db list -t flower
! stdout bar
# list with filter
exec anydb -f test.db list b.r
exec anydb -f test.db list b.r -s
stdout bar
# list with -i filter
exec anydb -f test.db list -is mucha
stdout mucha
# get single entry
exec anydb -f test.db get color
stdout grey