add remove notes

This commit is contained in:
Maksim Totmin 2024-05-20 11:04:35 +07:00
parent 77989adc72
commit ef77abb71a

52
note
View File

@ -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
exit 0