Skip to content

Instantly share code, notes, and snippets.

@dugancathal
Created October 24, 2018 02:12
Show Gist options
  • Save dugancathal/43976cf5741df326a4e4c1e02466e11b to your computer and use it in GitHub Desktop.
Save dugancathal/43976cf5741df326a4e4c1e02466e11b to your computer and use it in GitHub Desktop.
Create a repo on github using API - ./create-repo.sh $REPONAME [$USERNAME [$TOKEN]]
#!/usr/bin/env bash
CREATE_REPO_LOG_PATH=/tmp/create-repo.log
GITHUB_API=https://api.github.com
HEADERS=(-H 'Content-Type: application/json')
username="${2:-"dugancathal"}"
token="${3:-"$(cat ~/.github-token)"}"
if [[ "${1}" == "" ]]; then
echo -n 'Create repo: '
read reponame
else
reponame="${1}"
fi
echo "Creating repo: '${reponame}' for @${username}" | tee "${CREATE_REPO_LOG_PATH}"
curl -vvv -s "${GITHUB_API}/user/repos" -u"${username}:${token}" "${HEADERS[@]}" -d @- >> "${CREATE_REPO_LOG_PATH}" 2>&1 << JSON
{"name": "${reponame}"}
JSON
CREATE_REPO_COMMANDS=(
"git remote add origin git@github.com:${username}/${reponame}"
"git push -u origin master"
)
if [[ "$?" != 0 ]]; then
echo "FAILED to create repo!"
sleep 1
cat "${CREATE_REPO_LOG_PATH}"
exit 1
else
echo "SUCCESS!"
echo "Would you like to add that remote as an origin and push?"
read shouldCreateRemote
if [[ "${shouldCreateRemote}" == y* ]]; then
for command in "${CREATE_REPO_COMMANDS[@]}"; do
$command
done
else
echo "Ok. Add it to your repo with:"
for command in "${CREATE_REPO_COMMANDS[@]}"; do
echo " " $command
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment