Add init function

This commit is contained in:
Lukáš Kucharczyk 2022-11-12 12:46:34 +01:00
parent abe44c3f92
commit b044726c96
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
1 changed files with 13 additions and 0 deletions

13
tag.sh
View File

@ -1,6 +1,7 @@
#!/usr/bin/env bash
set -ueo pipefail
# usage:
# - init - create an empty database
# - add (tag|filename) $VALUE - adds a new tag or filename of $VALUE
# - list (tags|files) - lists all the values in a table
# - bytag $TAG - list all files by tag label
@ -8,12 +9,17 @@ set -ueo pipefail
# - listtags $FILENAME - list all tags for $FILENAME
DB_FILE="tags.db"
DB_SCHEMA="database.sql"
fail() {
echo "$1" >&2;
exit 1
}
log() {
echo "$1"
}
listtags() {
# $FILENAME $LIMIT
[ -z "$1" ] && fail "No filename supplied."
@ -32,6 +38,13 @@ listtags() {
$ADDITIONAL_QUERY"
}
if [[ "$1" = "init" ]]; then
[ -f "$DB_FILE" ] && fail "Database file \"$DB_FILE\" already exists. Aborting."
cat "$DB_SCHEMA" | sqlite3 "$DB_FILE"
log "Empty database file \"$DB_FILE\" created."
exit 0
fi
if [[ "$1" = "add" ]]; then
shift
[ "$1" = "tag" ] && TABLE="tags" && COLUMN="label"