-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-init.sh
executable file
·70 lines (55 loc) · 1.76 KB
/
github-init.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
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
TEMP=$(getopt -o 'hig:l:' --long "help,init,gitignore:,license:" -n $(basename $0) -- "$@")
EXPANDED=\"$(echo ${TEMP} | head -c 49)\"
if [ "$EXPANDED" = '"-- hig:l: --long help,init,gitignore:,license: -n"' ]
then
TEMP=$(getopt hig:l: $@)
fi
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true; do
case "$1" in
-h | --help ) HELP=true; shift ;;
-i | --init ) INIT=true; shift ;;
-g | --gitignore ) IGNORE_FILE_TYPE="$2"; shift 2 ;;
-l | --license ) LICENSE_NAME="$2"; shift 2 ;;
-- ) shift; break ;;
esac
done
if [ "$HELP" = "true" ]
then
echo "usage: github-init.sh [-higl] repo-name [repo-description]"
echo " -h Display this help"
echo " -i Initialize repo with a README"
echo " -g <ignore template> Initialize repo with a gitignore file"
echo " -l <license template> Initialize repo with a license"
exit 0
fi
NAME="$1"
DESCRIPTION="$2"
if [ \( -z "$GH_USERNAME" \) -o \( -z "$GH_API_TOKEN" \) ]
then
echo "You must set GH_USERNAME and GH_API_TOKEN."
exit 1
fi
if [ -z "$NAME" ]
then
echo "You must provide a name for your repo."
exit 1
fi
PAYLOAD="{
\"name\": \"$NAME\",
\"description\": \"$DESCRIPTION\",
\"homepage\": \"https://github.com/$GH_USERNAME/$NAME\",
\"auto_init\": ${INIT-false},
\"gitignore_template\": \"$IGNORE_FILE_TYPE\",
\"license_template\": \"$LICENSE_NAME\"
}"
curl -d "${PAYLOAD}" -H "Authorization: token ${GH_API_TOKEN}" https://api.github.com/user/repos
if [ ! -d $NAME ]
then
echo "Repo $NAME successfully created!"
git clone "[email protected]:${GH_USERNAME}/${NAME}.git"
else
echo "A directory '$NAME' already exists..."
fi