From c1564178fa533ad393b8a05f25f0c1b3ae756fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 11 Nov 2022 12:58:15 +0100 Subject: [PATCH] Remove hardcoded db filename Also prevent db files from getting tracked by git --- .gitignore | 1 + tag.sh | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3997bea --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.db \ No newline at end of file diff --git a/tag.sh b/tag.sh index 617d0b8..ef47db5 100755 --- a/tag.sh +++ b/tag.sh @@ -7,6 +7,7 @@ set -ueo pipefail # - 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; @@ -16,7 +17,7 @@ fail() { listtags() { [ -z "$1" ] && fail "No filename supplied." FILENAME="$1" - sqlite3 -table tags.db "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\"" + 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\"" } if [[ "$1" = "add" ]]; then @@ -29,25 +30,25 @@ if [[ "$1" = "add" ]]; then while true; do NEW_TAG="$1" shift - sqlite3 -table tags.db "INSERT INTO $TABLE ($COLUMN) VALUES (\"${NEW_TAG}\")" + sqlite3 -table "$DB_FILE" "INSERT INTO $TABLE ($COLUMN) VALUES (\"${NEW_TAG}\")" COUNTER=$((COUNTER++)) [ -z "${1:-}" ] && break done - sqlite3 -table tags.db "SELECT * FROM \"$TABLE\" LIMIT $COUNTER" + 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 tags.db "SELECT * FROM \"${TABLE_NAME}\"" + 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 tags.db "SELECT filename FROM files WHERE fid = (SELECT fid FROM tags_ties WHERE tid = (SELECT tid FROM tags WHERE label = \"${TAG_NAME}\"))" + 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 @@ -60,7 +61,7 @@ if [[ "$1" = "tag" ]]; then while true; do LABEL="$1" shift - sqlite3 -table tags.db "INSERT INTO tags_ties (fid, tid) VALUES ((SELECT fid FROM files WHERE filename = \"$FILENAME\"),(SELECT tid FROM tags WHERE label = \"$LABEL\"))" + 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\"))" [ -z "${1:-}" ] && break done listtags "$FILENAME"