Skip to content

Instantly share code, notes, and snippets.

@mattrasband
Last active October 28, 2020 21:19
Show Gist options
  • Save mattrasband/2224e7c88e3b478607b1efe4e52f8176 to your computer and use it in GitHub Desktop.
Save mattrasband/2224e7c88e3b478607b1efe4e52f8176 to your computer and use it in GitHub Desktop.
repos.sh
#!/usr/bin/env bash
usage() {
echo "Usage: $0 -d <base directory> -u <github username> <repo list>" 1>&2
echo "repo list is a text file with remote:org/repo names (per-line). If remote is anything but origin, the remote is cloned and origin is set to GITHUB_USERNAME/repo.git" 1>&2
exit 1
}
github_username="$GITHUB_USERNAME"
dev_dir="$DEV_DIR"
while getopts ":d:u:" o; do
case "${o}" in
d)
dev_dir="$OPTARG"
;;
u)
github_username="$OPTARG"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if [ -z "$github_username" ]; then
echo "missing -u <github username>"
exit 1
fi
if [ -z "$dev_dir" ]; then
echo "missing -d <base directory>"
exit 2
fi
if [ "${#@}" -eq 0 ]; then
echo "Missing repository file(s)"
exit 3
fi
for repo_list in "$@"; do
if [ ! -f "$repo_list" ]; then
echo "list of repositories $repo_list isn't a file"
exit 4
fi
done
for repo_list in "$@"; do
while read repo; do
remote=$(echo -n "$repo" | awk -F: '{ print $1 }')
path=$(echo -n "$repo" | awk -F: '{ print $2 }')
org=$(echo -n "$path" | awk -F/ '{ print $1 }')
repo_name=$(echo -n "$path" | awk -F/ '{ print $2 }')
target_dir="$dev_dir/$org"
repo_dir="$target_dir/$repo_name"
echo -n "Checking for $repo_dir... "
if [ -d "$repo_dir" ]; then
echo "OK!"
else
echo "Missing, cloning!"
mkdir -p "$target_dir"
git clone --quiet --origin $remote git@github.com:${org}/${repo_name}.git "$repo_dir"
if [[ "$remote" != "origin" ]]; then
cd $repo_dir
git remote add origin git@github.com:${github_username}/${repo_name}.git
cd - >/dev/null
fi
fi
done <"$repo_list"
done
#!/usr/bin/env sh
trap 'kill 0' INT
usage() {
echo "Usage: $0 [-t seconds] host:port[:timeout] ..." 1>&2
exit 1
}
wait_for() {
local timeout=$(echo -n "${1}" | awk -F: -v defaultTimeout=${2} '{ if ($3 == "") print defaultTimeout; else print $3 }')
local host=$(echo -n "${1}" | awk -F: '{ print $1 }')
local port=$(echo -n "${1}" | awk -F: '{ print $2 }')
echo "Waiting for $host:$port with a timeout of $timeout seconds"
local checkNum=0
while ! nc -z "$host" "$port" >/dev/null 2>&1; do
if [ "$timeout" -gt 0 ]; then
if [ $checkNum -ge $timeout ]; then
echo "$host:$port failed after $checkNum seconds"
exit 1
fi
checkNum=$((checkNum + 1))
fi
sleep 1
done
echo "$host:$port listening on TCP, let's go"
}
timeout=60
while getopts ":t:" o; do
case "${o}" in
t)
timeout=${OPTARG}
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
if ! hash nc 2>/dev/null; then
if hash apt 2>/dev/null; then
apt update
apt install -y netcat
hash -r
fi
fi
for arg in "$@"; do
wait_for "$arg" "$timeout" &
done
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment