Created
February 24, 2023 21:34
-
-
Save vgmoose/3028bb2f7983bb0c67441b0e460f31ff to your computer and use it in GitHub Desktop.
Download packages from a pacman repo in bash directly, without pacman installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# pacman downloader in bash | |
# requires: curl and tar | |
# the repo name and URL (includes OS) | |
REPO_NAME="my-repo" | |
ARCH=$(uname -m) | |
REPO_URL="https://packages.yourpacmanrepo.com/$ARCH" | |
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 .gz repo info from: $REPO_URL/$REPO_NAME.files" | |
RES=$(curl -s $REPO_URL/$REPO_NAME.files | tar tzf - | grep /$ | sed 's/.$//') | |
# 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 | |
# if verbose print version and arch too | |
if [ "$VERBOSE" = "1" ]; then | |
echo "${packages[$i]}-$ARCH" | |
else | |
echo "${packages[$i]}" | |
fi | |
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 | |
# if we end with $ARCH, remove it | |
if [ "${TARGET: -${#ARCH}}" = "$ARCH" ]; then | |
TARGET=${TARGET:0:-${#ARCH}} | |
fi | |
# download the package | |
OUTPUT=$1-$ARCH.pkg.tar.xz | |
vecho "Fetching .tar.xz package from: $REPO_URL/$OUTPUT" | |
curl --output $OUTPUT -s $REPO_URL/$OUTPUT | |
return $? | |
} | |
extract_package() { | |
# first download it | |
download_package $1 | |
# then extract it | |
vecho "Extracting package contents to $TARGET" | |
mkdir -p $TARGET | |
tar xf $OUTPUT -C $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-1.0.0-1" | |
echo " $0 -s ouija_board" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment