Skip to content

Instantly share code, notes, and snippets.

@thecjharries
Created May 29, 2018 04:32
Show Gist options
  • Save thecjharries/ee68f256ab940d9eb51f311491a7dbd0 to your computer and use it in GitHub Desktop.
Save thecjharries/ee68f256ab940d9eb51f311491a7dbd0 to your computer and use it in GitHub Desktop.
A quick tutorial on how to use curl to set up a new repo

Setup

git can automatically register scripts that are in your $PATH that begin with git-. This will set up and download the included scripts.

Note: you'll have to replace [gist_id with the ID in the URL for now. I haven't updated this yet with the published URL.

$ mkdir -p ~/bin
$ export PATH=$HOME/bin:$PATH
$ curl -fLo ~/bin/git-github-create-pat https://gist.github.com/thecjharries/[gist_id]/raw/git-github-create-pat.sh
$ curl -fLo ~/bin/git-github-init-remote https://gist.github.com/thecjharries/[gist_id]/raw/git-github-init-remote.sh
$ git help -a | grep -E 'github-(create-pat|init-remote)' && echo 'Installation worked' || echo 'Installation failed'

Make sure to edit the files with the values for your situation. You won't get far trying to authenicate as me.

Usage

First run git-github-create-pat like so:

$ git github-create-pat

This adds a hidden config containing curl authenication information in the form of user:token.

After that's done, you can run git-github-init-remote, which needs the personal access token generated similarly to the above:

$ cd <project directory>
$ git init
$ git github-init-remote <project name>

If you don't specify a project name, the script uses the current directory name, i.e. the repo will be named basename <project directory>.

Improvements

Many are planned. Stay tuned.

Copyright 2018 CJ Harries
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#!/bin/bash
# UNCOMMENT ME FOR DEBUGGING
set -x
# SET ME
GITHUB_USER=thecjharries
RC_FILE="$HOME/.zshrc"
TOKEN_NOTE="$(hostname) CLI token"
CONFIG_DIRECTORY="$HOME/.config/github"
RAW_RETURN="${CONFIG_DIRECTORY}/${GITHUB_USER}-$(hostname).json"
FINAL_PAT="${CONFIG_DIRECTORY}/${GITHUB_USER}-$(hostname)-pat"
# UNCOMMENT TO ADD TO RC FILE
# if ! grep "GITHUB_USER" "${RC_FILE}"; then
# echo "export GITHUB_USER=${GITHUB_USER}" >> "${RC_FILE}"
# source "${RC_FILE}"
# fi
# MAKE SURE YOU UNDERSTAND WHAT THE FOLLOWING CODE DOES
# YOU DON'T NEED TO TOUCH ANYTHING BELOW THIS COMMENT UNLESS YOU WANT TO
DATA=$(
cat <<EOF
{
"scopes": ["repo"],
"note": "$TOKEN_NOTE"
}
EOF
)
mkdir -p "$CONFIG_DIRECTORY"
read -s -p "GitHub password: " GITHUB_PASSWORD
read -s -p "2FA app code (blank for no 2FA): " OTP
if [[ -z $OTP ]]; then
HEADER=''
else
HEADER=" --header 'x-github-otp: $OTP' "
fi
eval "curl --user '${GITHUB_USER}:${GITHUB_PASSWORD}' $HEADER --data '$DATA' --output '${RAW_RETURN}' https://api.github.com/authorizations"
TOKEN=$(
awk '\
/"token":/{ \
gsub(",$", "", $2); \
gsub("^\"|\"$", "", $2); \
print $2; \
}' \
"${RAW_RETURN}"
)
echo "${GITHUB_USER}:$TOKEN" > "${FINAL_PAT}"
#!/bin/bash
set -x
# SET THIS IF IT DNE
# GITHUB_USER=
# CHANGE THIS TO A FILE THAT CONTAINS 'username:token'
PAT_FILE="$HOME/.config/github/${GITHUB_USER}-$(hostname)-pat"
if [[ -z "${1+x}" ]]; then
PROJECT=$(basename $PWD)
else
PROJECT=$1
fi
DATA=$(cat <<EOF
{
"name": "$PROJECT",
"homepage": "https://github.com/${GITHUB_USER}/$PROJECT#readme",
"private": false,
"has_issues": true
}
EOF
)
OUTFILE=$(mktemp --tmpdir github-XXXXXX.json)
curl \
-u $(cat "${PAT_FILE}") \
-o "$OUTFILE" \
-fL \
--data "$DATA" \
https://api.github.com/user/repos
REMOTE=$(
awk '\
/"ssh_url":/{ \
gsub(",$", "", $2); \
gsub("^\"|\"$", "", $2); \
print $2; \
}' \
"$OUTFILE"
)
git remote add origin "$REMOTE"
rm -rf "$OUTFILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment