Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Last active November 18, 2019 20:58
Show Gist options
  • Select an option

  • Save bitmvr/c8551672c6ebd167280067af1083c68a to your computer and use it in GitHub Desktop.

Select an option

Save bitmvr/c8551672c6ebd167280067af1083c68a to your computer and use it in GitHub Desktop.
The portable jq installer for the scripting elite.
#!/usr/bin/env bash
PATH=./jqi-tmp:$PATH
jqi::getLatestTag(){
local response
response="$(curl -s https://github.com/stedolan/jq/releases/latest)"
response="${response##*tag/}"
response="${response%%\">*}"
echo "$response"
}
jqi::osDetection(){
local os="$OSTYPE"
case "$os" in
darwin*)
echo "macOS"
;;
linux*)
echo "linux"
;;
msys*)
echo "windows"
;;
*)
echo "unknown"
;;
esac
}
jqi::macosInstall(){
local version="$1"
curl -sL -o jq "https://github.com/stedolan/jq/releases/download/${version}/jq-osx-amd64"
chmod +x ./jq
}
jqi::winInstall(){
local version="$1"
curl -sL -o jq.exe "https://github.com/stedolan/jq/releases/download/${version}/jq-win64.exe"
chmod +x ./jq.exe
}
jqi::linuxInstall(){
local version="$1"
curl -sL -o jq "https://github.com/stedolan/jq/releases/download/${version}/jq-linux64"
chmod +x ./jq
}
jqi::packageDropzone(){
mkdir -p ./jqi-tmp && cd "$_" || exit 1
}
jqi::returnToRoot(){
cd ..
}
jqi::cleanup(){
rm -rf ./jqi-tmp
}
jqi::installjq(){
local tag
local platform
tag="$(jqi::getLatestTag)"
platform="$(jqi::osDetection)"
jqi::packageDropzone
case "$platform" in
macOS)
jqi::macosInstall "$tag"
;;
linux)
jqi::linuxInstall "$tag"
;;
windows)
jqi::winInstall "$tag"
;;
unknown)
echo "ERROR: Could not determine your operating system. Exiting..."
exit 1
esac
jqi::returnToRoot
}
@bitmvr
Copy link
Copy Markdown
Author

bitmvr commented Mar 6, 2019

You can test jqi by saving it locally to your machine and in the same directory, create a new script with the following content:

Testing the jq Installer

#!/usr/bin/env bash

# Source jqi and install if jq is not available

if ! command -v jq; then
  source ./jqi.sh
  jqi::installjq
  JQI_IN_USE="TRUE"
fi

test_json='{"name":"John", "age":31, "city":"New York"}'

echo "jq location: $(command -v jq)"
echo "Test JSON string: $test_json"
echo "Value for Name: $(jq -r '.name' < <(echo "$test_json"))"
echo "Value for Age: $(jq -r '.age' < <(echo "$test_json"))"
echo "Value for City: $(jq -r '.city' < <(echo "$test_json"))"
echo ""

# Safely Perform A Cleanup
if [ -n "$JQI_IN_USE" ]; then
  jqi::cleanup
fi

Test Output

jq location: ./jqi-tmp/jq
Test JSON string: {"name":"John", "age":31, "city":"New York"}
Value for Name: John
Value for Age: 31
Value for City: New York

@chriscorwin
Copy link
Copy Markdown

So -- I put jqi somewhere in my path? @bitmvr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment