-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitRepo.sh
102 lines (88 loc) · 2.55 KB
/
gitRepo.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
#!/usr/bin/bash
storePAT='y'
function prettyPrint () {
echo "-------------------------------------------------------"
echo "$1"
echo "-------------------------------------------------------"
}
function getCredentials(){
touch credentials.env
chmod 700 credentials.env
prettyPrint "Kindly enter your GitHub username: "
read USERNAME
prettyPrint "Kindly enter your GitHub Personal Access Token: "
read -s PAT
prettyPrint "Do you want to store your Personal Access Token in credentials.env file? (y/n)"
read storePAT
echo "USERNAME=$USERNAME" > credentials.env
if [[ $storePAT == 'y' ]]; then
echo "PAT=$PAT" >> credentials.env
fi
}
function getRepoDetails(){
prettyPrint "Name of the GitHUb repository: "
read name
prettyPrint "Do you want a private repository? (y/n)"
read privateChoice
if [[ $privateChoice == 'y' ]]; then
isPrivate='true'
elif [[ $privateChoice == 'n' ]]; then
isPrivate='false'
else prettyPrint "Something went wrong.
Kindly re-enter Repository Details."
getRepoDetails
fi
}
function createNewRepo(){
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $PAT" \
-H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/user/repos \
-d "{\"name\":\"$name\", \"private\":$isPrivate }"
}
function checkValidResponse(){
response=$( createNewRepo )
if [[ "$response" =~ "\"message\": \"name already exists on this account\"" ]]; then
prettyPrint "This repository already exists...
Kindly enter valid Repository name,
So that it doesn't match any of the existing repositories."
getRepoDetails
checkValidResponse
elif [[ "$response" =~ "\"message\": \"Bad credentials\"" ]]; then
prettyPrint "Invalid Credentials...
Kindly re-enter the Credentials."
getCredentials
getRepoDetails
checkValidResponse
fi
}
function createLocalDirectory(){
link="[email protected]:$USERNAME/$name.git"
mkdir $name
cp ./credentials.env ./$name
cp ./gitRepo.sh ./$name
rm ./credentials.env
cd $name
git init
echo 'gitRepo.sh' > .gitignore
echo 'credentials.env' >> .gitignore
echo "# $name" > README.md
git add .
git commit -m "initial commit"
git branch -M main
git remote add origin $link
git push -u origin main
}
function main(){
prettyPrint "
Welcome to Git Automator
"
prettyPrint "Before we get started!
We wanna know you a little better"
getCredentials
getRepoDetails
checkValidResponse
createLocalDirectory
}
main