Skip to content

Instantly share code, notes, and snippets.

@osvein
Last active September 25, 2017 18:40
Show Gist options
  • Select an option

  • Save osvein/eb8a7a6d7fc5f1d455a63dba67709de7 to your computer and use it in GitHub Desktop.

Select an option

Save osvein/eb8a7a6d7fc5f1d455a63dba67709de7 to your computer and use it in GitHub Desktop.
#!/bin/sh
public="true"
desc=
host="api.github.com"
port="443"
endpoint="/gists"
repourl="https://gist.github.com"
creds="url=$repourl
"
while getopts su:d: opt
do
case $opt in
s) public="false";;
u) username="${OPTARG%%":"*}"
password="${OPTARG#*":"}"
test "$username" && creds="${creds}username=$username
"
test "$password" && creds="${creds}password=$password
"
;;
d) desc="$OPTARG";;
?) printf 'Usage: %s [-d description] [-s] [-u username[:password]]' $0 >&2
exit 2
;;
esac
done
creds="$(echo "$creds" | git credential fill)"
echo "$creds" | while IFS="=" read -r key value
do
case $key in
username) username="$value";;
password) password="$value";;
esac
done
body="$(printf '{
"description": "%s",
"public": "%s",
"files": { ".gist-init" : { "content": "
" } }
}' "$desc" "$public")"
id=
printf 'POST %s HTTP/1.1\r\n'\
'Host: %s\r\n'\
'User-Agent: %s\r\n'\
'Accept: application/json\r\n'\
'Content-Type: application/json\r\n'\
'Content-Length: %d\r\n'\
'\r\n'\
'%s' "$endpoint" "$host" "$0" "${#body}" "$body" | openssl s_client -connect "$host:$port" -quiet | {
read -r line
echo "$line" >2
while read -r line && test "$line" && test "$line" != "\r"
do
if [ "$(echo "${line%%":"*}" | tr [:upper:] [:lower:])" = "location" ]
then
id="${line#*"/"}"
break;
fi
done
}
if [ ! "$id" ]
then
echo "$creds" | git credential reject
exit 1;
fi
echo "$creds" | git credential approve
gisturl="$repourl/$id"
echo "$gisturl"
git init
git remote add origin "$gisturl"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment