failsafe optional var, template baserul for downloads
[webi-installers/.git] / rg / rg.bash
1 # title: Ripgrep
2 # homepage: https://github.com/BurntSushi/ripgrep
3 # tagline: a modern drop-in grep replacement
4 # alias: rg
5 # description: |
6 #   `rg` is a drop-in replacement for `grep`, that respects `.gitignore` and `.ignore`, has all of the sensible default options you want (colors, numbers, etc) turned on by default, is written in Rust, and simply outperforms grep in every imaginable way. R.I.P. grep.
7 # examples: |
8 #
9 #   ```bash
10 #   rg <search-term> # searches recursively, ignoing .git, node_modules, etc
11 #   ```
12 #
13 #   ```bash
14 #   rg 'function doStuff'
15 #   ```
16 #
17 #   ```bash
18 #   rg 'doStuff\(.*\)'
19 #   ```
20
21 set -e
22 set -u
23
24 # Use the script's first argument or the supplied WEBI_VERSION or ''
25 WEBI_VERSION=${1:-${WEBI_VERSION:-}}
26
27 # Set a temporary directory, if not already set
28 WEBI_TMP=${WEBI_TMP:-"$(mktemp -d -t webinstall-ripgrep.XXXXXXXX)"}
29
30 ###################
31 #  Get WEBI vars  #
32 ###################
33
34 # The WEBI bootstrap will define these
35 # but each script should be testable in its own right
36
37 if [ -z "${WEBI_PKG_URL:-}" ]; then
38   release_tab="${WEBI_HOST}/api/releases/ripgrep@${WEBI_VERSION:-}.csv?os=$(uname -s)&arch=$(uname -m)&ext=tar&limit=1"
39   WEBI_CSV=$(curl -fsSL "$release_tab" -H "User-Agent: $(uname -a)")
40   WEBI_CHANNEL=$(echo $WEBI_TAB | cut -d ',' -f 3)
41   if [ "error" == "$WEBI_CHANNEL" ]; then
42      echo "could not find release for ripgrep v${WEBI_VERSION}"
43      exit 1
44   fi
45   WEBI_VERSION=$(echo $WEBI_TAB | cut -d ',' -f 1)
46   WEBI_PKG_URL=$(echo $WEBI_TAB | cut -d ',' -f 9)
47   WEBI_PKG_FILE="$WEBI_TMP/$(echo $WEBI_PKG_URL | sed s:.*/::)"
48 fi
49
50 ###################
51 # Install ripgrep #
52 ###################
53
54 new_rg="${HOME}/.local/bin/rg"
55
56 # Test for existing version 
57 set +e
58 cur_rg="$(command -v rg)"
59 set -e
60 if [ -n "$cur_rg" ]; then
61   cur_ver=$(rg --version | head -n 1 | cut -d ' ' -f 2)
62   if [ "$cur_ver" == "$WEBI_VERSION" ]; then
63     echo "ripgrep v$WEBI_VERSION already installed at $cur_rg"
64     exit 0
65   elif [ "$cur_rg" != "$new_rg" ]; then
66     echo "WARN: possible conflict with ripgrep v$WEBI_VERSION at $cur_rg"
67   fi
68 fi
69
70 # TODO move download to the webi bootstrap
71 echo Downloading ripgrep v"${WEBI_VERSION}" from "${WEBI_PKG_URL}"
72 curl -fsSL "${WEBI_PKG_URL}" -o "${WEBI_PKG_FILE}"
73 pushd "${WEBI_TMP}" 2>&1 >/dev/null
74         echo Installing ripgrep v${WEBI_VERSION} as "$new_rg" 
75         tar xf "${WEBI_PKG_FILE}"
76         rm "${WEBI_PKG_FILE}"
77         mv ./ripgrep-*/rg "${HOME}/.local/bin/"
78 popd 2>&1 >/dev/null
79
80 ###################
81 #   Update PATH   #
82 ###################
83
84 # TODO get better output from pathman / output the path to add as return to webi bootstrap
85 pathman add "$HOME/.local/bin/"
86
87 echo "Installed 'rg'"
88 echo ""