-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
608 lines (518 loc) · 18.1 KB
/
Copy pathsetup.sh
File metadata and controls
608 lines (518 loc) · 18.1 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/bin/bash
# setup.sh - Git Environment Setup for *nix
# Part of the Git Environment Setup Tool
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
check_git() {
log_info "Checking for Git..."
if command -v git &> /dev/null; then
local git_version=$(git --version)
log_success "Git is already installed: $git_version"
return 0
else
log_error "Git is not installed."
install_git
fi
}
install_git() {
log_info "Attempting to install Git..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if command -v apt &> /dev/null; then
sudo apt update && sudo apt install -y git
elif command -v dnf &> /dev/null; then
sudo dnf install -y git
elif command -v yum &> /dev/null; then
sudo yum install -y git
elif command -v pacman &> /dev/null; then
sudo pacman -S --noconfirm git
else
log_error "Unsupported package manager. Please install Git manually."
exit 1
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
if command -v brew &> /dev/null; then
brew install git
else
log_error "Homebrew not found. Please install Git manually."
exit 1
fi
else
log_error "Unsupported OS for automated install. Please install Git manually."
exit 1
fi
# Verify installation
if command -v git &> /dev/null; then
log_success "Git installed successfully!"
else
log_error "Git installation failed."
exit 1
fi
}
configure_identity() {
log_info "Configuring Global Git Identity..."
local current_name=$(git config --global user.name)
local current_email=$(git config --global user.email)
local change_identity="y"
if [ ! -z "$current_name" ] || [ ! -z "$current_email" ]; then
log_info "Current identity: $current_name <$current_email>"
echo "Do you want to change your name/email? (y/N): "
read change_identity
fi
if [[ "$change_identity" =~ ^[Yy]$ ]]; then
echo "Enter your full name: "
read name
while [ -z "$name" ]; do
log_error "Name cannot be empty."
echo "Enter your full name: "
read name
done
echo "Enter your email address: "
read email
while [[ ! "$email" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; do
log_error "Invalid email format."
echo "Enter your email address: "
read email
done
git config --global user.name "$name"
git config --global user.email "$email"
log_success "Global identity configured: $(git config --global user.name) <$(git config --global user.email)>"
fi
# --- Part 2: Global SSH Key ---
local current_ssh=$(git config --global core.sshCommand | sed 's/ssh -i //')
log_info "Current Global SSH Key: ${current_ssh:-System Default}"
echo "Do you want to configure the global SSH key? (y/N): "
read configure_ssh
if [[ "$configure_ssh" =~ ^[Yy]$ ]]; then
echo "1. Select existing key from .ssh"
echo "2. Generate new key"
echo "3. Use System Default (Clear custom config)"
echo "4. Skip"
echo -n "Select an option [1-4]: "
read ssh_opt
case $ssh_opt in
1)
log_info "Existing SSH Keys:"
local keys=$(list_ssh_keys)
if [ -z "$keys" ]; then
log_error "No keys found."
else
list_ssh_keys | nl
echo -n "Enter the number: "
read key_num
local selected_key=$(list_ssh_keys | sed -n "${key_num}p")
if [ ! -z "$selected_key" ]; then
git config --global core.sshCommand "ssh -i ~/.ssh/$selected_key"
log_success "Global SSH key set to: ~/.ssh/$selected_key"
fi
fi
;;
2)
generate_new_ssh_key
echo -n "Enter the name of the key you just created (e.g. id_ed25519_work): "
read key_name
local key_path="$HOME/.ssh/id_ed25519_$key_name"
if [ -f "$key_path" ]; then
git config --global core.sshCommand "ssh -i ~/.ssh/id_ed25519_$key_name"
log_success "Global SSH key set to: ~/.ssh/id_ed25519_$key_name"
else
log_error "Key not found: $key_path"
fi
;;
3)
git config --global --unset core.sshCommand
log_success "Reverted to System Default SSH key."
;;
esac
fi
}
setup_ssh() {
log_info "Setting up SSH Keys..."
local ssh_dir="$HOME/.ssh"
local key_file="$ssh_dir/id_ed25519"
if [ -f "$key_file" ]; then
log_info "Existing SSH key found at $key_file"
echo "Do you want to use this key? (Y/n): "
read use_existing
if [[ ! "$use_existing" =~ ^[Nn]$ ]]; then
show_public_key "$key_file.pub"
return 0
fi
fi
log_info "Generating a new Ed25519 SSH key..."
mkdir -p "$ssh_dir"
chmod 700 "$ssh_dir"
local email=$(git config --global user.email)
ssh-keygen -t ed25519 -C "$email" -f "$key_file" -N ""
log_success "SSH key generated successfully."
show_public_key "$key_file.pub"
}
list_ssh_keys() {
local target_dir="${1:-$HOME/.ssh}"
if [ ! -d "$target_dir" ]; then
return
fi
# List files in .ssh, ignoring known non-key files and public keys
# We look for files that are NOT directories
find "$target_dir" -maxdepth 1 -type f | while read -r file; do
local filename=$(basename "$file")
# Filter out common non-private-key files
if [[ "$filename" == *.pub ]] || \
[[ "$filename" == "known_hosts" ]] || \
[[ "$filename" == "known_hosts.old" ]] || \
[[ "$filename" == "authorized_keys" ]] || \
[[ "$filename" == "config" ]] || \
[[ "$filename" == "environment" ]]; then
continue
fi
# Optional: Check if file starts with "---- BEGIN" to be sure it's a key?
# For now, sticking to file exclusion as implied by spec/test
echo "$filename"
done
}
generate_new_ssh_key() {
local target_dir="${1:-$HOME/.ssh}"
local email_arg="$2"
mkdir -p "$target_dir"
chmod 700 "$target_dir"
echo "Enter a name for the new key (e.g., 'work', 'personal')."
echo "The key will be saved as 'id_ed25519_<name>': "
read key_suffix
while [ -z "$key_suffix" ]; do
log_error "Key name cannot be empty."
read key_suffix
done
local key_file="$target_dir/id_ed25519_$key_suffix"
local email=${email_arg:-$(git config --global user.email)}
log_info "Generating key: $key_file for $email"
ssh-keygen -t ed25519 -C "$email" -f "$key_file" -N ""
log_success "SSH key generated."
show_public_key "$key_file.pub"
}
show_ssh_walkthrough() {
log_info "Guided Walkthrough: SSH Key Management"
echo ""
echo "This tool now supports multiple SSH keys mapped to specific folders."
echo ""
echo "1. Create or Select a key in 'Manage SSH Keys' or during 'Add Profile'."
echo "2. Copy the public key content (starting with 'ssh-ed25519...')."
echo "3. Log in to your Git provider (GitHub, GitLab, etc.)."
echo "4. Go to 'Settings' -> 'SSH and GPG keys' and click 'New SSH Key'."
echo "5. Give it a title and paste your key."
echo ""
echo "When you enter a folder linked to a profile with an assigned key,"
echo "Git will automatically use that specific key for authentication."
echo ""
echo "Press Enter when you have completed these steps..."
read
}
show_public_key() {
local pub_key_file=$1
log_info "Your Public SSH Key:"
echo "----------------------------------------------------------------"
cat "$pub_key_file"
echo "----------------------------------------------------------------"
}
get_git_profiles() {
# Parses git config for includeIf directives
# Format: includeIf.gitdir:<dir>.path=<config_file>
git config --global --list | grep "includeIf.gitdir:" | while read -r line; do
# Extract dir (remove 'includeIf.gitdir:' and everything after '.path=')
local dir=${line#includeIf.gitdir:}
dir=${dir%%.path=*}
# Extract path (remove everything up to '=')
local path=${line#*=}
echo "$dir:$path"
done
}
list_profiles() {
log_info "Current Folder-Based Profiles:"
# Check if we have any output from get_git_profiles
if [ -z "$(get_git_profiles)" ]; then
echo " No profiles found."
return
fi
local count=1
get_git_profiles | while read -r line; do
local dir=${line%%:*}
local config=${line#*:}
echo " $count. $dir -> $config"
((count++))
done
}
save_git_profile() {
local target_dir=$1
local name=$2
local email=$3
local profile_name=$4
local ssh_key=$5
local config_file="$HOME/.gitconfig-$profile_name"
log_info "Creating profile config at $config_file..."
cat > "$config_file" <<EOF
[user]
name = $name
email = $email
EOF
if [ ! -z "$ssh_key" ]; then
log_info "Assigning SSH key $ssh_key to profile..."
# Ensure path is absolute for sshCommand
local ssh_key_path="$HOME/.ssh/$ssh_key"
cat >> "$config_file" <<EOF
[core]
sshCommand = ssh -i ~/.ssh/$ssh_key
EOF
fi
log_info "Updating global .gitconfig with includeIf..."
# Ensure directory path ends with / for includeIf
[[ "$target_dir" != */ ]] && target_dir="$target_dir/"
git config --global "includeIf.gitdir:$target_dir.path" "$config_file"
log_success "Profile saved and linked to $target_dir"
}
add_profile() {
log_info "--- Add New Git Profile ---"
echo "Enter the target directory path (e.g., ~/Work): "
read target_dir
# Expand ~ if present
target_dir="${target_dir/#\~/$HOME}"
if [ ! -d "$target_dir" ]; then
log_error "Directory does not exist: $target_dir"
return 1
fi
echo "Enter user name for this profile: "
read name
echo "Enter email address for this profile: "
read email
# Generate a simple profile name from the directory
local profile_name=$(basename "$target_dir" | tr '[:upper:]' '[:lower:]')
local selected_ssh_key=""
echo "Do you want to assign an SSH key to this profile? (y/N): "
read assign_ssh
if [[ "$assign_ssh" =~ ^[Yy]$ ]]; then
echo "1. Select existing key"
echo "2. Generate new key"
echo "3. Skip"
echo -n "Select an option [1-3]: "
read ssh_opt
case $ssh_opt in
1)
log_info "Existing SSH Keys:"
local keys=$(list_ssh_keys)
if [ -z "$keys" ]; then
log_error "No keys found. You may want to generate one."
else
list_ssh_keys | nl
echo -n "Enter the number: "
read key_num
selected_ssh_key=$(list_ssh_keys | sed -n "${key_num}p")
fi
;;
2)
generate_new_ssh_key "$HOME/.ssh" "$email"
# Assume the new key is the one we want.
# We need generate_new_ssh_key to return or set a variable.
# For now, let's just ask for the name again or improve the flow.
echo -n "Enter the name of the key you just created (e.g. id_ed25519_work): "
read selected_ssh_key
;;
esac
fi
save_git_profile "$target_dir" "$name" "$email" "$profile_name" "$selected_ssh_key"
}
remove_profile() {
log_info "--- Remove Git Profile ---"
local profiles=$(get_git_profiles)
if [ -z "$profiles" ]; then
log_error "No profiles found to remove."
return
fi
list_profiles
echo "Enter the number of the profile to remove: "
read num
local selected=$(get_git_profiles | sed -n "${num}p")
if [ -z "$selected" ]; then
log_error "Invalid selection."
return
fi
local dir=${selected%%:*}
local config=${selected#*:}
log_info "Removing profile for $dir..."
git config --global --unset "includeIf.gitdir:$dir.path"
if [ -f "$config" ]; then
rm "$config"
log_success "Deleted config file: $config"
fi
log_success "Profile removed."
}
edit_profile() {
log_info "--- Edit Git Profile ---"
local profiles=$(get_git_profiles)
if [ -z "$profiles" ]; then
log_error "No profiles found to edit."
return
fi
list_profiles
echo "Enter the number of the profile to edit: "
read num
local selected=$(get_git_profiles | sed -n "${num}p")
if [ -z "$selected" ]; then
log_error "Invalid selection."
return
fi
local dir=${selected%%:*}
local config=${selected#*:}
log_info "Editing profile for $dir (stored in $config)"
local current_name=$(git config -f "$config" user.name)
local current_email=$(git config -f "$config" user.email)
local current_ssh=$(git config -f "$config" core.sshCommand | sed 's/ssh -i //')
echo "Enter new name [$current_name]: "
read name
name=${name:-$current_name}
echo "Enter new email [$current_email]: "
read email
email=${email:-$current_email}
git config -f "$config" user.name "$name"
git config -f "$config" user.email "$email"
echo "Current SSH Key: ${current_ssh:-None}"
echo "Do you want to change SSH key? (y/N): "
read change_ssh
if [[ "$change_ssh" =~ ^[Yy]$ ]]; then
log_info "Existing SSH Keys:"
list_ssh_keys | nl
echo "0. Remove SSH key assignment"
echo -n "Enter the number: "
read key_num
if [ "$key_num" == "0" ]; then
git config -f "$config" --unset core.sshCommand
else
local selected_ssh_key=$(list_ssh_keys | sed -n "${key_num}p")
if [ ! -z "$selected_ssh_key" ]; then
git config -f "$config" core.sshCommand "ssh -i ~/.ssh/$selected_ssh_key"
fi
fi
fi
log_success "Profile updated."
}
manage_profiles() {
while true; do
log_info "--- Manage Folder-Based Git Profiles ---"
echo "1. List Profiles"
echo "2. Add New Profile"
echo "3. Edit Existing Profile"
echo "4. Remove Profile"
echo "5. Back to Main Menu"
echo -n "Select an option [1-5]: "
read choice
case $choice in
1) list_profiles ;;
2) add_profile ;;
3) edit_profile ;;
4) remove_profile ;;
5) return ;;
*) log_error "Invalid option." ;;
esac
echo ""
done
}
delete_ssh_key() {
log_info "--- Delete SSH Key ---"
local keys=$(list_ssh_keys)
if [ -z "$keys" ]; then
log_error "No keys found."
return
fi
list_ssh_keys | nl
echo -n "Select the key number to delete: "
read key_num
local selected_key=$(list_ssh_keys | sed -n "${key_num}p")
if [ -z "$selected_key" ]; then
log_error "Invalid selection."
return
fi
echo "WARNING: This will permanently delete '$selected_key' and its public key."
echo "Are you sure? (type 'yes' to confirm): "
read confirm
if [ "$confirm" == "yes" ]; then
local key_path="$HOME/.ssh/$selected_key"
rm -f "$key_path" "$key_path.pub"
log_success "Deleted key: $selected_key"
else
log_info "Deletion cancelled."
fi
}
manage_ssh_keys() {
while true; do
log_info "--- Manage SSH Keys ---"
echo "1. List Existing Keys"
echo "2. Generate New SSH Key"
echo "3. View Public Key"
echo "4. Delete SSH Key"
echo "5. Back to Main Menu"
echo -n "Select an option [1-5]: "
read ssh_choice
case $ssh_choice in
1)
log_info "Existing SSH Keys:"
list_ssh_keys
;;
2) generate_new_ssh_key ;;
3)
log_info "Select a key to view its public content:"
local keys=$(list_ssh_keys)
if [ -z "$keys" ]; then
log_error "No keys found."
else
list_ssh_keys | nl
echo -n "Enter the number: "
read key_num
local selected_key=$(list_ssh_keys | sed -n "${key_num}p")
if [ -z "$selected_key" ]; then
log_error "Invalid selection."
else
show_public_key "$HOME/.ssh/$selected_key.pub"
fi
fi
;;
4) delete_ssh_key ;;
5) return ;;
*) log_error "Invalid option." ;;
esac
echo ""
done
}
main() {
log_info "Starting Git Environment Setup on $(uname -s)..."
check_git
while true; do
echo "--- Main Menu ---"
echo "1. Configure Global Identity"
echo "2. Setup SSH Keys"
echo "3. Manage Folder-Based Profiles"
echo "4. Manage SSH Keys"
echo "5. Exit"
echo -n "Select an option [1-5]: "
read main_choice
case $main_choice in
1) configure_identity ;;
2) setup_ssh; show_ssh_walkthrough ;;
3) manage_profiles ;;
4) manage_ssh_keys ;;
5) log_success "Exiting. Happy coding!"; exit 0 ;;
*) log_error "Invalid option." ;;
esac
done
}
# Only run main if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi