Last active
July 26, 2020 02:30
-
-
Save tumf/4e394aba2e7e4caea184b507b4a1b753 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
path=$1 | |
go=$2 | |
commands='gist grep diff cut mktemp' | |
for c in $commands; do | |
hash $c || { echo "install $c"; exit 255; } | |
done | |
if [ ! -r $path ]; then | |
echo "$path not found" | |
exit 255 | |
fi | |
function usage() | |
{ | |
echo "Usage: `basename $0` file [go]" | |
} | |
if [ -z "$path" ]; then | |
usage | |
exit 255 | |
fi | |
function gist_url() | |
{ | |
local path=$1 | |
[ -z "$path" ] && exit 255 | |
local file=$(basename ${path}) | |
local url=$(gist -l|grep -E "^https://gist.github.com/[0-9a-z]+ ${file}( | \(secret\))$" |cut -d ' ' -f 1) | |
[ -n "$url" ] || return 1 | |
echo $url | |
} | |
function gist_id() | |
{ | |
local path=$1 | |
[ -z "$path" ] && exit 255 | |
local id=$(gist_url ${path} |cut -d '/' -f 4) | |
[ -n "$id" ] || return 1 | |
echo $id | |
} | |
function gist_diff() | |
{ | |
local path=$1 | |
[ -z "$path" ] && exit 255 | |
local id=$(gist_id $path) | |
[ -n "$id" ] || return 2 | |
local original_file=$(mktemp) | |
gist -r $id > ${original_file} || return 2 | |
diff -uN $original_file $path | |
local ret=$? | |
rm ${original_file} | |
return $ret | |
} | |
opts="" | |
update_opts="-p" | |
url=$(gist_url $path) | |
[ -n "$url" ] && update_opts="-u $url" | |
if [ "$go" = "go" ]; then | |
gist_diff $path &>/dev/null | |
else | |
gist_diff $path | |
fi | |
case $? in | |
0) | |
echo $url | |
exit | |
;; | |
1) | |
update_opts="-u $url" | |
;; | |
2) | |
[ "$go" = "go" ] || echo "new gist" | |
;; | |
*) | |
exit $? | |
;; | |
esac | |
if [ "$go" != "go" ]; then | |
while true; do | |
read -p "Do you wish to overwrite the gist? [Y] " yn | |
case $yn in | |
[Yy]* ) go="go"; break;; | |
* ) exit;; | |
esac | |
done | |
fi | |
[ "$go" = "go" ] && gist $opts $update_opts -- $path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment