Why? Because bash.

This commit is contained in:
kayos@tcp.direct 2022-11-14 17:27:51 -08:00
parent 4d8312f02d
commit 98d912951d
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
8 changed files with 164 additions and 114 deletions

View File

@ -1,109 +0,0 @@
#!/usr/bin/env bash
export _DESTINATION="$HOME/Workshop/"
# ----------------------------------
LPLUS="\e[0;33m[\e[0;32m+\e[0;33m]\e[0m"
LFAIL="\e[0;33m[\e[1;31mx\e[0;33m]\e[0m"
LFOOB="\e[0;33m[\e[90m-\e[0;33m]\e[0m"
LWOOT="\e[0;32mSuccess\e[0m!"
FATAL="\e[1;31m"
RESET="\e[0m"
function _t() {
echo -e "\e[90m[$(date -u +'%H:%M:%S')]${RESET}"
}
function cln() {
# shellcheck disable=SC2001
echo "$*" | sed -z 's|\n| |g'
}
function log() {
echo -e "$(cln "$(_t) ${LPLUS} $* ${RESET}")"
}
function debug() {
echo -e "$(cln "$(_t) ${LFOOB} \e[90m$*${RESET}")"
}
function err() {
echo -e "$(cln "$(_t) ${LFAIL} \e[31mERR \e[0m${*//fatal: /} ${RESET}")"
}
function fatal() {
err "${FATAL}[FATAL]${RESET}: $*"
exit 1
}
#-----------------------------------
function get() {
_PAGE=1
while :; do
_APIRES=$(curl -s "https://api.github.com/$_CONTEXT/$_USERNAME/repos?page=$_PAGE&per_page=100")
if ! [[ "$_APIRES" =~ "clone_url" ]]; then
if [[ "$_APIRES" =~ [a-zA-Z] ]]; then
echo "$_APIRES"
fatal "\e[1;31mbad response from API\e[0m"
fi
break
fi
_URLS=$(echo "$_APIRES" | grep "clone_url" | awk -F '"' '{print $4}')
if $_SSH; then _URLS=${_URLS//https:\/\//ssh:\/\/git@}; fi
while read -r line; do
cd "${_DESTINATION}/${_USERNAME}" || fatal "failed to change directory to ${_DESTINATION}/${_USERNAME}"
if _RES=$( (git clone "$line" >&1) 2>&1); then
log "${_RES}${LWOOT}"
continue
fi
if [[ "$_RES" =~ "already exists" ]]; then
update "$(echo "$_RES" | awk -F "'" '{print $2}')"
continue
fi
_REPO="$(echo "$line" | awk -F '/' '{print $NF}' | awk -F '.' '{print $0}')"
err "(${_REPO}) $_RES"
continue
done < <(echo "$_URLS")
(("_PAGE = $_PAGE + 1"))
done
}
function update() {
if ! cd "$1"; then
err "failed to change directory to update $1"
return 1
fi
if ! _F=$( (git fetch >&1) 2>&1); then
err "(${1}) $_F"
return 1
fi
if ! _P=$( (git pull >&1) 2>&1); then
err "(${1}) $_P"
return 1
fi
# shellcheck disable=SC2076
if ! [[ "$_P" =~ "Already up to date." ]]; then
log "Updating '$1'...${LWOOT}"
return 0
fi
debug "$1 already up to date"
return 0
}
function setup() {
if [ "$1" == "--ssh" ]; then
export _SSH=true
shift 1
fi
export _CONTEXT="users"
export _USERNAME="$1"
shift 1
mkdir -p "$_DESTINATION/$_USERNAME" || return 1
cd "$_DESTINATION/$_USERNAME" || return 1
}
export _SSH=false
if ! setup "$@"; then
fatal "Failed to setup directory structure!"
fi
targ="$1"
log "cloning all repos owned by $targ into $(pwd)/"
get
echo -e "\e[1;32mfin.\e[0m"

View File

@ -2,11 +2,13 @@
set -e
_target="$HOME/.local/bin"
mkdir -p "$_target"
rm -vrf "${_target}/clone-all"
cp -v clone-all.sh "${_target}/clone-all"
mkdir -vp "$_target"
rm -v "${_target}/clone-all"
touch "${_target}/clone-all"
cd src
cat *.sh >>"${_target}/clone-all"
chmod +x "${_target}/clone-all"
if ! [[ ":$PATH:" == *":$_target:"* ]]; then
echo "Your PATH is missing '$_target', consider adding it."
echo "Your PATH is missing '$_target', consider adding it."
fi
echo "fin."

1
src/0-meta.sh Normal file
View File

@ -0,0 +1 @@
#!/usr/bin/env bash

62
src/1-logr.sh Normal file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env bash
LPLUS="\e[0;33m[\e[0;32m+\e[0;33m]\e[0m"
LFAIL="\e[0;33m[\e[1;31mx\e[0;33m]\e[0m"
LFOOB="\e[0;33m[\e[90m-\e[0;33m]\e[0m"
REDIR="\e[0;90m"
LWOOT="\e[0;32mSuccess\e[0m"
LNOPE="\e[31mError\e[0m"
FATAL="\e[1;31m"
RESET="\e[0m"
LAST=""
function _t() {
echo -e "\e[90m[$(date -u +'%H:%M:%S')]${RESET}"
}
function cln() {
# shellcheck disable=SC2001
echo "$*" | sed -z 's|\n| |g'
}
function log() {
LAST="${*}..."
echo -ne "$(cln "$(_t) ${LFOOB} ${LAST}")"
}
function log0() {
echo -ne "\r$(cln "$(_t) ${LPLUS} ${LAST}${LWOOT}${RESET}")\n"
LAST=""
}
function log1() {
echo -ne "\r$(cln "$(_t) ${LFAIL} ${LAST}${LNOPE}${RESET}")\n"
LAST=""
}
function log2() {
NEW="${LAST//Cloning/Updating}${REDIR}${*}... ${RESET}"
echo -ne "\r$(cln "$(_t) ${LFOOB} ${NEW}")"
LAST=$NEW
}
function logln() {
echo -e "$(_t) ${LFOOB} ${*}"
}
function debug() {
if [ -z "$*" ]; then return 0; fi
echo -e "$(cln "$(_t) ${LFOOB} \e[90m$*${RESET}")"
}
function err() {
echo -e "$(cln "$(_t) ${LFAIL} ${LERRD}${*//fatal: /} ${RESET}")"
}
function fatal() {
err "${FATAL}[FATAL]${RESET}: $*"
exit 1
}
function testLogger() {
log "trying to yeet"
sleep 1
log0
log "trying to yort"
sleep 1
log1
log "trying to yeeeeeeeeeeet"
sleep 1
log2 "Slowing it down"
sleep 1
log0
}

22
src/2-init.sh Normal file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
function setup() {
shopt -s extglob
_DESTINATION=${CLONEALL_DESTINATION:-"$HOME/Workshop"}
_DESTINATION="${_DESTINATION%%+(/)}"
_SSH=false
if [ "$_DESTINATION" == "." ]; then
_DESTINATION=$(pwd)
fi
if [ "$1" == "--ssh" ]; then
_SSH=true
shift 1
fi
_CONTEXT="users"
_USERNAME="$1"
shift 1
debug "$(mkdir -vp "${_DESTINATION}/${_USERNAME}")" || return 1
}

36
src/3-loop.sh Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env bash
function get() {
_PAGE=1
while :; do
_APIRES=$(curl -s "https://api.github.com/$_CONTEXT/$_USERNAME/repos?page=$_PAGE&per_page=100")
if ! [[ "$_APIRES" =~ "clone_url" ]]; then
if [[ "$_APIRES" =~ [a-zA-Z] ]]; then
echo "$_APIRES"
fatal "\e[1;31mbad response from API\e[0m"
fi
break
fi
_URLS=$(echo "$_APIRES" | grep "clone_url" | awk -F '"' '{print $4}')
if $_SSH; then _URLS=${_URLS//https:\/\//ssh:\/\/git@}; fi
while read -r line; do
_REPO="$(echo "$line" | awk -F '/' '{print $NF}' | awk -F '.' '{print $0}' | sed 's|.git||g')"
_TARGET="${_DESTINATION}/${_USERNAME}/${_REPO}"
log "Cloning '$_REPO'"
if _RES=$( (git clone "$line" "$_TARGET" >&1) 2>&1); then
log0
continue
fi
if [[ "$_RES" =~ "already exists" ]]; then
log2 "Updating"
update "$_REPO"
continue
fi
err "(${_REPO}) $_RES"
continue
done < <(echo "$_URLS")
(("_PAGE = $_PAGE + 1"))
done
}

28
src/4-updt.sh Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env bash
function update() {
#debug "Update input: $1"
_TARGET="${_DESTINATION}/${_USERNAME}/$1"
#debug "$(mkdir -vp "$_TARGET")"
if ! cd "$_TARGET"; then
err "failed to change directory to update $1"
return 1
fi
if ! _F=$( (git fetch >&1) 2>&1); then
log1
err "$_F"
return 1
fi
if ! _P=$( (git pull >&1) 2>&1); then
log1
err "$_P"
return 1
fi
# shellcheck disable=SC2076
if ! [[ "$_P" =~ "Already up to date." ]]; then
log0
return 0
fi
echo -e "Current"
return 0
}

8
src/5-main.sh Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
if ! setup "$@"; then
fatal "Failed to setup directory structure!"
fi
logln "Cloning all repos owned by $_USERNAME into ${_DESTINATION}/${_USERNAME}/"
get
echo -e "\e[1;32mfin.\e[0m"