Skip to content

Instantly share code, notes, and snippets.

@datsfilipe
Created December 28, 2023 16:49
Show Gist options
  • Save datsfilipe/15a407d5bfbd21778787ccd02a8a2020 to your computer and use it in GitHub Desktop.
Save datsfilipe/15a407d5bfbd21778787ccd02a8a2020 to your computer and use it in GitHub Desktop.
A custom notes vault management with shell script, jq and git.
#!/bin/sh
config_file="$HOME/.config/datsvault.json"
if [ ! -e "$config_file" ]; then
echo "Config file '$config_file' does not exist. Creating one..."
mkdir -p "$HOME/.config"
echo '{"vault_path": "'"$HOME/.config/datsvault"'", "terminal": "alacritty", "vault_repo": "null"}' > "$config_file"
# Prettify the config file
tmp=$(mktemp)
jq . "$config_file" > "$tmp"
mv "$tmp" "$config_file"
echo "Config file created. Please edit it and try again."
exit 0
fi
vault_path=$(jq -r '.vault_path' "$config_file")
term_app=$(jq -r '.terminal' "$config_file")
vault_repo=$(jq -r '.vault_repo' "$config_file")
if [ ! -d "$vault_path" ]; then
if [ "$vault_repo" = "null" ]; then
echo "Vault path '$vault_path' does not exist. Please create it and try again."
exit 1
else
echo "Vault path '$vault_path' does not exist. Cloning the remote repository..."
mkdir -p "$vault_path"
git clone "$vault_repo" "$vault_path"
echo "Remote repository cloned."
fi
fi
case "$1" in
-n)
title="$(date '+%Y-%m-%d')"
note_path="$vault_path/$title.md"
echo "---" >> "$note_path"
echo "title: \"$title\"" >> "$note_path"
echo "---" >> "$note_path"
tmux new-session -d -s "$title" "nvim '$note_path'; tmux kill-session -t $title"
$term_app -e tmux attach-session -t "$title" >/dev/null 2>&1 &
disown
;;
-d)
note="$2"
note_path=$(fd "$note" "$vault_path" | head -n 1)
if [ -n "$note_path" ]; then
title=$(grep -m 1 '^title: ' "$note_path" | sed 's/^title: "\(.*\)"/\1/')
trash-put "$note_path"
echo "Note '$title' deleted."
else
echo "Note '$note' not found."
exit 1
fi
;;
-l)
selected_note=$(fd '\.md$|\.mdx$' "$vault_path" --type f | fzf)
if [ -n "$selected_note" ]; then
title=$(grep -m 1 '^title: ' "$selected_note" | sed 's/^title: "\(.*\)"/\1/')
tmux new-session -d -s "$title" "nvim '$selected_note'; tmux kill-session -t $title"
$term_app -e tmux attach-session -t "$title" >/dev/null 2>&1 &
disown
fi
;;
-s)
pushd "$vault_path" || exit 1
git add .
if git diff --quiet --exit-code --cached && git diff --quiet --exit-code; then
echo "No changes to commit in the vault."
else
only_deletions=$(git diff --cached --name-status | grep '^D' | wc -l)
only_additions=$(git diff --cached --name-status | grep '^A' | wc -l)
staged_files=$(git diff --cached --name-only)
if [ "$only_deletions" -gt 0 ] && [ "$only_additions" -eq 0 ]; then
commit_msg="refactor: delete $only_deletions file(s)"
elif [ "$only_deletions" -eq 0 ] && [ "$only_additions" -gt 0 ]; then
commit_msg="feat: add $only_additions file(s)"
else
commit_msg="refactor: update $staged_files"
fi
git commit -m "$commit_msg"
# Assuming you are pushing to the 'main' branch of the 'origin' remote
git push origin main
echo "Vault updated and pushed to the remote repository with commit message: $commit_msg"
fi
popd || exit 1
;;
-u)
pushd "$vault_path" && git pull origin main && popd
echo "Vault updated with changes from the remote repository."
;;
*)
echo "Usage: $0 [-n | -d note | -l | -s | -u]"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment