aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xreposync.sh50
1 files changed, 35 insertions, 15 deletions
diff --git a/reposync.sh b/reposync.sh
index bcbad7e..1426aa5 100755
--- a/reposync.sh
+++ b/reposync.sh
@@ -14,7 +14,7 @@
# USERNAME GitHub Username
# TOKEN Password or Token
# REPO_DIR Directory on the filesystem
-# PRIVATE Treat repositories as private (YES/[NO])
+# PRIVATE Treat repositories as private (true/false)
ARGV=($@)
ARGC=${#ARGV[@]}
@@ -24,26 +24,37 @@ API_BASE="https://api.github.com"
# ['name']='ssh_url' # name with .git suffix
declare -A GH_REPOS
+function url_inject_credentials() {
+ sed -n -e "s/^\(https:\/\/\)\(.*\)$/\1$USERNAME:$TOKEN@\2/p"
+}
+
function curl_wrapper() {
- RAW_CURL=$(curl -s -w "%{http_code}" $@; exit $?)
- RET=$?
- [ $RET -ne 0 ] && >&2 echo curl failed with $RET && exit 1
+ local CURL_RETURN
+ CURL_RETURN=$( curl -s -w "%{http_code}" $@ ; exit $? )
+ local RET=$?
+ [ $RET -ne 0 ] && >&2 echo cURL code $RET && return 1
+
+ head -n -1 <<< "$CURL_RETURN"
- HTTP_CODE=$(tail -n 1 <<< "$RAW_CURL")
- [ $HTTP_CODE -ge 300 ] && >&2 echo HTTP error $HTTP_CODE && exit 1
+ local HTTP_CODE=$(tail -n 1 <<< "$CURL_RETURN")
+ [ $HTTP_CODE -ge 300 ] && >&2 echo HTTP Code $HTTP_CODE && return 1
- head -n -1 <<< "$RAW_CURL"
+ return 0
}
# create new repository for $USERNAME
# 1: name
function github_create_repo() {
[ -z "$TOKEN" ] && >&2 echo TOKEN not set. No write access. && exit 1
+
+ local JSON_RETURN
JSON_RETURN=$(curl_wrapper -X POST -H "Content-Type: application/json" -u $USERNAME:$TOKEN \
- -d "{\"name\":\"$1\"}" "$API_BASE/user/repos"; exit $? )
+ -d "{\"name\":\"$1\",\"private\":$PRIVATE}" "$API_BASE/user/repos"; exit $? )
[ $? -ne 0 ] && exit 1
- GH_REPOS[$1.git]=$(jq -r ".clone_url" <<< $JSON_RETURN | sed -n -e "s/^\(https:\/\/\)\(.*\)$/\1$USERNAME:$TOKEN@\2/p" )
+ echo "$JSON_RETURN"
+
+ GH_REPOS[$1.git]=$(jq -r ".clone_url" <<< "$JSON_RETURN" | url_inject_credentials )
}
function github_update_repo_list() {
@@ -52,15 +63,19 @@ function github_update_repo_list() {
local CURL_USER=""
[ ! -z "$TOKEN" ] && CURL_USER="-u $USERNAME:$TOKEN"
- # replace with /user/repos to also get private
- JSON_REPOS=$(curl_wrapper $CURL_USER "$API_BASE/users/$USERNAME/repos"; exit $?)
- [ $? -ne 0 ] && exit 1
+ local VISIBILITY=""
+ [ "$PRIVATE" = "true" ] && VISIBILITY="private" || VISIBILITY="public"
+
+ local JSON_REPOS
+ JSON_REPOS=$(curl_wrapper -u $USERNAME:$TOKEN \
+ "$API_BASE/user/repos?visibility=$VISIBILITY"; exit $?)
+ [ $? -ne 0 ] && jq ".message" <<< "$JSON_REPOS" && exit 1
GH_REPOS_COUNT=$(jq ". | length" <<< "$JSON_REPOS")
for (( i=0; i<$GH_REPOS_COUNT; i++ )); do
- name="$(jq ".[$i].name" <<< "$JSON_REPOS" | tr -d '"' ).git"
- GH_REPOS[$name]=$(jq -r ".[$i].clone_url" <<< "$JSON_REPOS" | sed -n -e "s/^\(https:\/\/\)\(.*\)$/\1$USERNAME:$TOKEN@\2/p" )
+ name="$(jq -r ".[$i].name" <<< "$JSON_REPOS" ).git"
+ GH_REPOS[$name]=$(jq -r ".[$i].clone_url" <<< "$JSON_REPOS" | url_inject_credentials )
done
}
@@ -68,6 +83,12 @@ function github_update_repo_list() {
source "$1"
+[ -z "$USERNAME" -o -z "$TOKEN" ] && echo GitHub credentials config error. && exit 1
+
+[ ! -d "$REPO_DIR" ] && echo Repo directory does not exist. && exit 1
+
+[ "$PRIVATE" != "true" ] && PRIVATE=false
+
github_update_repo_list
LOCAL_REPOS=( $(for repo in $(ls -d $REPO_DIR/*.git/ 2> /dev/null); do basename $repo; done ) )
@@ -91,7 +112,6 @@ printf "%s\n" "${TO_PUSH[@]}"
echo
for repo in "${TO_CLONE[@]}"; do
- ##sudo -u git mkdir "$REPO_DIR/$repo"
sudo -u git git clone --bare \
"${GH_REPOS[$repo]}" "$REPO_DIR/$repo"
done