mirror of
https://codeberg.org/scip/anydb.git
synced 2025-12-17 12:31:02 +01:00
Compare commits
24 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fe3951f3c2 | |||
| 249c3f1cfb | |||
| 8687e084bf | |||
|
|
24240b85f2 | ||
|
|
8e400c6831 | ||
|
|
3de65aa1c3 | ||
| be79886e89 | |||
|
|
dc328afa44 | ||
|
|
cfa739ac83 | ||
|
|
6de8a6168a | ||
|
|
8d04f798e0 | ||
|
|
c414a8972a | ||
|
|
e6f20f5623 | ||
|
|
41d9bd6e07 | ||
|
|
e392aa924f | ||
|
|
a49aa5b8d0 | ||
|
|
6e3fb4ef91 | ||
| 7bc30da609 | |||
| 37ca653461 | |||
| 1119b06330 | |||
| f93b7bec08 | |||
| d94868132d | |||
| ba39e3f8cd | |||
| b7111002f1 |
16
Dockerfile
16
Dockerfile
@@ -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
|
FROM golang:1.22-alpine as builder
|
||||||
|
|
||||||
RUN apk update
|
RUN apk update
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -66,11 +66,11 @@ clean:
|
|||||||
rm -rf $(tool) releases coverage.out
|
rm -rf $(tool) releases coverage.out
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -v ./...
|
ANYDB_PASSWORD=test go test -v ./...
|
||||||
|
|
||||||
singletest:
|
singletest:
|
||||||
@echo "Call like this: ''make singletest TEST=TestPrepareColumns MOD=lib"
|
@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:
|
cover-report:
|
||||||
go test ./... -cover -coverprofile=coverage.out
|
go test ./... -cover -coverprofile=coverage.out
|
||||||
|
|||||||
84
README.md
84
README.md
@@ -16,6 +16,7 @@ reasons:
|
|||||||
often, which is not good for a tool intended to be used for many
|
often, which is not good for a tool intended to be used for many
|
||||||
years.
|
years.
|
||||||
- more features:
|
- more features:
|
||||||
|
- output table in list mode uses TAB separator
|
||||||
- better STDIN + pipe support
|
- better STDIN + pipe support
|
||||||
- supports JSON output
|
- supports JSON output
|
||||||
- supports more verbose tabular output
|
- supports more verbose tabular output
|
||||||
@@ -23,6 +24,8 @@ reasons:
|
|||||||
- tagging
|
- tagging
|
||||||
- filtering using tags
|
- filtering using tags
|
||||||
- encryption of entries
|
- encryption of entries
|
||||||
|
- templates for custom output for maximum flexibility
|
||||||
|
- includes a tiny web server, which serves a restful API
|
||||||
|
|
||||||
**anydb** can do all the things you can do with skate:
|
**anydb** can do all the things you can do with skate:
|
||||||
|
|
||||||
@@ -74,8 +77,8 @@ anydb list '[a-z]+\d'
|
|||||||
anydb list -o wide
|
anydb list -o wide
|
||||||
KEY TAGS SIZE AGE VALUE
|
KEY TAGS SIZE AGE VALUE
|
||||||
blah important 4 B 7 seconds ago haha
|
blah important 4 B 7 seconds ago haha
|
||||||
foo 3 B 15 seconds ago bar
|
foo 3 B 15 seconds ago bar
|
||||||
猫咪 3 B 3 seconds ago 喵
|
猫咪 3 B 3 seconds ago 喵
|
||||||
|
|
||||||
# there are shortcuts as well
|
# there are shortcuts as well
|
||||||
anydb ls -l
|
anydb ls -l
|
||||||
@@ -92,9 +95,60 @@ anydb import -r backup.json
|
|||||||
|
|
||||||
# you can encrypt entries. anydb asks for a passphrase
|
# you can encrypt entries. anydb asks for a passphrase
|
||||||
# and will do the same when you retrieve the key using the
|
# 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
|
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/
|
||||||
|
|
||||||
|
# sometimes you need to know some details about the current database
|
||||||
|
# add -d for more details
|
||||||
|
anydb info
|
||||||
|
|
||||||
# it comes with a manpage builtin
|
# it comes with a manpage builtin
|
||||||
anydb man
|
anydb man
|
||||||
```
|
```
|
||||||
@@ -130,6 +184,30 @@ There are multiple ways to install **anydb**:
|
|||||||
If you do not find a binary release for your platform, please don't
|
If you do not find a binary release for your platform, please don't
|
||||||
hesitate to ask me about it, I'll add it.
|
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
|
## Documentation
|
||||||
|
|
||||||
The documentation is provided as a unix man-page. It will be
|
The documentation is provided as a unix man-page. It will be
|
||||||
|
|||||||
3
TODO.md
Normal file
3
TODO.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
- repl
|
||||||
|
- mime-type => exec app + value
|
||||||
|
- [edit command](https://github.com/TLINDEN/rpnc/blob/master/command.go#L249)
|
||||||
4
anydb.1
4
anydb.1
@@ -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:
|
.\" Standard preamble:
|
||||||
.\" ========================================================================
|
.\" ========================================================================
|
||||||
@@ -133,7 +133,7 @@
|
|||||||
.\" ========================================================================
|
.\" ========================================================================
|
||||||
.\"
|
.\"
|
||||||
.IX Title "ANYDB 1"
|
.IX Title "ANYDB 1"
|
||||||
.TH ANYDB 1 "2024-12-18" "1" "User Commands"
|
.TH ANYDB 1 "2024-12-22" "1" "User Commands"
|
||||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||||
.\" way too many mistakes in technical documents.
|
.\" way too many mistakes in technical documents.
|
||||||
.if n .ad l
|
.if n .ad l
|
||||||
|
|||||||
16
app/attr.go
16
app/attr.go
@@ -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
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -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
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
143
app/db.go
143
app/db.go
@@ -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
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -13,9 +29,12 @@ import (
|
|||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const MaxValueWidth int = 60
|
||||||
|
|
||||||
type DB struct {
|
type DB struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
Dbfile string
|
Dbfile string
|
||||||
|
Bucket string
|
||||||
DB *bolt.DB
|
DB *bolt.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +46,39 @@ type DbEntry struct {
|
|||||||
Bin []byte `json:"bin"`
|
Bin []byte `json:"bin"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Created time.Time `json:"created"`
|
Created time.Time `json:"created"`
|
||||||
|
Size int
|
||||||
|
}
|
||||||
|
|
||||||
|
type BucketInfo struct {
|
||||||
|
Name string
|
||||||
|
Keys int
|
||||||
|
Size int
|
||||||
|
Sequence uint64
|
||||||
|
Stats bolt.BucketStats
|
||||||
|
}
|
||||||
|
|
||||||
|
type DbInfo struct {
|
||||||
|
Buckets []BucketInfo
|
||||||
|
Path string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post process an entry for list output.
|
||||||
|
// Do NOT call it during write processing!
|
||||||
|
func (entry *DbEntry) Normalize() {
|
||||||
|
entry.Size = len(entry.Value)
|
||||||
|
|
||||||
|
if entry.Encrypted {
|
||||||
|
entry.Value = "<encrypted-content>"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(entry.Bin) > 0 {
|
||||||
|
entry.Value = "<binary-content>"
|
||||||
|
entry.Size = len(entry.Bin)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(entry.Value) > MaxValueWidth {
|
||||||
|
entry.Value = entry.Value[0:MaxValueWidth] + "..."
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type DbEntries []DbEntry
|
type DbEntries []DbEntry
|
||||||
@@ -37,8 +89,8 @@ type DbTag struct {
|
|||||||
|
|
||||||
const BucketData string = "data"
|
const BucketData string = "data"
|
||||||
|
|
||||||
func New(file string, debug bool) (*DB, error) {
|
func New(file string, bucket string, debug bool) (*DB, error) {
|
||||||
return &DB{Debug: debug, Dbfile: file}, nil
|
return &DB{Debug: debug, Dbfile: file, Bucket: bucket}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Open() error {
|
func (db *DB) Open() error {
|
||||||
@@ -76,7 +128,7 @@ func (db *DB) List(attr *DbAttr) (DbEntries, error) {
|
|||||||
|
|
||||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||||
|
|
||||||
bucket := tx.Bucket([]byte(BucketData))
|
bucket := tx.Bucket([]byte(db.Bucket))
|
||||||
if bucket == nil {
|
if bucket == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -144,7 +196,7 @@ func (db *DB) Set(attr *DbAttr) error {
|
|||||||
// any tags. if so, we initialize our update struct with these
|
// any tags. if so, we initialize our update struct with these
|
||||||
// tags unless it has new tags configured.
|
// tags unless it has new tags configured.
|
||||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(BucketData))
|
bucket := tx.Bucket([]byte(db.Bucket))
|
||||||
if bucket == nil {
|
if bucket == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -173,7 +225,7 @@ func (db *DB) Set(attr *DbAttr) error {
|
|||||||
|
|
||||||
err = db.DB.Update(func(tx *bolt.Tx) error {
|
err = db.DB.Update(func(tx *bolt.Tx) error {
|
||||||
// insert data
|
// insert data
|
||||||
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketData))
|
bucket, err := tx.CreateBucketIfNotExists([]byte(db.Bucket))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create DB bucket: %w", err)
|
return fmt.Errorf("failed to create DB bucket: %w", err)
|
||||||
}
|
}
|
||||||
@@ -207,13 +259,14 @@ func (db *DB) Get(attr *DbAttr) (*DbEntry, error) {
|
|||||||
entry := DbEntry{}
|
entry := DbEntry{}
|
||||||
|
|
||||||
err := db.DB.View(func(tx *bolt.Tx) error {
|
err := db.DB.View(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(BucketData))
|
bucket := tx.Bucket([]byte(db.Bucket))
|
||||||
if bucket == nil {
|
if bucket == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
jsonentry := bucket.Get([]byte(attr.Key))
|
jsonentry := bucket.Get([]byte(attr.Key))
|
||||||
if jsonentry == nil {
|
if jsonentry == nil {
|
||||||
|
// FIXME: shall we return a key not found error?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,13 +285,14 @@ func (db *DB) Get(attr *DbAttr) (*DbEntry, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Del(attr *DbAttr) error {
|
func (db *DB) Del(attr *DbAttr) error {
|
||||||
|
// FIXME: check if it exists prior to just call bucket.Delete()?
|
||||||
if err := db.Open(); err != nil {
|
if err := db.Open(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
err := db.DB.Update(func(tx *bolt.Tx) error {
|
err := db.DB.Update(func(tx *bolt.Tx) error {
|
||||||
bucket := tx.Bucket([]byte(BucketData))
|
bucket := tx.Bucket([]byte(db.Bucket))
|
||||||
|
|
||||||
if bucket == nil {
|
if bucket == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -250,14 +304,14 @@ func (db *DB) Del(attr *DbAttr) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Import(attr *DbAttr) error {
|
func (db *DB) Import(attr *DbAttr) (string, error) {
|
||||||
// open json file into attr.Val
|
// open json file into attr.Val
|
||||||
if err := attr.GetFileValue(); err != nil {
|
if err := attr.GetFileValue(); err != nil {
|
||||||
return err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if attr.Val == "" {
|
if attr.Val == "" {
|
||||||
return errors.New("empty json file")
|
return "", errors.New("empty json file")
|
||||||
}
|
}
|
||||||
|
|
||||||
var entries DbEntries
|
var entries DbEntries
|
||||||
@@ -265,27 +319,27 @@ func (db *DB) Import(attr *DbAttr) error {
|
|||||||
newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05")
|
newfile := db.Dbfile + now.Format("-02.01.2006T03:04.05")
|
||||||
|
|
||||||
if err := json.Unmarshal([]byte(attr.Val), &entries); err != nil {
|
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) {
|
if fileExists(db.Dbfile) {
|
||||||
// backup the old file
|
// backup the old file
|
||||||
err := os.Rename(db.Dbfile, newfile)
|
err := os.Rename(db.Dbfile, newfile)
|
||||||
if err != nil {
|
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
|
// should now be a new db file
|
||||||
if err := db.Open(); err != nil {
|
if err := db.Open(); err != nil {
|
||||||
return cleanError(newfile, err)
|
return "", cleanError(newfile, err)
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
err := db.DB.Update(func(tx *bolt.Tx) error {
|
err := db.DB.Update(func(tx *bolt.Tx) error {
|
||||||
// insert data
|
// insert data
|
||||||
bucket, err := tx.CreateBucketIfNotExists([]byte(BucketData))
|
bucket, err := tx.CreateBucketIfNotExists([]byte(db.Bucket))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create bucket: %w", err)
|
return fmt.Errorf("failed to create bucket: %w", err)
|
||||||
}
|
}
|
||||||
@@ -306,28 +360,53 @@ func (db *DB) Import(attr *DbAttr) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cleanError(newfile, err)
|
return "", cleanError(newfile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("backed up database file to %s\n", newfile)
|
return fmt.Sprintf("backed up database file to %s\nimported %d database entries\n",
|
||||||
fmt.Printf("imported %d database entries\n", len(entries))
|
newfile, len(entries)), nil
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanError(file string, err error) error {
|
func (db *DB) Info() (*DbInfo, error) {
|
||||||
// remove given [backup] file and forward the given error
|
if err := db.Open(); err != nil {
|
||||||
os.Remove(file)
|
return nil, err
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func fileExists(filename string) bool {
|
|
||||||
info, err := os.Stat(filename)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
// return false on any error
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
package app
|
||||||
|
|
||||||
// look if a key in a map exists, generic variant
|
// look if a key in a map exists, generic variant
|
||||||
|
|||||||
36
app/io.go
Normal file
36
app/io.go
Normal 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()
|
||||||
|
}
|
||||||
118
cfg/config.go
118
cfg/config.go
@@ -1,16 +1,114 @@
|
|||||||
|
/*
|
||||||
|
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
|
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.0.5"
|
||||||
|
|
||||||
|
type BucketConfig struct {
|
||||||
|
Encrypt bool
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Debug bool
|
Debug bool
|
||||||
Dbfile string
|
Dbfile string
|
||||||
Mode string // wide, table, yaml, json
|
Dbbucket string
|
||||||
NoHeaders bool
|
Template string
|
||||||
Encrypt bool
|
Mode string // wide, table, yaml, json
|
||||||
DB *app.DB
|
NoHeaders bool
|
||||||
File string
|
NoHumanize bool
|
||||||
Tags []string
|
Encrypt bool // one entry
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
16
cmd/anydb.go
16
cmd/anydb.go
@@ -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
|
package cmd
|
||||||
|
|
||||||
var manpage = `
|
var manpage = `
|
||||||
|
|||||||
@@ -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
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -13,6 +29,7 @@ import (
|
|||||||
"github.com/tlinden/anydb/app"
|
"github.com/tlinden/anydb/app"
|
||||||
"github.com/tlinden/anydb/cfg"
|
"github.com/tlinden/anydb/cfg"
|
||||||
"github.com/tlinden/anydb/output"
|
"github.com/tlinden/anydb/output"
|
||||||
|
"github.com/tlinden/anydb/rest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Set(conf *cfg.Config) *cobra.Command {
|
func Set(conf *cfg.Config) *cobra.Command {
|
||||||
@@ -48,7 +65,7 @@ func Set(conf *cfg.Config) *cobra.Command {
|
|||||||
|
|
||||||
// encrypt?
|
// encrypt?
|
||||||
if conf.Encrypt {
|
if conf.Encrypt {
|
||||||
pass, err := app.AskForPassword()
|
pass, err := getPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -80,7 +97,7 @@ func Get(conf *cfg.Config) *cobra.Command {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cmd = &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",
|
Short: "Retrieve value for a key",
|
||||||
Long: `Retrieve value for a key`,
|
Long: `Retrieve value for a key`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -101,7 +118,7 @@ func Get(conf *cfg.Config) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if entry.Encrypted {
|
if entry.Encrypted {
|
||||||
pass, err := app.AskForPassword()
|
pass, err := getPassword()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -124,9 +141,11 @@ func Get(conf *cfg.Config) *cobra.Command {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().StringVarP(&attr.File, "output", "o", "", "output to file (ignores -m)")
|
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) (default 'simple')")
|
cmd.PersistentFlags().StringVarP(&conf.Mode, "mode", "m", "", "output format (simple|wide|json) (default 'simple')")
|
||||||
cmd.PersistentFlags().BoolVarP(&conf.NoHeaders, "no-headers", "n", false, "omit headers in tables")
|
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, "show")
|
||||||
cmd.Aliases = append(cmd.Aliases, "g")
|
cmd.Aliases = append(cmd.Aliases, "g")
|
||||||
@@ -186,7 +205,7 @@ func Export(conf *cfg.Config) *cobra.Command {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.WriteFile(&attr, conf, entries)
|
return output.WriteJSON(&attr, conf, entries)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,7 +224,7 @@ func List(conf *cfg.Config) *cobra.Command {
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cmd = &cobra.Command{
|
var cmd = &cobra.Command{
|
||||||
Use: "list [-t <tag>] [-o <mode>] [<filter-regex>]",
|
Use: "list [<filter-regex>] [-t <tag>] [-m <mode>] [-n -N] [-T <tpl>]",
|
||||||
Short: "List database contents",
|
Short: "List database contents",
|
||||||
Long: `List database contents`,
|
Long: `List database contents`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -235,8 +254,10 @@ 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), 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(&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.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().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
cmd.PersistentFlags().StringArrayVarP(&attr.Tags, "tags", "t", nil, "tags, multiple allowed")
|
||||||
|
|
||||||
cmd.Aliases = append(cmd.Aliases, "/")
|
cmd.Aliases = append(cmd.Aliases, "/")
|
||||||
@@ -258,7 +279,13 @@ func Import(conf *cfg.Config) *cobra.Command {
|
|||||||
// errors at this stage do not cause the usage to be shown
|
// errors at this stage do not cause the usage to be shown
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
|
|
||||||
return conf.DB.Import(&attr)
|
out, err := conf.DB.Import(&attr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print(out)
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,3 +334,63 @@ func Man(conf *cfg.Config) *cobra.Command {
|
|||||||
|
|
||||||
return cmd
|
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 getPassword() ([]byte, error) {
|
||||||
|
var pass []byte
|
||||||
|
|
||||||
|
envpass := os.Getenv("ANYDB_PASSWORD")
|
||||||
|
|
||||||
|
if envpass == "" {
|
||||||
|
readpass, err := app.AskForPassword()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pass = readpass
|
||||||
|
} else {
|
||||||
|
pass = []byte(envpass)
|
||||||
|
}
|
||||||
|
|
||||||
|
return pass, nil
|
||||||
|
}
|
||||||
56
cmd/root.go
56
cmd/root.go
@@ -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
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -6,6 +22,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/alecthomas/repr"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/tlinden/anydb/app"
|
"github.com/tlinden/anydb/app"
|
||||||
"github.com/tlinden/anydb/cfg"
|
"github.com/tlinden/anydb/cfg"
|
||||||
@@ -29,22 +46,49 @@ func completion(cmd *cobra.Command, mode string) error {
|
|||||||
func Execute() {
|
func Execute() {
|
||||||
var (
|
var (
|
||||||
conf cfg.Config
|
conf cfg.Config
|
||||||
|
configfile string
|
||||||
ShowVersion bool
|
ShowVersion bool
|
||||||
ShowCompletion string
|
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{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "anydb <command> [options]",
|
Use: "anydb <command> [options]",
|
||||||
Short: "anydb",
|
Short: "anydb",
|
||||||
Long: `A personal key value store`,
|
Long: `A personal key value store`,
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.DB = db
|
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
|
return nil
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -66,16 +110,14 @@ func Execute() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
home, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// options
|
// options
|
||||||
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "v", false, "Print program version")
|
rootCmd.PersistentFlags().BoolVarP(&ShowVersion, "version", "v", false, "Print program version")
|
||||||
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
|
rootCmd.PersistentFlags().BoolVarP(&conf.Debug, "debug", "d", false, "Enable debugging")
|
||||||
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f",
|
rootCmd.PersistentFlags().StringVarP(&conf.Dbfile, "dbfile", "f",
|
||||||
filepath.Join(home, ".config", "anydb", "default.db"), "DB file to use")
|
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")
|
||||||
|
|
||||||
rootCmd.AddCommand(Set(&conf))
|
rootCmd.AddCommand(Set(&conf))
|
||||||
rootCmd.AddCommand(List(&conf))
|
rootCmd.AddCommand(List(&conf))
|
||||||
@@ -83,7 +125,9 @@ func Execute() {
|
|||||||
rootCmd.AddCommand(Del(&conf))
|
rootCmd.AddCommand(Del(&conf))
|
||||||
rootCmd.AddCommand(Export(&conf))
|
rootCmd.AddCommand(Export(&conf))
|
||||||
rootCmd.AddCommand(Import(&conf))
|
rootCmd.AddCommand(Import(&conf))
|
||||||
|
rootCmd.AddCommand(Serve(&conf))
|
||||||
rootCmd.AddCommand(Man(&conf))
|
rootCmd.AddCommand(Man(&conf))
|
||||||
|
rootCmd.AddCommand(Info(&conf))
|
||||||
|
|
||||||
err = rootCmd.Execute()
|
err = rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
36
common/io.go
Normal file
36
common/io.go
Normal 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()
|
||||||
|
}
|
||||||
@@ -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"
|
version: "3.9"
|
||||||
services:
|
services:
|
||||||
init:
|
init:
|
||||||
|
|||||||
14
example.toml
Normal file
14
example.toml
Normal 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
|
||||||
15
go.mod
15
go.mod
@@ -4,13 +4,26 @@ go 1.22.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/alecthomas/repr v0.4.0 // indirect
|
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/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/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/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/rogpeppe/go-internal v1.13.1 // indirect
|
||||||
github.com/spf13/cobra v1.8.1 // indirect
|
github.com/spf13/cobra v1.8.1 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // 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
|
go.etcd.io/bbolt v1.3.11 // indirect
|
||||||
golang.org/x/crypto v0.31.0 // indirect
|
golang.org/x/crypto v0.31.0 // indirect
|
||||||
golang.org/x/sys v0.28.0 // indirect
|
golang.org/x/sys v0.28.0 // indirect
|
||||||
|
|||||||
39
go.sum
39
go.sum
@@ -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/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 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
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 h1:I5AqhkPK6nBZ/qJXySdI7ot5BlXSZ7qvDY1zAn5ZJac=
|
||||||
github.com/asdine/storm/v3 v3.2.1/go.mod h1:LEpXwGt4pIqrE/XcTvCnZHT5MgZCV6Ub9q7yQzOFWr0=
|
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/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/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 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
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.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/golang/protobuf v1.3.2/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/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 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
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/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
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/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 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.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 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
||||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
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/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 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
|
||||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
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=
|
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 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
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/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=
|
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.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
|
||||||
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
|
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/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-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-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 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
|
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
|
||||||
|
|||||||
16
main.go
16
main.go
@@ -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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
16
main_test.go
16
main_test.go
@@ -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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
2
mkrel.sh
2
mkrel.sh
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Copyright © 2023 Thomas von Dein
|
# Copyright © 2024 Thomas von Dein
|
||||||
|
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# 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
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
|||||||
50
output/export.go
Normal file
50
output/export.go
Normal 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
|
||||||
|
}
|
||||||
@@ -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
|
package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
tpl "text/template"
|
||||||
|
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/olekukonko/tablewriter"
|
"github.com/olekukonko/tablewriter"
|
||||||
@@ -16,14 +35,12 @@ import (
|
|||||||
func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
func List(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||||
// FIXME: call sort here
|
// FIXME: call sort here
|
||||||
switch conf.Mode {
|
switch conf.Mode {
|
||||||
case "wide":
|
case "wide", "", "table":
|
||||||
fallthrough
|
|
||||||
case "":
|
|
||||||
fallthrough
|
|
||||||
case "table":
|
|
||||||
return ListTable(writer, conf, entries)
|
return ListTable(writer, conf, entries)
|
||||||
case "json":
|
case "json":
|
||||||
return ListJson(writer, conf, entries)
|
return ListJson(writer, conf, entries)
|
||||||
|
case "template":
|
||||||
|
return ListTemplate(writer, conf, entries)
|
||||||
default:
|
default:
|
||||||
return errors.New("unsupported mode")
|
return errors.New("unsupported mode")
|
||||||
}
|
}
|
||||||
@@ -39,43 +56,67 @@ func ListJson(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
|||||||
return nil
|
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 {
|
||||||
|
row.Normalize()
|
||||||
|
|
||||||
|
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 {
|
func ListTable(writer io.Writer, conf *cfg.Config, entries app.DbEntries) error {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
if !conf.NoHeaders {
|
if !conf.NoHeaders {
|
||||||
if conf.Mode == "wide" {
|
if conf.Mode == "wide" {
|
||||||
table.SetHeader([]string{"KEY", "TAGS", "SIZE", "AGE", "VALUE"})
|
table.SetHeader([]string{"KEY", "TAGS", "SIZE", "UPDATED", "VALUE"})
|
||||||
} else {
|
} else {
|
||||||
table.SetHeader([]string{"KEY", "VALUE"})
|
table.SetHeader([]string{"KEY", "VALUE"})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, row := range entries {
|
for _, row := range entries {
|
||||||
size := len(row.Value)
|
row.Normalize()
|
||||||
|
|
||||||
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" {
|
if conf.Mode == "wide" {
|
||||||
table.Append([]string{
|
switch conf.NoHumanize {
|
||||||
row.Key,
|
case true:
|
||||||
strings.Join(row.Tags, ","),
|
table.Append([]string{
|
||||||
humanize.Bytes(uint64(size)),
|
row.Key,
|
||||||
//row.Created.Format("02.01.2006T03:04.05"),
|
strings.Join(row.Tags, ","),
|
||||||
humanize.Time(row.Created),
|
strconv.Itoa(row.Size),
|
||||||
row.Value,
|
row.Created.Format("02.01.2006T03:04.05"),
|
||||||
})
|
row.Value,
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
table.Append([]string{
|
||||||
|
row.Key,
|
||||||
|
strings.Join(row.Tags, ","),
|
||||||
|
humanize.Bytes(uint64(row.Size)),
|
||||||
|
//row.Created.Format("02.01.2006T03:04.05"),
|
||||||
|
humanize.Time(row.Created),
|
||||||
|
row.Value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
table.Append([]string{row.Key, row.Value})
|
table.Append([]string{row.Key, row.Value})
|
||||||
}
|
}
|
||||||
|
|||||||
110
output/single.go
110
output/single.go
@@ -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
|
package output
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -5,47 +21,25 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/tlinden/anydb/app"
|
"github.com/tlinden/anydb/app"
|
||||||
"github.com/tlinden/anydb/cfg"
|
"github.com/tlinden/anydb/cfg"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
//"github.com/alecthomas/repr"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
|
func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
|
||||||
if attr.File != "" {
|
if attr.File != "" {
|
||||||
fd, err := os.OpenFile(attr.File, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
|
return WriteFile(writer, conf, attr, entry)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
isatty := term.IsTerminal(int(os.Stdout.Fd()))
|
||||||
|
|
||||||
switch conf.Mode {
|
switch conf.Mode {
|
||||||
case "simple":
|
case "simple", "":
|
||||||
fallthrough
|
|
||||||
case "":
|
|
||||||
if len(entry.Bin) > 0 {
|
if len(entry.Bin) > 0 {
|
||||||
if isatty {
|
if isatty {
|
||||||
fmt.Println("binary data omitted")
|
fmt.Println("binary data omitted")
|
||||||
@@ -69,7 +63,69 @@ func Print(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEn
|
|||||||
fmt.Println(string(jsonentry))
|
fmt.Println(string(jsonentry))
|
||||||
case "wide":
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WriteFile(writer io.Writer, conf *cfg.Config, attr *app.DbAttr, entry *app.DbEntry) error {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
}
|
|
||||||
141
rest/handlers.go
Normal file
141
rest/handlers.go
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
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)
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
113
rest/serve.go
Normal file
113
rest/serve.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
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.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,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -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
|
# simple file, we cannot use redirection here, so dd is our friend
|
||||||
exec dd if=/dev/random of=file.txt count=5 bs=10
|
exec dd if=/dev/random of=file.txt count=5 bs=10
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
# check default outputs
|
||||||
|
|
||||||
exec anydb -v
|
exec anydb -v
|
||||||
|
|||||||
@@ -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
|
# setup simple db
|
||||||
exec anydb -f test.db set foo bar
|
exec anydb -f test.db set foo bar
|
||||||
|
|
||||||
|
|||||||
@@ -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 entry
|
# simple entry
|
||||||
exec anydb -f test.db set foo bar
|
exec anydb -f test.db set foo bar
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user