Repository navigation
Expand file tree
/
Copy pathsetup_github_mac.sh
More file actions
234 lines (197 loc) · 8.57 KB
/
Copy pathsetup_github_mac.sh
File metadata and controls
234 lines (197 loc) · 8.57 KB
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
#!/bin/bash
################################################################################
# GitHub Setup Script for macOS
# This script configures Git and GitHub on a Mac using username and password
# Note: GitHub deprecated password authentication. Use Personal Access Token (PAT)
# instead of password for better security.
################################################################################
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
################################################################################
# Step 1: Check if Homebrew is installed
################################################################################
check_homebrew() {
print_status "Checking for Homebrew..."
if ! command -v brew &> /dev/null; then
print_warning "Homebrew not found. Installing Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
print_status "Homebrew installed successfully"
else
print_status "Homebrew is already installed"
fi
}
################################################################################
# Step 2: Install Git if not present
################################################################################
install_git() {
print_status "Checking for Git..."
if ! command -v git &> /dev/null; then
print_warning "Git not found. Installing Git..."
brew install git
print_status "Git installed successfully"
else
print_status "Git is already installed: $(git --version)"
fi
}
################################################################################
# Step 3: Configure Git with user credentials
################################################################################
configure_git_credentials() {
echo ""
print_status "Configuring Git credentials..."
read -p "Enter your GitHub username: " github_username
read -sp "Enter your GitHub password or Personal Access Token: " github_password
echo ""
# Validate input
if [[ -z "$github_username" ]]; then
print_error "Username cannot be empty"
exit 1
fi
if [[ -z "$github_password" ]]; then
print_error "Password/Token cannot be empty"
exit 1
fi
# Configure git credentials
git config --global user.name "$github_username"
git config --global user.email "$github_username@github.com"
print_status "Git configured with username: $github_username"
}
################################################################################
# Step 4: Setup Git credentials helper (macOS Keychain)
################################################################################
setup_credentials_helper() {
print_status "Setting up Git credentials helper (macOS Keychain)..."
# Store credentials in macOS Keychain
git config --global credential.helper osxkeychain
print_status "Credentials helper configured for macOS Keychain"
}
################################################################################
# Step 5: Store credentials in Keychain
################################################################################
store_credentials_in_keychain() {
echo ""
print_status "Storing credentials in macOS Keychain..."
read -p "Enter your GitHub username: " github_username
read -sp "Enter your GitHub password or Personal Access Token: " github_password
echo ""
# Use git credential helper to store credentials
echo "https://$github_username:$github_password@github.com" | \
git credential-osxkeychain store
print_status "Credentials stored in macOS Keychain"
}
################################################################################
# Step 6: Test GitHub connection
################################################################################
test_github_connection() {
echo ""
print_status "Testing GitHub connection..."
if ssh -T git@github.com &> /dev/null; then
print_status "SSH connection to GitHub successful"
else
print_warning "SSH not configured. Testing HTTPS connection instead..."
git config --global url."https://github.com/".insteadOf git://github.com/
print_status "Configured to use HTTPS protocol"
fi
}
################################################################################
# Step 7: Clone test repository
################################################################################
clone_test_repo() {
echo ""
read -p "Do you want to clone the test repository (vigu01/courses)? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
test_repo_dir="$HOME/github_test_repo"
if [ -d "$test_repo_dir" ]; then
print_warning "Test directory already exists at $test_repo_dir"
read -p "Overwrite? (y/n): " -n 1 -r
echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
rm -rf "$test_repo_dir"
else
return
fi
fi
print_status "Cloning test repository..."
git clone https://github.com/vigu01/courses.git "$test_repo_dir"
print_status "Test repository cloned to: $test_repo_dir"
cd "$test_repo_dir"
print_status "Repository details:"
echo " Remote URL: $(git config --get remote.origin.url)"
echo " Branch: $(git branch --show-current)"
fi
}
################################################################################
# Step 8: Display Git configuration
################################################################################
display_config() {
echo ""
print_status "Current Git configuration:"
echo " User Name: $(git config --global user.name)"
echo " User Email: $(git config --global user.email)"
echo " Credential Helper: $(git config --global credential.helper)"
echo ""
}
################################################################################
# Step 9: Display security warning
################################################################################
display_security_warning() {
echo ""
echo -e "${YELLOW}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${YELLOW}║ SECURITY WARNING & BEST PRACTICES ║${NC}"
echo -e "${YELLOW}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo "⚠️ GitHub deprecated password authentication!"
echo ""
echo "Recommended: Use Personal Access Token (PAT) instead:"
echo " 1. Go to: https://github.com/settings/tokens"
echo " 2. Click 'Generate new token (classic)'"
echo " 3. Select scopes: repo, read:user, user:email"
echo " 4. Copy the token and use it as your password in this script"
echo ""
echo "For SSH setup (more secure):"
echo " ssh-keygen -t ed25519 -C 'your_email@example.com'"
echo " cat ~/.ssh/id_ed25519.pub | pbcopy"
echo " Paste at: https://github.com/settings/keys"
echo ""
echo "Never commit credentials or tokens to repositories!"
echo ""
}
################################################################################
# Main execution
################################################################################
main() {
echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║ GitHub Setup Script for macOS ║"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""
display_security_warning
check_homebrew
install_git
configure_git_credentials
setup_credentials_helper
store_credentials_in_keychain
test_github_connection
clone_test_repo
display_config
echo ""
print_status "GitHub setup completed successfully!"
echo ""
}
# Run main function
main "$@"