note-store/note
2024-05-20 11:04:35 +07:00

156 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
VER=0.0.1
NOTESPATH="$HOME/.note-store"
CMD_INSERT(){
path=$1
mkdir -p -v "$NOTESPATH/$(dirname -- "$path")" &> /dev/null
path="$NOTESPATH/$path.md"
$EDITOR "$path"
}
CMD_HELP(){
if [ ! -z $1 ]; then
CMD_INSERT "$@"
else
echo
cat <<-_EOF
Usage:
$PROGRAM init
Initialize new note storage.
$PROGRAM [ls]
List notes.
$PROGRAM [edit] note-name
Insert a new note or edit an existing note using editor.
$PROGRAM help
Show this text.
$PROGRAM version
Show version information.
_EOF
fi
exit 0
}
CMD_INIT(){
test -d "$NOTESPATH" && echo "Note Store already exist" && exit 0 ||
mkdir "$NOTESPATH" &> /dev/null
echo "New note storage creted to: "$NOTESPATH""
exit 0
}
CMD_SHOWTREE(){
if [ ! -z "$1" ]; then
glow -p $NOTESPATH/"$1.md"
else
echo "Note Store"
tree -N -C -l --noreport "$NOTESPATH" | tail -n +2 |\
sed -E 's/\.md(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
fi
exit 0
}
CMD_FIND(){
[[ $# -eq 0 ]] && die "Usage: $PROGRAM $COMMAND note-names..."
IFS="," eval 'echo "Search Terms: $*"'
local terms="*$(printf '%s*|*' "$@")"
tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs --ignore-case "$NOTESPATH" |\
tail -n +2 | sed -E 's/\.md(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
}
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
shift
old_path="$NOTESPATH/${1%/}"
old_dir="$old_path"
new_path="$NOTESPATH/$2"
if [[ $move -eq 1 ]]; then
mv -v "$old_path.md" "$new_path.md" || exit 1
else
cp -r -v "$old_path.md" "$new_path.md" || exit 1
fi
}
CMD_SHOWVERSION(){
echo $PROGRAM version: $VER
}
if [[ ! "$1" = init ]]; then
test -d "$NOTESPATH" || CMD_HELP
fi
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 "$@" ;;
help|--help) shift; CMD_HELP "$@" ;;
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|add) shift; CMD_INSERT "$@" ;;
version|-v) CMD_SHOWVERSION ;;
*) CMD_HELP "$@" ;;
esac
find "$NOTESPATH"* -maxdepth 0 -type f ! -name *.md -delete &> /dev/null
exit 0