From ef77abb71acd4f97473b23f50d6ba243e445c7e6 Mon Sep 17 00:00:00 2001 From: Maksim Totmin Date: Mon, 20 May 2024 11:04:35 +0700 Subject: [PATCH] add remove notes --- note | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/note b/note index a755790..95ac534 100755 --- a/note +++ b/note @@ -61,6 +61,31 @@ CMD_GREP(){ echo "add to-do grep" } +CMD_DELETE(){ + local opts recursive="" force=0 + opts="$($GETOPT -o rf -l recursive,force -n "$PROGRAM" -- "$@")" + local err=$? + eval set -- "$opts" + while true; do case $1 in + -r|--recursive) recursive="-r"; shift ;; + -f|--force) force=1; shift ;; + --) shift; break ;; + esac done + [[ $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--recursive,-r] [--force,-f] note-name" + local path="$1" + check_sneaky_paths "$path" + + local notedir="$NOTESPATH/${path%/}" + local notefile="$NOTESPATH/$path.md" + [[ -f $notefile && -d $notedir && $path == */ || ! -f $notefile ]] && notefile="${notedir%/}/" + [[ -e $notefile ]] || die "Error: $path is not in the note store." + + [[ $force -eq 1 ]] || yesno "Are you sure you would like to delete $path?" + + rm $recursive -f -v "$notefile" + rmdir -p "${notefile%/*}" 2>/dev/null +} + CMD_COPY_MOVE() { move=1 [[ $1 == "copy" ]] && move=0 @@ -86,8 +111,30 @@ if [ -z "$1" ]; then CMD_SHOWTREE fi +check_sneaky_paths() { + local path + for path in "$@"; do + [[ $path =~ /\.\.$ || $path =~ ^\.\./ || $path =~ /\.\./ || $path =~ ^\.\.$ ]] && die "Error: You've attempted to pass a sneaky path to pass. Go home." + done +} + +die() { + echo "$@" >&2 + exit 1 +} + +yesno() { + [[ -t 0 ]] || return 0 + local response + read -r -p "$1 [y/N] " response + [[ $response == [yY] ]] || exit 1 +} + PROGRAM="${0##*/}" COMMAND="$1" +GETOPT="getopt" +SHRED="shred -f -z" +BASE64="base64" case "$1" in init) shift; CMD_INIT "$@" ;; @@ -95,13 +142,14 @@ case "$1" in show|ls|list) shift; CMD_SHOWTREE "$@" ;; find|search) shift; CMD_FIND "$@" ;; grep) shift; CMD_GREP "$@" ;; + delete|rm|remove) shift; CMD_DELETE "$@" ;; copy|cp) shift; CMD_COPY_MOVE "copy" "$@" ;; rename|mv) shift; CMD_COPY_MOVE "move" "$@" ;; - edit) shift; CMD_INSERT "$@" ;; + edit|add) shift; CMD_INSERT "$@" ;; version|-v) CMD_SHOWVERSION ;; *) CMD_HELP "$@" ;; esac find "$NOTESPATH"* -maxdepth 0 -type f ! -name *.md -delete &> /dev/null -exit 0 \ No newline at end of file +exit 0