-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitssh-init.sh
More file actions
583 lines (495 loc) · 17.7 KB
/
gitssh-init.sh
File metadata and controls
583 lines (495 loc) · 17.7 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
#!/bin/sh
#================================================================#
# GitSSH INITIALIZATION MODULE
# Functions for setting up and validating the GitSSH system
#================================================================#
#================================================================#
# DEPENDENCY INSTALLATION HELPERS
#================================================================#
# Show installation instructions for jq
_show_jq_installation() {
printf "jq is required but not installed\n"
printf "Install with:\n"
os=$(_detect_os)
case "$os" in
linux)
if command -v apt >/dev/null 2>&1; then
printf " Ubuntu/Debian: sudo apt install jq\n"
fi
if command -v yum >/dev/null 2>&1; then
printf " CentOS/RHEL: sudo yum install jq\n"
fi
if command -v dnf >/dev/null 2>&1; then
printf " Fedora: sudo dnf install jq\n"
fi
if command -v pacman >/dev/null 2>&1; then
printf " Arch: sudo pacman -S jq\n"
fi
;;
macos)
if command -v brew >/dev/null 2>&1; then
printf " macOS (Homebrew): brew install jq\n"
else
printf " macOS: Install Homebrew first, then 'brew install jq'\n"
fi
;;
*)
manager_hint=$(_get_package_manager_hint)
printf " %s jq\n" "$manager_hint"
;;
esac
}
# Check and install dependencies
_check_and_setup_dependencies() {
missing_deps=""
# Check for jq
if ! command -v jq >/dev/null 2>&1; then
missing_deps="jq"
fi
# Check for git
if ! command -v git >/dev/null 2>&1; then
missing_deps="$missing_deps git"
fi
# Check for ssh
if ! command -v ssh >/dev/null 2>&1; then
missing_deps="$missing_deps openssh-client"
fi
if [ -n "$missing_deps" ]; then
_print_error "Missing required dependencies: $missing_deps"
for dep in $missing_deps; do
case "$dep" in
jq) _show_jq_installation ;;
git) printf "Install Git from: https://git-scm.com/downloads\n" ;;
openssh-client) printf "Install OpenSSH client via your package manager\n" ;;
esac
done
return 1
fi
return 0
}
#================================================================#
# CONFIGURATION FILE INITIALIZATION
#================================================================#
# Initialize sessions configuration file
_init_sessions_config() {
if [ ! -f "$GIT_SSH_CONFIG_FILE" ]; then
printf '{}' > "$GIT_SSH_CONFIG_FILE"
_print_success "Created sessions config: $GIT_SSH_CONFIG_FILE"
return 0
else
# Validate existing file
if _validate_json_file "$GIT_SSH_CONFIG_FILE"; then
printf "Sessions config exists: %s\n" "$GIT_SSH_CONFIG_FILE"
return 0
else
_print_warning "Invalid sessions config detected, recreating..."
_backup_file "$GIT_SSH_CONFIG_FILE" "invalid-backup"
printf '{}' > "$GIT_SSH_CONFIG_FILE"
_print_success "Recreated sessions config"
return 0
fi
fi
}
# Initialize users configuration file
_init_users_config() {
create_users_config=false
if [ ! -f "$GIT_SSH_USERS_FILE" ]; then
create_users_config=true
printf "Creating new users config...\n"
elif ! _validate_json_file "$GIT_SSH_USERS_FILE" ".users"; then
create_users_config=true
printf "Repairing invalid users config...\n"
_backup_file "$GIT_SSH_USERS_FILE" "backup"
printf "Backed up existing config to %s.backup\n" "$GIT_SSH_USERS_FILE"
fi
if [ "$create_users_config" = "true" ]; then
_create_default_users_config
else
printf "Users config exists: %s\n" "$GIT_SSH_USERS_FILE"
fi
}
# Create default users configuration
_create_default_users_config() {
# Try to detect existing SSH hosts
printf "Detecting SSH hosts from ~/.ssh/config...\n"
detected_users=""
if [ -f "$HOME/.ssh/config" ]; then
while IFS= read -r line; do
case "$line" in
Host\ github-*)
user=$(printf "%s" "$line" | sed 's/Host github-//' | awk '{print $1}')
if [ -n "$user" ] && _validate_username "$user"; then
detected_users="$detected_users $user"
fi
;;
esac
done < "$HOME/.ssh/config"
fi
# Create config file
temp_file=$(_create_temp_file)
{
printf '{\n'
printf ' "users": {\n'
if [ -n "$detected_users" ]; then
printf "Found SSH hosts for: %s\n" "$detected_users" >&2
first=true
for user in $detected_users; do
if [ "$first" = "false" ]; then
printf ',\n'
fi
printf ' "%s": {"name": "%s", "email": "%[email protected]", "ssh_host": "github-%s"}' \
"$user" "$user" "$user" "$user"
first=false
done
printf '\n'
else
printf "Creating template with example users...\n" >&2
printf ' "user1": {"name": "user1", "email": "[email protected]", "ssh_host": "github-user1"},\n'
printf ' "user2": {"name": "user2", "email": "[email protected]", "ssh_host": "github-user2"}\n'
fi
printf ' }\n'
printf '}\n'
} > "$temp_file"
if _safe_file_replace "$temp_file" "$GIT_SSH_USERS_FILE"; then
_print_success "Created users config: $GIT_SSH_USERS_FILE"
printf "Please edit the file to set correct email addresses:\n"
printf " nano %s\n" "$GIT_SSH_USERS_FILE"
return 0
else
_print_error "Failed to create users config"
rm -f "$temp_file"
return 1
fi
}
#================================================================#
# CONFIGURATION VALIDATION
#================================================================#
# Validate configuration files
_validate_config() {
need_init=false
validation_errors=""
# Check if config files exist
if [ ! -f "$GIT_SSH_CONFIG_FILE" ]; then
validation_errors="$validation_errors missing_sessions_config"
need_init=true
elif ! _validate_json_file "$GIT_SSH_CONFIG_FILE"; then
validation_errors="$validation_errors invalid_sessions_config"
need_init=true
fi
if [ ! -f "$GIT_SSH_USERS_FILE" ]; then
validation_errors="$validation_errors missing_users_config"
need_init=true
elif ! _validate_json_file "$GIT_SSH_USERS_FILE" ".users"; then
validation_errors="$validation_errors invalid_users_config"
need_init=true
fi
# Check dependencies
if ! _check_dependencies 2>/dev/null; then
validation_errors="$validation_errors missing_dependencies"
need_init=true
fi
if [ "$need_init" = "true" ]; then
if [ -n "$validation_errors" ]; then
printf "Configuration issues detected: %s\n" "$(printf "%s" "$validation_errors" | tr '_' ' ')"
fi
printf "Running initialization...\n"
git_ssh_init
fi
}
# Validate and repair configuration
git_ssh_validate() {
printf "Validating GitSSH Configuration\n"
printf "================================\n"
issues_found=0
# Check dependencies
printf "Dependencies:\n"
if command -v jq >/dev/null 2>&1; then
_print_success " jq: Available"
else
_print_error " jq: Missing"
_show_jq_installation
issues_found=$((issues_found + 1))
fi
if command -v git >/dev/null 2>&1; then
_print_success " git: Available"
else
_print_error " git: Missing"
issues_found=$((issues_found + 1))
fi
if command -v ssh >/dev/null 2>&1; then
_print_success " ssh: Available"
else
_print_error " ssh: Missing"
issues_found=$((issues_found + 1))
fi
# Check configuration files
printf "\nConfiguration Files:\n"
if [ -f "$GIT_SSH_CONFIG_FILE" ]; then
if _validate_json_file "$GIT_SSH_CONFIG_FILE"; then
_print_success " Sessions config: Valid"
else
_print_error " Sessions config: Invalid JSON"
issues_found=$((issues_found + 1))
fi
else
_print_warning " Sessions config: Missing"
issues_found=$((issues_found + 1))
fi
if [ -f "$GIT_SSH_USERS_FILE" ]; then
if _validate_json_file "$GIT_SSH_USERS_FILE" ".users"; then
_print_success " Users config: Valid"
# Show user count
if _check_dependencies 2>/dev/null; then
user_count=$(jq '.users | length' "$GIT_SSH_USERS_FILE" 2>/dev/null)
printf " Users configured: %s\n" "${user_count:-0}"
fi
else
_print_error " Users config: Invalid structure"
issues_found=$((issues_found + 1))
fi
else
_print_warning " Users config: Missing"
issues_found=$((issues_found + 1))
fi
# Check SSH configuration
printf "\nSSH Configuration:\n"
ssh_hosts=$(_get_ssh_hosts)
if [ -n "$ssh_hosts" ]; then
host_count=$(printf "%s" "$ssh_hosts" | wc -w)
_print_success " SSH hosts found: $host_count"
for host in $ssh_hosts; do
if _test_ssh_connection "$host" 2; then
_print_success " $host: Connected"
else
_print_warning " $host: Connection failed"
fi
done
else
_print_warning " No GitHub SSH hosts found in ~/.ssh/config"
printf " Add SSH host configurations to use SSH features\n"
fi
# Summary
printf "\nValidation Summary:\n"
if [ "$issues_found" -eq 0 ]; then
_print_success "Configuration is valid and ready to use"
else
_print_warning "Found $issues_found issue(s)"
printf "Run 'gitssh init' to fix configuration issues\n"
fi
return "$issues_found"
}
#================================================================#
# MAIN INITIALIZATION FUNCTION
#================================================================#
# Initialize configuration files with default structure
git_ssh_init() {
printf "Initializing GitSSH User Session Manager...\n"
printf "=============================================\n"
# Check and setup dependencies
if ! _check_and_setup_dependencies; then
return 1
fi
# Initialize configuration files
_init_sessions_config || return 1
_init_users_config || return 1
# Validate final configuration
if _validate_final_config; then
_print_success "Initialization complete!"
printf "Use 'gitssh user add' to add more users interactively\n"
printf "Use 'gitssh help' for full command reference\n"
return 0
else
_print_error "Initialization failed validation"
return 1
fi
}
# Final configuration validation after initialization
_validate_final_config() {
printf "Validating configuration...\n"
# Check sessions config
if ! _validate_json_file "$GIT_SSH_CONFIG_FILE"; then
_print_error "Sessions configuration is invalid"
return 1
fi
# Check users config
if ! _validate_json_file "$GIT_SSH_USERS_FILE" ".users"; then
_print_error "Users configuration is invalid"
return 1
fi
# Show configuration summary
if _check_dependencies 2>/dev/null; then
user_count=$(jq '.users | length' "$GIT_SSH_USERS_FILE" 2>/dev/null)
printf "Configuration valid with %s user(s)\n" "${user_count:-0}"
if [ "$user_count" -gt 0 ]; then
printf "Configured users:\n"
jq -r '.users | to_entries[] | " * " + .key + ": " + .value.email' "$GIT_SSH_USERS_FILE" 2>/dev/null
fi
fi
return 0
}
#================================================================#
# MIGRATION AND UPGRADE FUNCTIONS
#================================================================#
# Migrate from old configuration format (if needed)
git_ssh_migrate() {
printf "Checking for migration needs...\n"
migration_needed=false
# Check for old format files (example)
old_config_file="$HOME/.GitSSH-config"
if [ -f "$old_config_file" ]; then
printf "Found old configuration file: %s\n" "$old_config_file"
migration_needed=true
fi
if [ "$migration_needed" = "false" ]; then
_print_info "No migration needed"
return 0
fi
printf "Migration required. Proceed? (Y/n): "
read -r proceed
case "$proceed" in
[Nn]*) printf "Migration cancelled\n"; return 0 ;;
esac
# Backup existing configurations
if [ -f "$GIT_SSH_CONFIG_FILE" ]; then
_backup_file "$GIT_SSH_CONFIG_FILE" "pre-migration"
fi
if [ -f "$GIT_SSH_USERS_FILE" ]; then
_backup_file "$GIT_SSH_USERS_FILE" "pre-migration"
fi
# Perform migration
_print_info "Migration completed"
printf "Old configuration files backed up\n"
}
# Reset configuration to defaults
git_ssh_reset() {
printf "Reset GitSSH Configuration\n"
printf "===========================\n"
printf "This will remove all configuration and start fresh\n"
printf "Are you sure? (y/N): "
read -r confirm
case "$confirm" in
[Yy]*) ;;
*) printf "Reset cancelled\n"; return 0 ;;
esac
# Backup existing configurations
backup_timestamp=$(date +%Y%m%d_%H%M%S)
if [ -f "$GIT_SSH_CONFIG_FILE" ]; then
_backup_file "$GIT_SSH_CONFIG_FILE" "reset-$backup_timestamp"
rm -f "$GIT_SSH_CONFIG_FILE"
printf "Backed up and removed: %s\n" "$GIT_SSH_CONFIG_FILE"
fi
if [ -f "$GIT_SSH_USERS_FILE" ]; then
_backup_file "$GIT_SSH_USERS_FILE" "reset-$backup_timestamp"
rm -f "$GIT_SSH_USERS_FILE"
printf "Backed up and removed: %s\n" "$GIT_SSH_USERS_FILE"
fi
# Clear session data
rm -f "$GIT_SESSION_TEMP"
printf "Cleared session data\n"
# Reinitialize
printf "\nReinitializing...\n"
git_ssh_init
}
#================================================================#
# SYSTEM INFORMATION
#================================================================#
# Show system information relevant to GitSSH
git_ssh_system_info() {
printf "GitSSH System Information\n"
printf "==========================\n"
# Operating System
os=$(_detect_os)
printf "Operating System: %s\n" "$os"
# Shell information
printf "Shell: %s\n" "${SHELL:-unknown}"
# Git version
if command -v git >/dev/null 2>&1; then
git_version=$(git --version 2>/dev/null | head -1)
printf "Git: %s\n" "$git_version"
else
printf "Git: Not installed\n"
fi
# SSH version
if command -v ssh >/dev/null 2>&1; then
ssh_version=$(ssh -V 2>&1 | head -1)
printf "SSH: %s\n" "$ssh_version"
else
printf "SSH: Not installed\n"
fi
# jq version
if command -v jq >/dev/null 2>&1; then
jq_version=$(jq --version 2>/dev/null)
printf "jq: %s\n" "$jq_version"
else
printf "jq: Not installed\n"
fi
# Configuration paths
printf "\nConfiguration Files:\n"
printf " Sessions: %s\n" "$GIT_SSH_CONFIG_FILE"
printf " Users: %s\n" "$GIT_SSH_USERS_FILE"
printf " SSH Config: %s\n" "$HOME/.ssh/config"
# File status
printf "\nFile Status:\n"
for file in "$GIT_SSH_CONFIG_FILE" "$GIT_SSH_USERS_FILE" "$HOME/.ssh/config"; do
if [ -f "$file" ]; then
size=$(wc -c < "$file" 2>/dev/null)
printf " %s: exists (%s bytes)\n" "$(basename "$file")" "$size"
else
printf " %s: missing\n" "$(basename "$file")"
fi
done
# SSH Agent status
printf "\nSSH Agent:\n"
if _is_ssh_agent_running; then
key_count=$(ssh-add -l 2>/dev/null | wc -l)
printf " Status: Running (%s keys loaded)\n" "$key_count"
else
printf " Status: Not running or no keys loaded\n"
fi
# GitHub SSH hosts
printf "\nGitHub SSH Hosts:\n"
ssh_hosts=$(_get_ssh_hosts)
if [ -n "$ssh_hosts" ]; then
for host in $ssh_hosts; do
printf " %s\n" "$host"
done
else
printf " (none configured)\n"
fi
}
#================================================================#
# gitssh-init.sh refactoring
#================================================================#
# Replace git_ssh_init() with:
system_init() {
git_ssh_init "$@" # Keep existing implementation
}
# Replace git_ssh_onboard() with:
system_onboard() {
git_ssh_onboard "$@" # Keep existing implementation
}
# Replace git_ssh_validate() with:
system_validate() {
git_ssh_validate "$@" # Keep existing implementation
}
# Add new functions:
system_check_configured() {
# Quick check if system is already configured
_check_dependencies 2>/dev/null && [ -f "$GIT_SSH_USERS_FILE" ] && \
jq -e '.users | length > 0' "$GIT_SSH_USERS_FILE" >/dev/null 2>&1
}
config_show() {
git_ssh_system_info "$@" # Keep existing implementation
}
config_reset() {
git_ssh_reset "$@" # Keep existing implementation
}
config_backup() {
git_ssh_backup "$@" # Keep existing implementation
}
config_restore() {
git_ssh_restore "$@" # Keep existing implementation
}
config_migrate() {
git_ssh_migrate "$@" # Keep existing implementation
}