Compare commits
2 Commits
473d88b3c9
...
f43c1d433a
Author | SHA1 | Date |
---|---|---|
Lukáš Kucharczyk | f43c1d433a | |
Lukáš Kucharczyk | d7f4a9c299 |
39
tag.sh
39
tag.sh
|
@ -13,7 +13,6 @@ set -ueo pipefail
|
|||
# - tag $FILENAME $TAG - tag $FILENAME with $TAG, $TAG can be repeated
|
||||
# - listtags $FILENAME - list all tags for $FILENAME
|
||||
|
||||
# TODO: when tagging, check if tags exist
|
||||
# TODO: when tagging, allow more than one tag
|
||||
# FIXME: adding tags doesn't work
|
||||
|
||||
|
@ -60,8 +59,10 @@ listtags() {
|
|||
[ -z "$1" ] && fail "No filename supplied."
|
||||
FILENAME="$1"
|
||||
shift
|
||||
LIMIT="${1:-0}"
|
||||
|
||||
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
|
||||
|
||||
LIMIT="${1:-0}"
|
||||
ADDITIONAL_QUERY=""
|
||||
[ $LIMIT -gt 0 ] && ADDITIONAL_QUERY="LIMIT $LIMIT"
|
||||
|
||||
|
@ -146,6 +147,9 @@ add_path() {
|
|||
# $PATH1..$PATHN
|
||||
TABLE="files"
|
||||
COLUMN="filename"
|
||||
for FILE in "$@"; do
|
||||
file_exists_in_db "$FILE" && fail "File '$FILENAME' already exists in database."
|
||||
done
|
||||
sqlite_insert_single "$TABLE" "$COLUMN" "$@"
|
||||
}
|
||||
|
||||
|
@ -155,8 +159,7 @@ add_hash() {
|
|||
COLUMN="fid,md5"
|
||||
local FILENAME="${1:-}"
|
||||
[ -z "$FILENAME" ] && fail "No file specified."
|
||||
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
|
||||
[ -z "$RESULT" ] && fail "File \"$FILENAME\" does not exist in database."
|
||||
! file_exists_in_db "$FILENAME" && fail "File \"$FILENAME\" does not exist in database."
|
||||
sqlite_insert_multi "$TABLE" "$COLUMN" $RESULT "'$2'"
|
||||
}
|
||||
|
||||
|
@ -165,6 +168,28 @@ add_path_auto() {
|
|||
find "${2}" -type f -regextype posix-extended -iregex "$1" -exec "$0" import "{}" +
|
||||
}
|
||||
|
||||
tag_exists_in_db() {
|
||||
# $TAGLABEL
|
||||
TAG=${1:-}
|
||||
RESULT=$(sqlite_query "SELECT id FROM tags WHERE label = '$TAG'")
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
file_exists_in_db() {
|
||||
# $FILENAME
|
||||
FILENAME=${1:-}
|
||||
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME'")
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
main() {
|
||||
|
||||
[ -z "${1:-}" ] && fail "Usage: tag init/add/import/autoimport/list/listtags"
|
||||
|
@ -209,13 +234,15 @@ main() {
|
|||
|
||||
if [[ "$1" = "tag" ]]; then
|
||||
shift
|
||||
[ -z "$1" ] && fail "No filename supplied."
|
||||
[ -z "${1:-}" ] && fail "No filename supplied."
|
||||
FILENAME="$1"
|
||||
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
|
||||
shift
|
||||
[ -z "$1" ] && fail "No tag supplied."
|
||||
[ -z "${1:-}" ] && fail "No tag supplied."
|
||||
COUNTER=0
|
||||
while true; do
|
||||
LABEL="$1"
|
||||
! tag_exists_in_db "$LABEL" && fail "Tag '$TAG' does not exist in database."
|
||||
shift
|
||||
sqlite3 -table "$DB_FILE" "INSERT INTO tags_ties (id, id) VALUES ((SELECT id FROM files WHERE filename = \"$FILENAME\"),(SELECT id FROM tags WHERE label = \"$LABEL\"))"
|
||||
COUNTER=$((COUNTER++))
|
||||
|
|
Loading…
Reference in New Issue