Last active
August 28, 2023 18:32
-
-
Save MarkWalters-dev/992616d92e2cfca8b8e999490f16c1fc to your computer and use it in GitHub Desktop.
Add and update gists
This file contains 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
#!/usr/bin/env bash | |
#🌩️ gist 992616d92e2cfca8b8e999490f16c1fc ghgist | |
# Uses gh to create and update gists | |
# TODO: | |
# If .gist used then setup symlinks | |
# Need method to gh gist clone without using a .gist folder | |
# look for short description in file and use it | |
# if short description is provided then insert in file | |
# Short description always directly after gist id comment and an empty line | |
# Complex gists might need a readme. | |
# If .gist not used then .filename.readme.md or maybe .filename.md | |
# actual gist filename of readme would be README.md | |
# if used could also look for short description in readme | |
# this might be useful if --help is used. You could mdcat the readme | |
crash() { | |
local err=$? | |
printf "%s\nCrashed at: %s:%s\n" "$*" "${FUNCNAME[1]}" "${BASH_LINENO[0]}" | |
exit $err | |
} | |
command_exists() { | |
command -v $1 > /dev/null 2>&1 | |
return $? | |
} | |
f=$1 | |
[ -f "$f" ] || { echo "Usage: $(basename ${BASH_SOURCE[0]}) FILENAME [DESCRIPTION IF NEW GIST]"; exit; } | |
command_exists gh || crash "gh not installed" | |
[ -e "$HOME/.config/gh/hosts.yml" ] || crash "gh not configured\nRun gh auth login" | |
gist_icon="🌩️" # Unlikely I will find a line beginning with # and this symbol | |
l=$(grep "^#${gist_icon}" $f) | |
[ -n "$l" ] && { | |
gist_id=${l#*${gist_icon} gist } | |
gist_filename=${gist_id#* } # Does not support filenames with spaces | |
gist_id=${gist_id% *} | |
[ -n "$gist_id" ] || crash "Could not extract gist_id" | |
[ -n "$gist_filename" ] || crash "Could not extract gist_id" | |
echo Updating gist | |
gh gist edit $gist_id -f $gist_filename $f || crash "gh update error" | |
} || { | |
shift | |
description="$*" | |
[ -n "$description" ] || { echo "New gist requires a description"; exit; } | |
gist_filename=${f##*/} # Same as using basename | |
[ -n "$gist_filename" ] || crash "Error getting gist_filename" | |
gist_id=$(gh gist create --public -d "$description" -f $f $f) || crash "gh create error" | |
gist_id=${gist_id##*/} | |
[ -n "$gist_id" ] || { gh gist list; crash "Error getting gist_id\nDo you see anything new?"; } | |
# nix-script with nix-shell can use multiple shebang lines. | |
last_shebang=$(grep -n "^#!" $f | tail -1 | cut -f1 -d:) | |
[ -z "$last_shebang" ] && last_shebang=0 | |
last_shebang=$(( last_shebang + 1 )) | |
sed -i "${last_shebang}i\#${gist_icon} gist $gist_id $gist_filename" $f || crash "sed error" | |
gh gist edit $gist_id -f $gist_filename $f || crash "gh update error" | |
} | |
alternatemethod() { | |
# Would require a dedicated .gist folder and symlinks to bin folder | |
git init | |
git remote add gist "$(gh gist create README.md)" | |
git fetch gist | |
git checkout -f master | |
git add . && git commit -m "add the rest of the files" | |
git push | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment