Skip to content

Instantly share code, notes, and snippets.

@falgon
Last active November 15, 2021 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save falgon/3d3d120b3ec9b47960bfcb8951fb3772 to your computer and use it in GitHub Desktop.
Save falgon/3d3d120b3ec9b47960bfcb8951fb3772 to your computer and use it in GitHub Desktop.
TagSpaces (https://www.tagspaces.org/) を使うに当たって CLI 上でタグを付与 (ファイル名を変更) するユーティリティツール
#!/bin/bash
set -euf
# set exported tagspaces json file path
# e.g. json_name="$HOME/.config/tag-lib.json" addtag -show
declare json_name
help_message() {
echo "Usage:"
echo -e "\taddtag ([option] [filename | tag_title] | [filename] tagname...)"
echo -e "\toption:"
echo -e "\t\t-rm:\tremoving tags from [filename]"
echo -e "\t\t-show:\tshow specified tag"
echo -e "\t\t-V,--version:\tversion information"
}
tag_process() {
eval arr=(${1})
if [ ${#arr[@]} -eq 0 ]; then
help_message
exit 1
fi
arr=$(printf "%s " "${arr[@]}" | xargs -n1 | sort -g | uniq | xargs)
echo "[${arr}]"
}
get_fname() {
fpath="$1"
fname="$(basename "$fpath")"
fname="${fname%.*}"
echo "$fname"
}
get_extension() {
fpath="$1"
fname="$(basename "$fpath")"
ext="${fname##*.}"
if [ "$ext" = "$fname" ]; then
ext=""
else
ext=".${ext}"
fi
echo "$ext"
}
already_tagged() {
already_tag_str="$(echo "$1" |\
awk '{print substr($0, index($0, "["), index($0, "]") -1 )}')"
already_tag_str="$(echo ${already_tag_str/%?/})"
already_tag_str="${already_tag_str:1}"
echo "$already_tag_str"
}
version() {
echo "addtag for TagSpaces v$1"
}
read_tag_json() {
if [ -z "$json_name" ] || [ ! -f "$json_name" ]; then
return -1
fi
cat "$json_name" |\
jq ".tagGroups[].children[].title"
}
if [ $# -gt 0 ]; then
if [ "$1" = "-V" ]; then
version "0.0.1"
exit 0
elif [ "$1" = "--version" ]; then
version "0.0.1"
exit 0
elif [ "$1" = "-rm" ]; then
if [ -n "$2" ] && ([ -f "$2" ] || [ -d "$2" ]); then
fname="$(get_fname "$2")"
extension="$(get_extension "$2")"
fname_without_tag="$(echo "$fname" | sed -e "s/\[.*\]//g")"
if [ -z "$(already_tagged "$fname")" ]; then
echo "this file ($fname) is not tagged"
echo "nothing to do"
exit 0
else
fname="${fname_without_tag}${extension}"
new_fpath="$(dirname "$2")/${fname}"
echo "remove tagging..."
echo "rename to $new_fpath from $2"
mv "$2" "$new_fpath"
exit 0
fi
elif [ -n "$2" ]; then
echo "no such file or directory: $2"
exit 1
else
help_message
exit 1
fi
elif [ "$1" = "-show" ]; then
read_tag_json
res=$?
if [ $res -eq -1 ]; then
echo "invalid json file or no such file or directory" >&2
exit 1
fi
exit 0
elif [ -f "$1" ] || [ -d "$1" ]; then
args=("$@")
for((i=1;i<$#;i++)) do
QUOTED_ARGS+=("'${args[$i]}'")
done
fname="$(get_fname "$1")"
extension="$(get_extension "$1")"
atagged="$(already_tagged "$fname")"
tags=""
if [ -z "$atagged" ]; then
tags="$(tag_process "${QUOTED_ARGS[*]}")"
added_tags="${tags:1:-1}"
echo "set new tags: $added_tags"
elif [ -n "$atagged" ]; then
for tag in "$atagged"; do
QUOTED_ARGS+=("'${tag}'")
done
tags="$(tag_process "${QUOTED_ARGS[*]}")"
echo "this file is already tagged"
added_tags="${tags:1:-1}"
echo "added tags: $added_tags"
fi
fname_without_tag="$(echo "$fname" | sed -e "s/\[.*\]//g")"
fname="$fname_without_tag${tags}${extension}"
new_fpath="$(dirname "$1")/${fname}"
echo "rename to $new_fpath from $1"
mv "$1" "$new_fpath"
else
echo "no such file or directory: $1" >&2
exit 1
fi
else
help_message
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment