Skip to content

Instantly share code, notes, and snippets.

@jakefhyde
Last active November 21, 2023 22:50
Show Gist options
  • Select an option

  • Save jakefhyde/5870995af106c8d369895f9d6ecbdd0c to your computer and use it in GitHub Desktop.

Select an option

Save jakefhyde/5870995af106c8d369895f9d6ecbdd0c to your computer and use it in GitHub Desktop.
Github gist manager
#!/usr/bin/env zsh
set -e
GIST_ROOT=${GIST_ROOT:-${HOME}/.gist}
GIST_LIMIT=${GIST_LIMIT:-100}
BASE="$(basename $0)"
function display_help() {
echo "Usage: ${BASE}"
echo
echo ' -e, --executable [Optional] make files from gists matching *.sh executable and push'
echo ' -l, --link [Optional] create symlinks for files from gists matching *.sh ~/.local/bin'
echo " -d, --debug [Optional] enables debug logging"
echo " -h, --help print this message"
}
POSITIONAL_ARGS=()
EXECUTABLE=false
LINK=false
DEBUG=false
while [[ $# -gt 0 ]]; do
case $1 in
-e|--executable)
EXECUTABLE=true
shift
;;
-l|--link)
LINK=true
shift
;;
-d|--debug)
DEBUG=true
shift # past argument
;;
-h|--help)
display_help
exit 1
;;
-*|--*)
echo "Unknown option $1"
display_help
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
function log() {
echo -e "$1 $2"
}
function debug() {
if $DEBUG; then
log "\033[0;36m[DEBUG]\033[0m" "$1"
fi
}
function fatal() {
log "\033[0;31m[FATAL]\033[0m" "$1"
exit 1
}
function error() {
log "\033[0;31m[ERROR]\033[0m" "$1"
}
function warning() {
log "\033[1;33m[WARN]\033[0m" "$1"
}
function info() {
log "\033[0;32m[INFO]\033[0m" "$1"
}
function init() {
debug "Creating gist root"
mkdir -p ${GIST_ROOT}
}
function clone_gist() {
debug "Cloning gist $1"
gh gist clone $1
}
function update_gist() {
debug "Updating gist $1"
pushd $1
if ! git diff --quiet HEAD; then
fatal "Refusing to update gist $1, due to uncommitted changes"
fi
git fetch origin main >/dev/null 2>&1 && git checkout origin/main >/dev/null 2>&1
popd
}
function link_gist() {
if [ ! -d $HOME/.local/bin ]; then
fatal "$HOME/.local/bin does not exist, exiting"
fi
for f in $(find ${GIST_ROOT}/$1 -type f -name "*.sh"); do
debug "Creating symlink for $f"
if [ ! -f $HOME/.local/bin/$(basename $f) ]; then
ln -s $f $HOME/.local/bin/$(basename $f)
elif [ "$(realpath "$f")" != "$(realpath "$HOME/.local/bin/$(basename $f)")" ]; then
fatal "$f already exists in $HOME/.local/bin"
fi
done
}
function make_executable() {
debug "Making files in $1 executable"
pushd $1
if ! git diff --quiet HEAD; then
fatal "Refusing to update gist $1, due to uncommitted changes"
fi
for f in $(find ${GIST_ROOT}/$1 -type f -name "*.sh"); do
debug "Making $f executable"
chmod +x $f
git add $f
done
if ! git diff --quiet HEAD; then
git commit -m "Set executable"
git push --set-upstream origin
fi
popd
}
function main() {
init
pushd ${GIST_ROOT}
for g in $(gh gist list --limit ${GIST_LIMIT} | tail -n +1 | awk '{print $1}'); do
info "configuring ${g}"
if [ ! -d ./${g} ]; then
clone_gist ${g}
else
update_gist ${g}
fi
if [ $EXECUTABLE ]; then
make_executable ${g}
fi
if [ $LINK ]; then
link_gist ${g}
fi
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment