Skip to content

Commit

Permalink
jq is better for json parsing
Browse files Browse the repository at this point in the history
rust-lang#423 (comment) was having problems installing. (I think their python3 install was having a problem parsing json?) Having an install fail just because it was unable to locate the version number seems like "my kingdom for a horseshoe nail" now that the version is relatively stable. (Its good to have a single point of truth, but) we could have an emergency version number of "3.0.0" in this file rather than have the install fail because rust and rustlings does not depend on python for to function.

Having this rustlings install depend on bash that depends on python seems like "extra steps" that could be avoided. The pure shell version also works with some other shells such as dash.
  • Loading branch information
alexxroche authored Jun 6, 2020
1 parent bb2ca25 commit 0a3c4c9
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,26 @@ then
elif [ -x "$(command -v python2)" ]
then
PY="$(command -v python2)"
elif [ -x "$(command -v jq)" ]
then
PY="" # we will ignore python and try to use jq
JQ="$(command -v jq)" #jq is much faster than python for json parsing
else
echo "ERROR: No working python installation was found"
echo "Please install python and add it to the PATH variable"
exit 1
echo "WARNING: No working python installation was found; trying jq"
#echo "Please install python and add it to the PATH variable"
#exit 1
fi

# install jq for faster and more efficient json parsing
if [ -x "$(command -v brew)" ] && uname -a|grep -q Darwin;
then # probably OSX
command -v jq 1>/dev/null|| $(command -v brew ) install jq
JQ="$(command -v jq)" #jq is much faster than python for json parsing
fi

# Function that compares two versions strings v1 and v2 given in arguments (e.g 1.31 and 1.33.0).
# Returns 1 if v1 > v2, 0 if v1 == v2, 2 if v1 < v2.
function vercomp() {
vercomp() {
if [[ $1 == $2 ]]
then
return 0
Expand Down Expand Up @@ -102,7 +113,36 @@ Path=${1:-rustlings/}
echo "Cloning Rustlings at $Path..."
git clone -q https://github.com/rust-lang/rustlings $Path

Version=$(curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest | ${PY} -c "import json,sys;obj=json.load(sys.stdin);print(obj['tag_name']);")
# function to locate the version number from the latest releases JSON
locate_version(){
if [ -x "$JQ" ]; then
curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest | $JQ -r .tag_name
elif [ -x "$PY" ]; then
curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest | ${PY} -c "import json,sys;obj=json.load(sys.stdin);print(obj['tag_name']);"
else # try to use BASH to locate the version number in the json
#if [ ! -f "/tmp/rustlings_latest.json" ]; then
# curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest > /tmp/rustlings_latest.json
# #curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest|grep tag_name|awk '{print $NF}'|tr -d '"'|tr -d ','
#fi
#if [ -f "/tmp/rustlings_latest.json" ]; then # the advantage of writing to a temp file is that we can error if the network, (or curl) fail.
# ## Writing to a temp file so that my tests don't hammer the github api
# #grep tag_name /tmp/rustlings_latest.json|awk '{print $NF}'|tr -d '"'|tr -d ','
# # the above line is faster but requires awk and tr
# VER=$(grep tag_name /tmp/rustlings_latest.json)
VER=$(curl -s https://api.github.com/repos/rust-lang/rustlings/releases/latest|grep tag_name)
VER=${VER%\"*} # shadowing
VER=${VER##*\"}
if [ "$VER" ];then # we can do this in memory, rather than writing to /tmp/
echo $VER
else
# echo 3.0.0 # we could fall back to a default version and change this to a WARNING?
echo "ERROR: unable to parse json using either jq or python" >&2
exit 1
fi
fi
}

Version="$(locate_version)"
CargoBin="${CARGO_HOME:-$HOME/.cargo}/bin"

echo "Checking out version $Version..."
Expand Down

0 comments on commit 0a3c4c9

Please sign in to comment.