Skip to content

Instantly share code, notes, and snippets.

@vgmoose
Created February 24, 2023 23:16
Show Gist options
  • Save vgmoose/6e0fb243385dfeca733788b3abd994ec to your computer and use it in GitHub Desktop.
Save vgmoose/6e0fb243385dfeca733788b3abd994ec to your computer and use it in GitHub Desktop.
Implementation of https://github.com/fortheusers/libget repo downloader directly in bash
#!/bin/bash
# libget repo downloader in bash
# requires: curl, jq, and unzip
# the repo URL
REPO_URL="https://wiiu.cdn.fortheusers.org"
VERBOSE=0
vecho () {
if [ "$VERBOSE" = "1" ]; then
echo "=> $@"
fi
}
# used to store the downloaded file
OUTPUT=""
TARGET=""
# arrays where we will store package names and versions
declare -a packages
# load all package and version info into the hash
sync_packages () {
# download the repo info
vecho "Fetching json repo info from: $REPO_URL/repo.json"
RES=$(curl -s $REPO_URL/repo.json | jq -r '.packages[] | .name')
# copy the data into our package arrays
while read -r line; do
packages+=("$line")
done <<< "$RES"
}
list_packages() {
# sync the repo info
sync_packages
# print the package names
for i in "${!packages[@]}"; do
echo "${packages[$i]}"
done
return 0
}
search_package() {
# sync the repo info
sync_packages
vecho "Searching for matches for package: $1"
# search for the package
for i in "${!packages[@]}"; do
if [[ "${packages[$i]}" =~ $1 ]]; then
echo "${packages[$i]}"
fi
done
vecho "Package not found: $1"
return 1
}
download_package() {
TARGET=$1
# download the package
OUTPUT="$1.zip"
vecho "Fetching .zip package from: $REPO_URL/zips/$OUTPUT"
curl --output $OUTPUT -s $REPO_URL/zips/$OUTPUT
return $?
}
extract_package() {
# first download it
download_package $1
# then extract it
vecho "Extracting package contents to $TARGET"
mkdir -p $TARGET
unzip $OUTPUT -d $TARGET
return $?
}
# main args loop
while [ "$1" != "" ]
do
# on -v or --verbose, set verbose mode
if [ "$1" = "-v" ] || [ "$1" = "--verbose" ]; then
VERBOSE=1
fi
# on -l or --list, list packages
if [ "$1" = "-l" ] || [ "$1" = "--list" ]; then
list_packages
exit $?
fi
# on -d or --download, download package
if [ "$1" = "-d" ] || [ "$1" = "--download" ]; then
download_package $2
echo "Downloaded to $OUTPUT"
exit $?
fi
# on -x or --extract, download and extract package
if [ "$1" = "-x" ] || [ "$1" = "--extract" ]; then
extract_package $2
echo "Downloaded $OUTPUT, and extracted to $TARGET"
exit $?
fi
# on -s or --search, search for package
if [ "$1" = "-s" ] || [ "$1" = "--search" ]; then
search_package $2
exit $?
fi
shift
done
# display help info
echo "Usage: $0 [options] [package]"
echo "Options:"
echo " -l, --list List all packages"
echo " -s, --search Return a list of matching packages"
echo " -d, --download Download package by name"
echo " -x, --extract Download and extract package"
# TODO: add install, but might mess up filesystem and stuff
# echo " -i, --install Download, extract, and install package"
echo " -v, --verbose Verbose output"
echo " -h, --help Display this help message"
echo "Examples:"
echo " $0 -l"
echo " $0 -d ouija_board"
echo " $0 -s ouija_board"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment