Last active
October 20, 2022 23:41
-
-
Save ThinGuy/6ced7b53e85b7fa3576ced3b54ac3b82 to your computer and use it in GitHub Desktop.
Bash function to add a repo with new "signed-by" line in apt sources
This file contains hidden or 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
add-repo() { | |
add-repo_usage() { | |
# Help Blue, Help Blue Bold, Help Blue Italic, Italic Text and Reset Text | |
local HB='\e[38;2;72;226;225m' HBB='\e[1;38;2;72;226;225m' HBI='\e[3;38;2;72;226;225m' IT='\e[3m' RT='\e[0m' | |
printf "\n\e[2G${HBB}${FUNCNAME%_*}${RT}: ${IT}Add a repo using the new \x22signed-by\x22 feature${RT}\n\n" | |
printf "\e[2G${HBB}Usage${RT}:\n\n" | |
printf "\e[5G${FUNCNAME%_*} [ options ]\n\n" | |
printf "\e[2G${HBB}Options${RT}:\n\n" | |
printf "\e[5G-n,--repo-name\e[22GRepo Name\n\n" | |
printf "\e[5G-u,--repo-url\e[22GRepo URL (in sources.list format)\n\n" | |
printf "\e[5G-k,--repo-key\e[22GID (fingerprint, long, short) or URL of GPG Key\n\n" | |
printf "\e[2G${HBB}Examples${RT}:\n\n" | |
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n mongdb-6 \x5C\n\e[3G-u 'https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse' \x5C\n\e[3G-k https://www.mongodb.org/static/pgp/server-6.0.asc\n\n" | |
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n maas-3.2 \x5C\n\e[3G-u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \x5C\n\e[3G-k 4e7fdc5684d4a1c\n\n" | |
printf "\e[2G${HBB}${FUNCNAME%_*}${RT} \x5C\n\e[3G-n maas-3.2 \x5C\n\e[3G-u 'https://ppa.launchpadcontent.net/maas/3.2/ubuntu focal main' \x5C\n\e[3G-k 3ab6dcf1f234e78daa9c104204e7fdc5684d4a1c\n\n" | |
};export -f add-repo_usage | |
[[ ${@} = -h ]] && { ${FUNCNAME}_usage;return 2; } | |
local REPO_NAME REPO_URL REPO_KEY | |
ARGS=$(getopt -o n:u:k:h --long repo-name:,repo-url:,repo-key:,help -n ${FUNCNAME} -- "$@") | |
eval set -- "$ARGS" | |
while true ; do | |
case "$1" in | |
-n|--repo-name) local REPO_NAME="${2}";shift 2;; | |
-u|--repo-url) local REPO_URL="${2}";shift 2;; | |
-k|--repo-key) local REPO_KEY="${2}";shift 2;; | |
-h|--help) ${FUNCNAME}_usage;return 2;; | |
--) shift;break;; | |
*) printf "\n\e[0;38;2;255;0;0mError\x21\e[0m\e[1m Unknown Option \x22${1}\x22\e[0m\n\n";return 1;; | |
esac | |
done | |
[[ -n ${REPO_NAME} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing Name for Repo\n\n";add-repo_usage;return 1; } | |
[[ -n ${REPO_URL} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing Repo URL\n\n";add-repo_usage;return 1; } | |
[[ -n ${REPO_KEY} ]] && { true; } || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Missing GPG Key ID/URL\n\n";add-repo_usage;return 1; } | |
[[ $(grep -qiE 'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)' <<< "${REPO_KEY}";echo $?) -eq 0 ]] && { local KEYTYPE=URL; } | |
[[ ${#REPO_KEY} =~ (40|16|8) ]] && { local KEYTYPE=ID; } | |
[[ ${KEYTYPE} = ID ]] && { (curl -fsSL 'https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x'${REPO_KEY}''|gpg --dearmor|sudo tee 1>/dev/null /usr/share/keyrings/${REPO_NAME}-keyring.gpg);local KRR=$?; } | |
[[ ${KEYTYPE} = URL ]] && { (curl -fsSL ${REPO_KEY}|gpg --dearmor|sudo tee 1>/dev/null /usr/share/keyrings/${REPO_NAME}-keyring.gpg);local KRR=$?; } | |
[[ ${KRR} -eq 0 ]] || { printf "\n\e[2G\e[0;38;2;255;0;0mERROR:\e[0m Could not create /usr/share/keyrings/${REPO_NAME}-keyring.gpg\n\n";return 1; } | |
[[ ${KRR} -eq 0 ]] && { echo "deb [arch=$(printf '%s\n' --print-architecture --print-foreign-architectures|xargs -n1 dpkg|paste -sd,) signed-by=/usr/share/keyrings/${REPO_NAME}-keyring.gpg] ${REPO_URL}" |sudo tee /etc/apt/sources.list.d/${REPO_NAME}.list > /dev/null; } | |
sudo apt update | |
};export -f add-repo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.