-
Notifications
You must be signed in to change notification settings - Fork 0
/
gswitch
executable file
·228 lines (198 loc) · 6.33 KB
/
gswitch
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
#!/usr/bin/env bash
# Author
author="Philip Mello <@Microsoft>"
# Version
version="1.2"
# License
license="MIT"
# Date
current_date=$(date +'%b %d, %Y')
# GitHub
github='https://github.com/PhilipMello/git-switch'
script="GitHub Account Switch"
description="Multiple Github Accounts into one CLI"
echo "
_____ _ _ _
/ ____| (_) | | | $version
__ _| (_____ ___| |_ ___| |__ $current_date
/ _. |\___ \ \ /\ / / | __/ __| |_ \ $author
| (_| |____) \ V V /| | || (__| | | | $license
\___ |_____/ \_/\_/ |_|\__\___|_| |_|
__/ |
|___/ $github
# --------------------------------------------------------------
# Script : $script
# Description: $description
# --------------------------------------------------------------
# How to use: Execute gswitch
# Exemples:
# Manual: gswitch -h OR gswitch --h OR gswitch --help
# Switch account using parameter: gswitch --account1
# --------------------------------------------------------------
"
WHITE=""
BLUE="\033[97;104m"
YELLOW="\033[97;103m"
CYAN="\033[97;106m"
MAGENTA="\033[97;45m"
GREEN="\033[97;102m"
RED="\033[97;41m"
ENDCOLOR="\e[0m"
function github_generate_sshkey() {
echo "Go to your GitHub Portal and add your SSH Key
'https://github.com/settings/ssh/new'
"
# Prompt user for the number of accounts to create
read -p "How many GitHub accounts do you want to create (0-9)? " num_accounts
# Validate the input for the number of accounts
if ! [[ "$num_accounts" =~ ^[0-9]+$ ]] || [ "$num_accounts" -lt 0 ] || [ "$num_accounts" -gt 9 ]; then
echo "Invalid number. Please enter a number between 0 and 9."
exit 1
fi
# Prompt user to create the specified number of accounts
for ((i = 0; i < num_accounts; i++)); do
read -p "Type your GitHub email account for account #$i: " github_email_account
# Validate email format (basic check)
if [[ ! "$github_email_account" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]; then
echo "Invalid email format. Please try again."
continue
fi
# Generate the SSH key with the provided email and a custom filename
ssh-keygen -t ed25519 -C "$github_email_account" -f "$HOME/.ssh/id_ed25519_account_$i"
echo -e "
+-----------------------------------------------------------+
|${GREEN}SSH key generated for: $github_email_account${ENDCOLOR}
|${GREEN}Key saved as: $HOME/.ssh/id_ed25519_account_$i${ENDCOLOR}
+-----------------------------------------------------------+
"
done
}
function github_connection_test() {
# Github test connection
echo "Github connection testing..."
ssh -T git@github.com
}
function github_set_account() {
# Github set account
read -p "Enter your Github Account Name": github_set_account_name
read -p "Enter your Github Account Email": github_set_account_email
git config user.name "$github_set_account_name"
git config user.email "$github_set_account_email"
}
function github_account_switch() {
# Github Account Switch (2 Accounts)
echo -e "You're logged in as:\n"
ssh -T git@github.com
emails=()
files=()
while IFS=: read -r file email; do
emails+=("$email")
files+=("$(basename "$file")")
done < <(grep -H -Eo '[A-Za-z0-9._%+-]+@([A-Za-z0-9._%+-]+)?' ~/.ssh/id_* 2>/dev/null)
if [ ${#emails[@]} -eq 0 ]; then
echo "No accounts found."
exit 1
fi
echo "Select an email:"
for i in "${!emails[@]}"; do
echo "$((i+1)). ${emails[i]}"
done
read -p "Enter the number of your account: " choice
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt "${#emails[@]}" ]; then
echo "Invalid choice. Exiting."
exit 1
fi
selected_email=${emails[$((choice-1))]}
selected_file=${files[$((choice-1))]}
echo -e "Github Account $selected_email Selected!\n"
cp ~/.ssh/$selected_file ~/.ssh/id_ed25519.pub
ssh -T git@github.com
}
function github_fix_permissions() {
# Git fix files permissions
echo "Fixing SSH file permissions..."
chown $USER:$USER -R ~/.ssh
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/id_*
echo -e "
+-----------------------------------------------------------+
|${GREEN}Files permissions has been fixed${ENDCOLOR}
+-----------------------------------------------------------+
"
}
function github_show_config() {
echo "Run gSwitch in git directory level"
cat .git/config | grep -E "name|email"
}
function github_show_accounts() {
# Show account in ~/.ssh
cat ~/.ssh/id_* | grep -Eo '[A-Za-z0-9._%+-]+@([A-Za-z0-9._%+-]+)?' | while read -r email; do
echo "-----------------------------------------"
echo "| Email: $email |"
echo "-----------------------------------------"
done
}
# <-- Manual - BEGIN
manual() {
echo "Generate 2 SSH Keys and rename Github account to
id_ed25519_account1 and Github account 2 to id_ed25519_account2
in ~/.ssh
"
echo -e "Parameters:
--account1 Switch to GitHub Account 1
--account2 Switch to GitHub Account 2
"
exit 0
}
# Manual - END -->
if [[ $1 == "-h" || $1 == "--help" || $1 == "--h" ]]; then
manual
exit 1
elif [[ $1 == "--account1" ]]; then
cp ~/.ssh/id_ed25519_account1 ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519_account1.pub ~/.ssh/id_ed25519.pub
echo -e "Github Account #1 has been selected!\n"
ssh -T git@github.com
exit 0
elif [[ $1 == "--account2" ]]; then
cp ~/.ssh/id_ed25519_account2 ~/.ssh/id_ed25519
cp ~/.ssh/id_ed25519_account2.pub ~/.ssh/id_ed25519.pub
echo -e "Github Account #2 has been selected!\n"
ssh -T git@github.com
exit 0
fi
echo "Choose an option:"
echo "1. Switch GitHub Account"
echo "2. Generate SSH Key"
echo "3. Test Github SSH connection"
echo "4. Set Github account"
echo "5. Fix SSH file permissions"
echo "6. Show current GitHub config"
echo "7. Show Account in: ~/.ssh"
read option
case $option in
1)
github_account_switch
;;
2)
github_generate_sshkey
;;
3)
github_connection_test
;;
4)
github_set_account
;;
5)
github_fix_permissions
;;
6)
github_show_config
;;
7)
github_show_accounts
;;
*)
echo "Invalid option"
;;
esac