#!/usr/bin/env bash NC='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[0;33m' msg_info() { echo -e "${GREEN}$1${NC}" } msg_warn() { echo -e "${YELLOW}$1${NC}" } msg_error() { echo -e "${RED}$1${NC}" } print_help() { echo echo "grimoire - simple knowledge base manager" echo echo "USAGE:" echo " grimoire COMMAND [...]" echo echo "COMMANDs are:" echo " ls | list | tree:" echo " list pages in the wiki in a tree structure." echo " read | edit:" echo " read or edit wiki page." echo " peek:" echo " show wiki page rendered as a man page (using pandoc)." echo " search :" echo " perform a full text search of the entire wiki for " echo " and provides the user with a relevant list of matched files." echo " new:" echo " create new wiki page." echo " sync | push:" echo " sync wiki to git origin and Nextcloud PicoCMS content folder." echo " help:" echo " print this help" echo } ROOT=$HOME/git/wiki REMOTE=nextcloud:/sites/wiki/content COMMAND=${1:-list} shift case $COMMAND in ls|list|tree) tree "$ROOT" ;; read|edit|search) if [ "$COMMAND" = "search" ]; then FILE=$(rg --no-hidden --files-with-matches "$@" "$ROOT" | sed "s|^$ROOT/||" | fzf --layout=reverse --border --height=20) else FILE=$(find $ROOT -path "$ROOT/.*" -prune -o -type f -printf "%P\n" | fzf --layout=reverse --border --height=20) fi # Handle empty $FILE / null selection [ -z "$FILE" ] && msg_warn "No page selected...exiting!" && exit 0 SESSION=GRIMOIRE WINDOW=$(echo $FILE | sed 's|/| > |g;s|\.md$||') # If such a window already exists, exit without errors. tmux has-session -t "$SESSION:$WINDOW" 2>/dev/null && tmux switch-client -t "$SESSION:$WINDOW" && exit 0 # Create a new window (and the corresponding session if necessary). ! tmux has-session -t "$SESSION" 2>/dev/null && \ tmux new-session -d -s "$SESSION" -n "$WINDOW" || \ tmux new-window -t "$SESSION" -n "$WINDOW" # Setup a vertical split to use vim along with vim-slime tmux send-keys -t "$SESSION:$WINDOW" "cd $ROOT" Enter tmux send-keys -t "$SESSION:$WINDOW" "vim $FILE" Enter tmux split-window -t "$SESSION:$WINDOW" -h # Focus on vim tmux select-pane -t "$SESSION:$WINDOW".left # Attach to GRIMOIRE session (switch if already inside of tmux [ -z "$TMUX" ] && tmux attach-session -t "$SESSION:$WINDOW" || tmux switch-client -t "$SESSION:$WINDOW" ;; peek) FILE=$(find $ROOT -path "$ROOT/.*" -prune -o -type f -printf "%P\n" | fzf --layout=reverse --border --height=20) # Handle empty $FILE / null selection [ -z "$FILE" ] && msg_warn "No page selected...exiting!" && exit 0 # Pipe man pandoc output to `man` in local mode (reading from # stdin) pandoc -s -t man "$ROOT/$FILE" | man -l - ;; new) PAGE=$ROOT for i in "$@" do PAGE="$PAGE/$i" done # Ensure the require directory structure exists mkdir -p ${PAGE%/*} # Open new wiki page $EDITOR $PAGE ;; sync|push) # Synching to Nextcloud echo echo "[[[ Nextcloud ]]]" echo msg_warn "CHANGELOG ON THE SYNC OPERATION:" # Exclude hidden files EXCLUDES=".**" rclone sync --dry-run --exclude "$EXCLUDES" $ROOT $REMOTE 2>&1 >/dev/null | awk '{ print $4, $6 }' echo read -p "Do you want to proceed? [y/N] " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]] then rclone sync --exclude "$EXCLUDES" $ROOT $REMOTE --progress && msg_info "Success!" || (msg_error "Something went wrong! Check the output for more information." && exit 1) fi # Pushing to remote git echo echo "[[[ Git ]]]" echo pushd $ROOT >/dev/null GIT_REMOTE="origin" GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD) msg_warn "PUSHING TO $GIT_REMOTE:$GIT_BRANCH" read -p "Do you want to proceed? [y/N] " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]] then git push "$GIT_REMOTE" "$GIT_BRANCH" && msg_info "Success!" || (msg_error "Something went wrong! Check the output for more information." && exit 1) fi popd >/dev/null ;; help) print_help ;; *) msg_error "$COMMAND: invalid command" print_help exit 1 ;; esac exit 0