Compare commits
14 Commits
1580865b56
...
main
Author | SHA1 | Date | |
---|---|---|---|
0255031150
|
|||
55388fea39
|
|||
27f6db5410
|
|||
1458620bee
|
|||
2dbe0b08c4
|
|||
d95aa517df
|
|||
66a7c0e3e7
|
|||
69951be543
|
|||
b483c72df7
|
|||
e00766cce8
|
|||
76b490bc83
|
|||
d77349ce50
|
|||
741f349028
|
|||
c3acf30c66
|
152
tag.sh
152
tag.sh
@ -1,27 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
set -ueo pipefail
|
||||
[ "${DEBUG:-0}" = "1" ] && set -x
|
||||
# usage:
|
||||
# - init - create an empty database
|
||||
# - import - import $FILE or import $DIR, both can be multiple values
|
||||
# - autoimport ($REGEX|video) $FOLDER - will import all files in $FOLDER with a $REGEX filter,
|
||||
# or one of the preset filters:
|
||||
# - video
|
||||
# - 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
|
||||
: <<'DOCS'
|
||||
=head1 NAME
|
||||
|
||||
# TODO: adjust sqlite_insert_multiple to allow defining columns per insert, currently it's hardcoded to 2
|
||||
# TODO: merge sqlite_insert_single and sqlite_insert_multiple probably as result of the above
|
||||
tags is a tool for keeping file tags in a SQLite database.
|
||||
|
||||
SCRIPT_DIR=$(dirname $(readlink "$0"))
|
||||
=head1 SYNOPSIS
|
||||
|
||||
tag [OPTIONS] <init|import|add|list|bytag|listtags>
|
||||
|
||||
=head1 OPTIONS
|
||||
|
||||
=over 4
|
||||
|
||||
=item B<init>
|
||||
|
||||
Create an empty database.
|
||||
|
||||
=item B<import> I<filename>
|
||||
|
||||
Import filename, can be single value or multiple values.
|
||||
|
||||
=item B<autoimport> I<regexp> I<path>
|
||||
|
||||
Import all files in I<path>. Can be filtered using custom I<regexp>, or one of the regular expression presets (currently only "video").
|
||||
|
||||
=item B<add> I<type> I<value>
|
||||
|
||||
Adds a new item of the specified I<type>: C<tag>, C<file>, C<hash>.
|
||||
|
||||
=item B<list> I<type> I<[--bytag|--byfile]>
|
||||
|
||||
List C<tags>, C<files>, C<hashes> (these are all table names, internally).
|
||||
|
||||
=item B<tag> I<[-i|--interactive]> I<--file|--id> I<tag>
|
||||
|
||||
Tags I<filename> with one or more I<tag>.
|
||||
|
||||
=back
|
||||
|
||||
=head1 TODO
|
||||
|
||||
* adjust sqlite_insert_multiple to allow defining columns per insert, currently it's hardcoded to 2
|
||||
* merge sqlite_insert_single and sqlite_insert_multiple probably as result of the above
|
||||
* listtags doesn't work with full path
|
||||
* listtags doesn't work after tagging a file
|
||||
|
||||
=head1 LICENSE AND COPYRIGHT
|
||||
|
||||
=cut
|
||||
DOCS
|
||||
|
||||
SCRIPT_DIR=$(dirname "$(readlink "$0")")
|
||||
SCRIPT_NAME=$(basename "$0")
|
||||
|
||||
|
||||
DB_FILE="${PWD}/tags.db"
|
||||
[ "${1:-}" = "--db" ] && DB_FILE="$2" && shift 2
|
||||
[ "${1:-}" = "--db" ] && DB_FILE=$(readlink -f "${2/#~/$HOME}") && shift 2
|
||||
|
||||
|
||||
DB_SCHEMA="${SCRIPT_DIR}/database.sql"
|
||||
@ -41,7 +77,7 @@ log() {
|
||||
init() {
|
||||
# DB_NAME
|
||||
[ -f "$1" ] && fail "Database file \"$1\" already exists. Aborting."
|
||||
cat "$DB_SCHEMA" | sqlite3 "$1"
|
||||
sqlite3 "$1" < "$DB_SCHEMA"
|
||||
log "Empty database file \"$1\" created."
|
||||
}
|
||||
|
||||
@ -80,7 +116,8 @@ add() {
|
||||
[ "$1" = "path" ] && shift && add_path "$@" && exit 0
|
||||
[ "$1" = "hash" ] && shift && add_hash "$@" && exit 0
|
||||
[ "$1" = "file" ] && fail "Use \"$SCRIPT_NAME add path\" instead."
|
||||
fail "Usage: $SCRIPT_NAME add tag/path/hash"
|
||||
pod2usage "$0"
|
||||
exit 1
|
||||
}
|
||||
|
||||
sqlite_query() {
|
||||
@ -118,11 +155,11 @@ sqlite_insert_single() {
|
||||
|
||||
sqlite_insert_multi() {
|
||||
# $TABLE $COLUMN $VALUE1 $VALUE2
|
||||
[ -z "$1" ] && fail "No table specified."
|
||||
[ -z "${1:-}" ] && fail "No table specified."
|
||||
TABLE="$1"
|
||||
[ -z "$2" ] && fail "No column(s) supplied."
|
||||
[ -z "${2:-}" ] && { pod2usage "$0"; exit 1 ; }
|
||||
COLUMN="$2"
|
||||
[[ -z "$3" || -z "$4" ]] && fail "No column values supplied."
|
||||
[[ -z "${3:-}" || -z "${4:-}" ]] && { pod2usage "$0"; exit 1 ; }
|
||||
shift 2
|
||||
VALUES="($1,$2)"
|
||||
if [[ ! -z "${3:-}" && ! -z "${4:-}" ]]; then
|
||||
@ -150,34 +187,36 @@ add_path() {
|
||||
COLUMN="filename,path"
|
||||
FILES=()
|
||||
for FILE in "$@"; do
|
||||
[ ! -f "$FILE" ] && fail "File '$FILE' does not exist in the specified path."
|
||||
local FILENAME=$(basename "$FILE")
|
||||
local FILEPATH=$(realpath $(dirname "$FILE"))
|
||||
local FILEPATH=$(readlink -f "$(dirname "$FILE")")
|
||||
file_exists_in_db "$FILE" && fail "File '$FILE' already exists in database."
|
||||
FILES+=("'$FILENAME'")
|
||||
FILES+=("'$FILEPATH'")
|
||||
FILES+=("\"$FILENAME\"")
|
||||
FILES+=("\"$FILEPATH\"")
|
||||
done
|
||||
sqlite_insert_multi "$TABLE" "$COLUMN" "${FILES[@]}"
|
||||
}
|
||||
|
||||
add_hash() {
|
||||
# $FILE $HASH
|
||||
TABLE="hashes"
|
||||
COLUMN="fid,md5"
|
||||
local TABLE="hashes"
|
||||
local COLUMN="fid,md5"
|
||||
local FILENAME="${1:-}"
|
||||
[ -z "$FILENAME" ] && fail "No file specified."
|
||||
! file_exists_in_db "$FILENAME" && fail "File \"$FILENAME\" does not exist in database."
|
||||
sqlite_insert_multi "$TABLE" "$COLUMN" $RESULT "'$2'"
|
||||
local FID=$(id_by_filename "$FILENAME")
|
||||
[[ "$FID" -eq 0 ]] && fail "File \"$FILENAME\" does not exist in database."
|
||||
sqlite_insert_multi "$TABLE" "$COLUMN" $FID "'$2'"
|
||||
}
|
||||
|
||||
add_path_auto() {
|
||||
# $REGEX $FOLDER
|
||||
find "${2}" -type f -regextype posix-extended -iregex "$1" -exec "$0" import "{}" +
|
||||
find "${2}" -type f -regextype posix-extended -iregex "$1" -exec "$0" --db "$DB_FILE" import "{}" +
|
||||
}
|
||||
|
||||
tag_exists_in_db() {
|
||||
# $TAGLABEL
|
||||
TAG=${1:-}
|
||||
RESULT=$(sqlite_query "SELECT id FROM tags WHERE label = '$TAG'")
|
||||
RESULT=$(sqlite_query "SELECT id FROM tags WHERE label = \"$TAG\"")
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
return 1
|
||||
else
|
||||
@ -187,16 +226,23 @@ tag_exists_in_db() {
|
||||
|
||||
file_exists_in_db() {
|
||||
# $FILENAME
|
||||
local FILENAME=$(basename "${1:-}")
|
||||
local FILEPATH=$(dirname "${1:-}")
|
||||
local RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = '$FILENAME' AND path = '$FILEPATH'")
|
||||
if [[ -z "$RESULT" ]]; then
|
||||
local RESULT=$(id_by_filename "$1")
|
||||
if [[ "$RESULT" -eq 0 ]]; then
|
||||
return 1
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
id_by_filename() {
|
||||
# FILENAME
|
||||
local FILENAME=$(basename "${1:-}")
|
||||
local FILEPATH=$(readlink -f "$(dirname "${1:-}")")
|
||||
local RESULT=0
|
||||
RESULT=$(sqlite_query "SELECT id FROM files WHERE filename = \"$FILENAME\" AND path = \"$FILEPATH\"")
|
||||
echo $RESULT
|
||||
}
|
||||
|
||||
dbfile_exists() {
|
||||
if [[ ! -f "$DB_FILE" ]]; then
|
||||
return 1
|
||||
@ -207,13 +253,15 @@ dbfile_exists() {
|
||||
|
||||
main() {
|
||||
|
||||
[ -z "${1:-}" ] && fail "Usage: tag init/add/import/autoimport/list/listtags"
|
||||
[ -z "${1:-}" ] && { pod2usage "$0"; exit 1; }
|
||||
|
||||
if [[ "$1" = "init" ]]; then
|
||||
init "${2:-$DB_FILE}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
[ ! -f "$DB_FILE" ] && fail "Database file \"$DB_FILE\" does not exist."
|
||||
|
||||
if [[ "$1" = "import" ]]; then
|
||||
shift
|
||||
add "path" "$@"
|
||||
@ -234,7 +282,7 @@ main() {
|
||||
fi
|
||||
|
||||
if [[ "$1" = "list" ]]; then
|
||||
[ -z "$2" ] && fail "No table supplied."
|
||||
[ -z "${2:-}" ] && { pod2usage "$0"; exit 1 ; }
|
||||
TABLE_NAME="$2"
|
||||
sqlite_query "SELECT * FROM \"${TABLE_NAME}\""
|
||||
exit 0
|
||||
@ -249,6 +297,12 @@ main() {
|
||||
|
||||
if [[ "$1" = "tag" ]]; then
|
||||
shift
|
||||
if [[ "${1:-}" = "-i" || "${1:-}" = "--interactive" ]]; then
|
||||
"$0" --db "$DB_FILE" tagfid \
|
||||
$(sqlite_query "SELECT id,filename,path FROM files" | fzf | xargs -I{} echo '{}' | awk -F'|' '{print $1}') \
|
||||
$(sqlite_query "SELECT label FROM tags" | fzf --multi | xargs -I{} echo '{}' | awk -F'|' '{print $1}')
|
||||
exit 0
|
||||
fi
|
||||
[ -z "${1:-}" ] && fail "No filename supplied."
|
||||
FILENAME="$1"
|
||||
! file_exists_in_db "$FILENAME" && fail "File '$FILENAME' does not exist in database."
|
||||
@ -259,7 +313,8 @@ main() {
|
||||
LABEL="$1"
|
||||
! tag_exists_in_db "$LABEL" && fail "Tag '$TAG' does not exist in database."
|
||||
shift
|
||||
sqlite_query "INSERT INTO tags_ties (fid, tid) VALUES ((SELECT id FROM files WHERE filename = \"$FILENAME\"),(SELECT id FROM tags WHERE label = \"$LABEL\"))"
|
||||
local FID=$(id_by_filename "$FILENAME")
|
||||
sqlite_query "INSERT INTO tags_ties (fid, tid) VALUES ($FID,(SELECT id FROM tags WHERE label = \"$LABEL\"))"
|
||||
COUNTER=$((COUNTER++))
|
||||
[ -z "${1:-}" ] && break
|
||||
done
|
||||
@ -267,11 +322,34 @@ main() {
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$1" = "tagfid" ]]; then
|
||||
shift
|
||||
[ -z "${1:-}" ] && fail "No file ID supplied."
|
||||
local FID="$1"
|
||||
shift
|
||||
[ -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
|
||||
sqlite_query "INSERT INTO tags_ties (fid, tid) VALUES ($FID,(SELECT id FROM tags WHERE label = \"$LABEL\"))"
|
||||
COUNTER=$((COUNTER++))
|
||||
[ -z "${1:-}" ] && break
|
||||
done
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$1" = "listtags" ]]; then
|
||||
[ -z "${2:-}" ] && fail "Usage: $SCRIPT_NAME listtags filename"
|
||||
[ -z "${2:-}" ] && { pod2usage "$0"; exit 1 ; }
|
||||
listtags "$2"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [[ "$1" = "listbyfile" ]]; then
|
||||
sqlite_query "SELECT id,filename FROM files" |\
|
||||
fzf --delimiter="|" --preview "sqlite3 $DB_FILE 'SELECT label FROM tags WHERE id IN (SELECT tid FROM tags_ties WHERE fid ={2})'"
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
@ -15,6 +15,7 @@ oneTimeTearDown() {
|
||||
# runs one-time teardown two times
|
||||
# see https://github.com/kward/shunit2/issues/112
|
||||
[ -f "$DB_FILE" ] && rm "$DB_FILE"
|
||||
[ -f "$FILENAME" ] && rm "$FILENAME"
|
||||
}
|
||||
|
||||
testDbCreated() {
|
||||
@ -24,26 +25,49 @@ testDbCreated() {
|
||||
testTagAdded() {
|
||||
tag --db "$DB_FILE" add tag hello >/dev/null
|
||||
RESULT=$(sqlite3 "$DB_FILE" "SELECT label from tags WHERE label = 'hello'")
|
||||
assertEquals $RESULT "hello"
|
||||
assertEquals "hello" "$RESULT"
|
||||
}
|
||||
|
||||
testFailAddingNonexistentFile() {
|
||||
tag --db "$DB_FILE" import "$FILENAME" 2>/dev/null
|
||||
assertEquals 1 "$?"
|
||||
}
|
||||
|
||||
testFilenameAdded() {
|
||||
touch "$FILENAME"
|
||||
tag --db "$DB_FILE" add path "$FILENAME" >/dev/null
|
||||
RESULT=$(sqlite3 "$DB_FILE" "SELECT filename from files WHERE filename = '$FILENAME'")
|
||||
assertEquals "$RESULT" "$FILENAME"
|
||||
assertEquals "$FILENAME" "$RESULT"
|
||||
}
|
||||
|
||||
testHashAdded() {
|
||||
local HASH="fbe2153ce0614d76a378b2e6fe07cc9e"
|
||||
tag --db "$DB_FILE" add hash "$FILENAME" "$HASH" >/dev/null
|
||||
RESULT=$(sqlite3 "$DB_FILE" "SELECT md5 from hashes WHERE md5 = '$HASH'")
|
||||
assertEquals "$RESULT" "$HASH"
|
||||
assertEquals "$HASH" "$RESULT"
|
||||
}
|
||||
|
||||
testFailAddingHashOnNonexistentFile() {
|
||||
local HASH="fbe2153ce0614d76a378b2e6fe07cc9e"
|
||||
tag --db "$DB_FILE" add hash "nonexistentfile" "$HASH" 2>/dev/null
|
||||
assertEquals "$?" 1
|
||||
assertEquals 1 "$?"
|
||||
}
|
||||
|
||||
testTagFile() {
|
||||
tag --db "$DB_FILE" tag "$FILENAME" hello >/dev/null
|
||||
RESULT=$(sqlite3 "$DB_FILE" "SELECT label FROM tags WHERE id = (SELECT tid from tags_ties WHERE fid = (SELECT id FROM files WHERE filename = '$FILENAME'))")
|
||||
assertEquals "hello" "$RESULT"
|
||||
}
|
||||
|
||||
testListingTags() {
|
||||
tag --db "$DB_FILE" listtags "$FILENAME" | grep -q "^$FILENAME|hello$"
|
||||
assertEquals "$?" 0
|
||||
}
|
||||
|
||||
testAddingMultipleTags() {
|
||||
tag --db "$DB_FILE" add tag more more2 >/dev/null
|
||||
RESULT=$(sqlite3 -list "$DB_FILE" "SELECT COUNT(*) from tags WHERE label IN ('hello', 'more', 'more2')")
|
||||
assertEquals 3 "$RESULT"
|
||||
}
|
||||
|
||||
. ../shunit2/shunit2
|
Reference in New Issue
Block a user