-
Notifications
You must be signed in to change notification settings - Fork 1
/
retrieveRepos.sh
executable file
·61 lines (54 loc) · 1.73 KB
/
retrieveRepos.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env bash
echo ${GITHUB_AUTHTOKEN}
function github(){
if [ -z "${GITHUB_AUTHTOKEN}" ];
then
curl -s https://api.github.com/${1}
else
curl -s -H "Authorization: token ${GITHUB_AUTHTOKEN}" https://api.github.com/${1}
fi
}
function clone() {
echo Cloning https://github.com/${1}/${2}
pushd ${CODE_HOME}/${1}/
hub clone ${1}/${2} > /dev/null
echo Cloned ${1}/${2} to ${CODE_HOME}/${1}/${2}
popd
}
set -e
[[ -z "${1}" ]] && echo Please specify User or Org to clone. && exit 1
: ${CODE_HOME:=${HOME}/go/src/github.com} && mkdir -p ${CODE_HOME} && echo CODE_HOME is set to ${CODE_HOME}
CMDS="hub jq curl git"
for i in $CMDS
do
# command -v will return >0 when the $i is not found
which $i >/dev/null && continue || { echo "$i command not found."; exit 1; }
done
[[ ! $(github orgs/$1/repos | jq -r .message) == "Not Found" ]] && type=org
[[ ! $(github users/$1/repos | jq -r .message) == "Not Found" ]] && type=user
[[ -z "${type}" ]] && echo Invalid User or Org && exit 2
[[ ! -d ${CODE_HOME}/$1 ]] && echo Making User Folder Git repos && mkdir -p ${CODE_HOME}/$1
echo Fetching ${type} ${1}
for i in $(github ${type}s/${1}/repos | jq -r .[].name)
do
if [ ! -d ${CODE_HOME}/$1/$i ];
then
if [ "$(github repos/${1}/${i} | jq .parent)" == "null" ];
then
clone ${1} ${i} &
else
if [ "${2}" == "-f" ];
then
clone ${1} ${i} &
else
echo https://github.com/${1}/${i} is a Fork of Repo. Not Cloning.
fi
fi
else
echo You already have ${1}/$i
if [ "$2" == "-u" ]; then
cd ${CODE_HOME}/${1}/${i}
git fetch --all &
fi
fi
done