bin/MACC02VK5ECHTD7/how
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/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