small pixel drawing of a pufferfish dotfiles

bin/MACC02VK5ECHTD7/how

#!/bin/sh

clone_url="ssh://git@git.bestbuy.com/~jesse.olson/howdocs-test.git"

if [ ! "$#" -gt 0 ]; then
	echo "Need a string to search for!"
	exit 1
fi

# check for fzf & ripgrep-all, install if unfound
if ! fzf --version > /dev/null 2>&1; then
	echo "fzf required. Install fzf via brew y/n? "
	read -r
	if [ "$REPLY" = "y" ]; then
		brew install fzf
	else
		exit 1
	fi
fi

if ! rg --version > /dev/null 2>&1; then
	echo "ripgrep required. Install ripgrep via brew y/n? "
	read -r
	if [ "$REPLY" = "y" ]; then
		brew install ripgrep
	else
		exit 1
	fi
fi

# maybe todo: if there are staged changes, ask user to push them?

# clone/update repository
if [ -d ~/howdocs ]; then
	(cd ~/howdocs && git pull)
else
	echo 'First run detected. Cloning repo...'
	git clone "$clone_url" ~/howdocs
fi

# fzf the directory
result=$(rg -S --files-with-matches --no-messages "$1" ~/howdocs \
	| fzf --preview "highlight -O ansi -l {} 2> /dev/null \
	| rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' \
	|| rg --ignore-case --pretty --context 10 '$1' {}")

if [ "$result" = "" ]; then
	echo 'no documents found'
	exit 1
fi

# open up relevant file in vim for copy+pasting
if [ "$EDITOR" ]; then
	"$EDITOR" "$result"
else
	vim "$result"
fi

# if file changed, save & commit changes & push
if ! (cd ~/howdocs && git diff-index --quiet HEAD --); then
	echo "detected howdoc changes; pushing them:"
	(cd ~/howdocs && git add --all && git commit -m '[Automated Commit]' && git push)
fi