Skip to content

Instantly share code, notes, and snippets.

@111a5ab1
Last active July 4, 2019 02:40
Show Gist options
  • Save 111a5ab1/a3e13546311a5c028de38f07fe0f558a to your computer and use it in GitHub Desktop.
Save 111a5ab1/a3e13546311a5c028de38f07fe0f558a to your computer and use it in GitHub Desktop.
Create Github repo via command-line
#!/usr/bin/env bash
set -e
#-----------------------------------------------
# REPOSITORY
# Define GitHub repository details
#-----------------------------------------------
readonly USER="111A5AB1"
readonly REPOSITORY_NAME="terraform-gitlab-repos"
readonly IS_PRIVATE="true"
#-----------------------------------------------
# API
# Define GitHub API settings
# Ref: https://developer.github.com/v3
#-----------------------------------------------
readonly GIT_API_URL="https://api.github.com"
readonly API_VER="application/vnd.github.v3+json"
# Explicitly set API version to v3, https://developer.github.com/v3/#current-version
readonly USER_AGENT="${USER}"
# GitHub asks User-Agent is set to username or application, https://developer.github.com/v3/#user-agent-required
#-----------------------------------------------
# AUTH
#-----------------------------------------------
# Define password store key paths
readonly PASS_PATH="github"
readonly OTP_PATH="GitHub/github.com/${GITHUB_USER}"
# Read GitHub password and OTP from password store
password="$(pass ${PASS_PATH})"
otp="$(pass otp ${OTP_PATH})"
#-----------------------------------------------
# CREATE
# Create the new repository
# https://developer.github.com/v3/repos/#create
#-----------------------------------------------
curl --request POST \
--url "${GIT_API_URL}/user/repos" \
--user "${USER}:${password}" \
--user-agent "${USER_AGENT}" \
--header "Accept: ${API_VER}"
--header "Content-Type: application/json" \
--header "x-github-otp: ${otp}" \
--data '{"name":"${REPOSITORY_NAME}", "private":"${IS_PRIVATE}"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment