commit 401a3b74c30076d2dd6cd2ac0575f95333eb95a7
parent e2ac3e2943f21486ff9c585ccdd7d186197e80e5
Author: Anton Konyahin <me@konyahin.xyz>
Date: Sun, 4 Jun 2023 20:33:28 +0300
Rework argument's checks
Diffstat:
M | tagger | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------- |
1 file changed, 114 insertions(+), 15 deletions(-)
diff --git a/tagger b/tagger
@@ -3,11 +3,64 @@
# Developer notes
# - 'tagger alias folder' for output alias and completion commands
# - add '_tagger_completion name folder` for aliases completion
+# - check command for test tagger folder correctnes
set -e
-print_help() {
- echo "tagger - simple tagging system for any types of files
+# Error handling and checks
+err () {
+ echo "$@" >&2
+ exit
+}
+
+check () {
+ name="$1"
+ desc="$2"
+
+ pexist "$name" "$desc"
+
+ shift 2
+ for chk in "$@"
+ do
+ "$chk" "$name" "$desc"
+ done
+
+}
+
+pexist () {
+ [ -z "$1" ] &&
+ err "$2 should be specified"
+}
+
+pnodir () {
+ [ -d "$1" ] &&
+ err "$2 already exist: $1"
+}
+
+pfile () {
+ [ ! -f "$1" ] &&
+ err "$2 should exist and be a file: $1"
+}
+
+pdir () {
+ [ ! -d "$1" ] &&
+ err "$2 should exist and be a directory: $1"
+}
+
+ptagged () {
+ [ ! -f "$base/$1" ] &&
+ err "$2 should be added before: $1"
+}
+
+pbase () {
+ [ ! -d "$base" ] &&
+ err "$2 should be tagger folder: $1"
+}
+
+# Script logic
+
+print_help () {
+ err "tagger - simple tagging system for any types of files
Usage:
tagger init name
create new folder structure, under name, for
@@ -19,25 +72,71 @@ Usage:
tagger untag folder file tag
remove tag from file in tagger folder
tagger ls folder
- list all files under tagger folder
-"
+ list all files under tagger folder"
}
-if [ -z "$1" ]; then
- print_help
- exit
-fi
+## tagger init name
+## create new folder structure, under name, for
+## keeping files and tags
+init_tagger () {
+ check "$1" "tagger folder" pnodir
-base_folder="$2/.base"
+ mkdir -p "$base"
+}
+
+## tagger add folder file
+## move file to tagger in tagger folder
+add_file () {
+ check "$1" "tagger folder" pdir pbase
+ check "$2" "file" pfile
+
+ mv "$2" "$base"
+}
-[ "$1" = "init" ] && mkdir -p "$base_folder" && exit
+## tagger tag folder file tag
+## add tag to file from tagger folder
+tag_file () {
+ check "$1" "tagger folder" pdir pbase
+ check "$2" "file" pfile ptagged
+ check "$3" "tag"
+
+ tag_folder="$1/$3"
+ mkdir -p "$tag_folder"
+ ln -s "../.base/$2" "$tag_folder"
+}
+
+## tagger untag folder file tag
+## remove tag from file in tagger folder
+untag_file () {
+ check "$1" "tagger folder" pdir pbase
+ check "$2" "file" pfile ptagged
+ check "$3" "tag"
+
+ tag_folder="$1/$3"
+ rm -f "$tag_folder/$2"
+}
+
+## tagger ls folder
+## list all files under tagger folder
+list () {
+ check "$1" "tagger folder" pdir pbase
+ ls -1 "$base"
+}
-[ "$1" = "add" ] && mv "$3" "$base_folder" && exit
+# Process arguments
-[ "$1" = "tag" ] && mkdir -p "$2/$4" && ln -s "../.base/$3" "$2/$4/" && exit
+[ "$#" -lt 1 ] && print_help
-[ "$1" = "untag" ] && rm "$2/$4/$3" && exit
+command="$1"
+shift 1
-[ "$1" = "ls" ] && ls -1 "$base_folder" && exit
+base="$1/.base"
-echo "Unknown command: $1"
+case "$command" in
+ init) init_tagger "$@" ;;
+ add) add_file "$@" ;;
+ tag) tag_file "$@" ;;
+ untag) untag_file "$@" ;;
+ ls) list "$@" ;;
+ *) err "Unknown command $command" ;;
+esac