94 lines
2.5 KiB
Bash
Executable File
94 lines
2.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -ueo pipefail
|
|
# usage:
|
|
# - 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
|
|
# - tag $FILENAME $TAG - tag $FILENAME with $TAG, $TAG can be repeated
|
|
# - listtags $FILENAME - list all tags for $FILENAME
|
|
|
|
DB_FILE="tags.db"
|
|
|
|
fail() {
|
|
echo "$1" >&2;
|
|
exit 1
|
|
}
|
|
|
|
listtags() {
|
|
# $FILENAME $LIMIT
|
|
[ -z "$1" ] && fail "No filename supplied."
|
|
FILENAME="$1"
|
|
shift
|
|
LIMIT="${1:-0}"
|
|
|
|
ADDITIONAL_QUERY=""
|
|
[ $LIMIT -gt 0 ] && ADDITIONAL_QUERY="LIMIT $LIMIT"
|
|
|
|
sqlite3 -table "$DB_FILE" \
|
|
"SELECT filename, label from files \
|
|
INNER JOIN tags_ties ON tags_ties.fid = files.fid \
|
|
INNER JOIN tags ON tags.tid = tags_ties.tid \
|
|
WHERE filename = \"$FILENAME\"\
|
|
$ADDITIONAL_QUERY"
|
|
}
|
|
|
|
if [[ "$1" = "add" ]]; then
|
|
shift
|
|
[ "$1" = "tag" ] && TABLE="tags" && COLUMN="label"
|
|
[ "$1" = "file" ] && TABLE="files" && COLUMN="filename"
|
|
# TODO: refactor the add function to allow adding hashes
|
|
# [ "$1" = "hash" ] && TABLE="hashes" && COLUMN="md5,filename"
|
|
shift
|
|
[ -z "$1" ] && fail "No value supplied."
|
|
COUNTER=0
|
|
while true; do
|
|
NEW_TAG="$1"
|
|
shift
|
|
sqlite3 -table "$DB_FILE" "INSERT INTO $TABLE ($COLUMN) VALUES (\"${NEW_TAG}\")"
|
|
COUNTER=$((COUNTER++))
|
|
[ -z "${1:-}" ] && break
|
|
done
|
|
sqlite3 -table "$DB_FILE" "SELECT * FROM \"$TABLE\" LIMIT $COUNTER"
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "$1" = "list" ]]; then
|
|
[ -z "$2" ] && fail "No table supplied."
|
|
TABLE_NAME="$2"
|
|
sqlite3 -table "$DB_FILE" "SELECT * FROM \"${TABLE_NAME}\""
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "$1" = "bytag" ]]; then
|
|
[ -z "$2" ] && fail "No tag supplied."
|
|
TAG_NAME="$2"
|
|
sqlite3 -table "$DB_FILE" "SELECT filename FROM files WHERE fid = (SELECT fid FROM tags_ties WHERE tid = (SELECT tid FROM tags WHERE label = \"${TAG_NAME}\"))"
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "$1" = "tag" ]]; then
|
|
shift
|
|
[ -z "$1" ] && fail "No filename supplied."
|
|
FILENAME="$1"
|
|
shift
|
|
[ -z "$1" ] && fail "No tag supplied."
|
|
COUNTER=0
|
|
while true; do
|
|
LABEL="$1"
|
|
shift
|
|
sqlite3 -table "$DB_FILE" "INSERT INTO tags_ties (fid, tid) VALUES ((SELECT fid FROM files WHERE filename = \"$FILENAME\"),(SELECT tid FROM tags WHERE label = \"$LABEL\"))"
|
|
COUNTER=$((COUNTER++))
|
|
[ -z "${1:-}" ] && break
|
|
done
|
|
listtags "$FILENAME" "$COUNTER"
|
|
exit 0
|
|
fi
|
|
|
|
if [[ "$1" = "listtags" ]]; then
|
|
listtags "$2"
|
|
exit 0
|
|
fi
|
|
|
|
|
|
|