Compare commits

...

7 Commits

Author SHA1 Message Date
kayos@tcp.direct a55c031a34
Fix: install.sh 2022-11-18 07:27:39 -08:00
kayos@tcp.direct d95568b7ae
Fix CI 2022-11-18 07:22:12 -08:00
kayos@tcp.direct f2d4cab3df
tui fixes 2022-11-14 20:42:40 -08:00
kayos@tcp.direct 58dce564b8
Update README.md 2022-11-14 17:56:41 -08:00
kayos@tcp.direct d94d3d47ee
Fix SC2035 2022-11-14 17:53:01 -08:00
kayos c648ce9735
Update bash.yml 2022-11-14 17:51:11 -08:00
kayos@tcp.direct 98d912951d
Why? Because bash. 2022-11-14 17:27:51 -08:00
10 changed files with 186 additions and 118 deletions

View File

@ -1,12 +1,12 @@
on:
push:
branches:
- master
name: "shellcheck"
jobs:
shellcheck:
name: shellcheck
runs-on: ubuntu-latest
env:
INPUT_SHELLCHECK_OPTIONS: --format=gcc -x
steps:
- uses: actions/checkout@v3
- name: shellcheck

View File

@ -10,3 +10,8 @@ Clones and updates all github repos of a given user or organization. Supports SS
1) clone this repo
2) run `install.sh`
3) `clone-all target` or `clone-all --ssh target`
### Environment Variables
- `CLONEALL_SSH`: set/export this to true to enable ssh without the command-line flag
- `CLONEALL_DESTINATION`: set/export this to the directory you want the cloned/updated repos to live

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

@ -1,12 +1,19 @@
#!/usr/bin/env bash
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"
set +e
rm "${_target}/clone-all" 2 &>/dev/null
set -e
echo '#!/usr/bin/env bash' >>"${_target}/clone-all"
cd src
cat ./*.sh | grep -v 'usr/bin/env' >>"${_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."
cd ..
if ! ls -lah "${_target}/clone-all" >/dev/null; then
echo "installation failed somehow"
fi
echo "clone-all installed to: ${_target}/clone-all"

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

@ -0,0 +1,49 @@
#!/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
}

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
}

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

@ -0,0 +1,29 @@
#!/usr/bin/env bash
function update() {
#debug "Update input: $1"
_TARGET="${_DESTINATION}/${_USERNAME}/$1"
#debug "$(mkdir -vp "$_TARGET")"
if ! cd "$_TARGET"; then
log1
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
echo -e "Current"
return 0
fi
log0
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"

21
test/test_logger.sh Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
# shellcheck disable=SC1091
source ./src/1-logr.sh 2&>/dev/null|| source ../src/1-logr.sh
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
}
testLogger