-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewproject.sh
executable file
·259 lines (208 loc) · 6.46 KB
/
newproject.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
# Script is used to automate the process of creating new project and push to new github repo
# New Project Script Requires a GITHUB_USERNAME, GITHUB_TOKEN for using the GITHUB API to create a new repo
# Link for Creating Personal Access Tokens https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line
# GITHUB_USERNAME and GITHUB_TOKEN can be passed as variables on commandline or by exporting the variables in the bash shell before running the script
# see https://www.seyiobaweya.tech/articles/2020-01-17/new-project-script/
# script has a jq dependency for reading json. this is only used to check if the repo currently exists in your github acct.
# Refinements are welcome
normal="\033[0m"
greentext="\033[32m"
danger="\e[91m"
warning="\e[93m"
INTERACTIVE=
REPO_NAME=
REGEX="0-9a-zA-Z_-"
DEFAULT_DIR="$HOME/projects"
PROJECT_DIR=${PROJECT_DIR:-$DEFAULT_DIR}
REPO_FOLDER=
HELP=
error (){
local msg=$1
echo -e $danger"$msg"$normal
}
warning (){
local msg=$1
echo -e $warning"$msg"$normal
}
success (){
local msg=$1
echo -e $greentext"$msg"$normal
}
usage(){
warning "__________________________________________________________________"
warning "\nNewproject script to create github repo on start of new project"
warning "------------------------------------------------------------------"
warning "Usage: newproject [ -i | -d | -f | -u | -t | -h ] repo_name\n"
echo " -i : Interactive Prompt (No Arg)"
echo " -d : Default Projects Directory"
echo " -f : Folder for Storing Repo"
echo " -u : Github Username"
echo " -t : Github Personal Access Token"
echo -e " -h : Help (No Arg)\n"
warning " Sample: newproject -i reponame: To Get Interactive prompt"
warning " Sample: newproject -f project folder reponame: To Specify a different Project folder from Repo name\n"
exit 1
}
# regex check
nameCheck(){
local repo_name=$1
if [[ $repo_name =~ [^$REGEX] ]]; then
error "Repo Naming Convention Error !!!"
error "Repo can only Contain letters, Numbers and an Underscore ... EXITING !!! \n"
usage
fi
}
currentgitdir(){
local folder=$1
if [[ -e "$folder/.git" ]]; then
error "Repo Folder: $REPO_FOLDER includes an Existing Repository ... EXITING"
usage
fi
}
clean_up (){
warning "cleaning up Repo folder $REPO_FOLDER"
rm -rf $REPO_FOLDER
sleep 1
}
exitCheck(){
local exitcode=$1
if [ $exitcode -ne 0 ]; then
error "Command Exited with 1\nExiting ...\n"
clean_up
exit 1
fi
}
while getopts "d:u:t:f:ih" option; do
case "$option" in
i ) INTERACTIVE=1;;
d ) PROJECT_DIR=$OPTARG;;
f ) REPO_FOLDER=$OPTARG;;
u ) GITHUB_USERNAME=$OPTARG;;
t ) GITHUB_TOKEN=$OPTARG;;
h ) HELP=1;;
\? ) usage
;;
esac
done
shift $((OPTIND -1))
if [[ $HELP = 1 ]]; then
usage
fi
if [ "$#" -ne 1 ]; then
error "Illegal number of parameters\n"
usage
fi
PROJECT_DIR=$(realpath $PROJECT_DIR)
REPO_NAME=$1
# check if Reponame is set here
if [[ ! -n $REPO_NAME ]]; then
error "invalid Argument, Pass Repo Name\n ...EXITING"
usage
else
nameCheck $REPO_NAME
fi
# PROJECT DIRECTORY SESSION
if [[ $INTERACTIVE = 1 ]]; then
echo -n "Enter project Default Folder: ($PROJECT_DIR) "
read answer
[[ -n $answer ]] && PROJECT_DIR=$(realpath $answer)
fi
#check if project file exists
if [[ ! -d $PROJECT_DIR ]]; then
echo "Projects Default Directory Does not exist"
echo -n "CREATE AT $PROJECT_DIR (Press y|Y for Yes, any other key for No) Default - n: "
read reply
if [[ $reply = "Y" || $reply = "y" ]]; then
mkdir -p "$PROJECT_DIR"
else
error "Exiting Script. Project directory $PROJECT_DIR does not Exist"
exit 1
fi
fi
# PROJECT DIRECTORY SESSION END
# PROJECT FOLDER SECTION
if [[ $INTERACTIVE = 1 ]]; then
echo -n "Enter project Folder- Repo name will be used if no default: ($REPO_FOLDER) "
read answer
REPO_FOLDER=$answer
fi
# check for repo name in command
if [[ ! -n $REPO_FOLDER ]]; then
REPO_FOLDER="$PROJECT_DIR/$REPO_NAME"
else
REPO_FOLDER="$PROJECT_DIR/$REPO_FOLDER"
fi
# Checking if Folder is already a repository
currentgitdir $REPO_FOLDER
if [[ ! -d $REPO_FOLDER ]]; then
warning "$REPO_FOLDER DOES NOT EXIST"
echo "creating Folder $REPO_FOLDER"
mkdir -p $REPO_FOLDER
sleep 1
fi
# END PPROJECT FOLDER
# GIT CRENDENTIALS CHECK
if [[ $INTERACTIVE = 1 ]]; then
echo -n "ENTER GITUSERNAME: ($GITHUB_USERNAME) "
read answer
[[ -n $answer ]] && GITHUB_USERNAME=$answer
echo -n "ENTER GIT TOKEN: "
read -s answer
[[ -n $answer ]] && GITHUB_TOKEN=$answer
fi
if [[ ! -n $GITHUB_TOKEN || ! -n $GITHUB_USERNAME ]]; then
error "GIT Crendentials not set"
clean_up
exit 1
fi
# GIT CONFIG
GITHUB_URL="https://api.github.com/users/${GITHUB_USERNAME}/repos"
GITHUB_ORIGIN_URL="https://${GITHUB_USERNAME}:${GITHUB_TOKEN}@github.com/${GITHUB_USERNAME}/${REPO_NAME}.git"
# Check if GITREPO ALREADY EXISTS
echo -n "\nChecking If Repo Already exists ..."
curl -s "$GITHUB_URL" > /dev/null 2>&1
EXIT_CODE=$?
exitCheck $EXIT_CODE
for row in $(curl -s "$GITHUB_URL" | jq -r '.[] | .name'); do
if [[ $row == "$REPO_NAME" ]]; then
error "Repo Name Already Exists"
exit 1
fi
done
success " done.\n"
echo -n "Creating GITHUB Repository $REPO_NAME ...\n"
curl -u "${GITHUB_USERNAME}:${GITHUB_TOKEN}" https://api.github.com/user/repos -d '{"name":"'$REPO_NAME'"}' > /dev/null 2>&1
EXIT_CODE=$?
exitCheck $EXIT_CODE
success " done.\n"
# Navigation to project dir
cd $REPO_FOLDER
# Initializing Git
echo -n "Initializing GIT REPO at $REPO_FOLDER ..."
git init > /dev/null 2>&1
success " done.\n"
# Creating README File
echo -n "Creating PROJECT README ..."
echo "### $REPO_NAME" > README.md
success " done.\n"
# Add .gitignore File
echo -n "Creating Gitignore file ..."
echo ".env" > .gitignore
success " done. \n"
# ADD Git Origin
echo -n "Adding Remote Origin ..."
git remote add origin $GITHUB_ORIGIN_URL > /dev/null 2>&1
success " done.\n"
# Stage Readme
git add .
# First Commit
echo -n "Creating First Commit ..."
git commit -m "Initial $REPO_NAME commit" > /dev/null 2>&1
success " done.\n"
# Push First Commit
echo -n "Pushing First commit to Remote ..."
git push -u origin master > /dev/null 2>&1
success " done."
echo "Repo ${REPO_NAME} Created"
echo "Repo can be accessed at https://github.com/${GITHUB_USERNAME}/${REPO_NAME}"