-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitssh-utils.sh
More file actions
373 lines (315 loc) · 9.36 KB
/
gitssh-utils.sh
File metadata and controls
373 lines (315 loc) · 9.36 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
#!/bin/sh
#================================================================#
# GitSSH UTILITIES MODULE
# Core utility functions for GitSSH User Session Manager
#================================================================#
#================================================================#
# DEPENDENCY CHECKING
#================================================================#
_check_dependencies() {
if ! command -v jq >/dev/null 2>&1; then
printf "Error: jq required but not found. Run 'gitssh_init' for setup instructions.\n"
return 1
fi
return 0
}
#================================================================#
# REPOSITORY UTILITIES
#================================================================#
# Get repository identifier
_get_repo_id() {
git rev-parse --show-toplevel 2>/dev/null || printf ""
}
# Extract username from remote URL
_extract_username_from_remote() {
url="$1"
case "$url" in
*github.com[:/]*)
printf "%s" "$url" | sed 's|.*github\.com[:/]\([^/]*\)/.*|\1|'
;;
*)
printf ""
;;
esac
}
# Extract repository name from URL
_extract_repo_name() {
local url="$1"
local repo_name
# Universal extraction: get everything after the last slash
repo_name=$(printf "%s" "$url" | sed 's|.*/||')
# Remove .git suffix only if it exists at the end
repo_name=$(printf "%s" "$repo_name" | sed 's|\.git$||')
printf "%s" "$repo_name"
}
# Check if in git repository
_is_git_repo() {
git rev-parse --git-dir >/dev/null 2>&1
}
# Get current branch name
_get_current_branch() {
git branch --show-current 2>/dev/null || printf ""
}
# Get remote URL
_get_remote_url() {
remote_name="${1:-origin}"
git remote get-url "$remote_name" 2>/dev/null || printf ""
}
#================================================================#
# USER DATA UTILITIES
#================================================================#
# Get all configured users
_get_configured_users() {
_check_dependencies || return 1
[ ! -f "$GIT_SSH_USERS_FILE" ] && return 1
jq -r '.users | keys[]' "$GIT_SSH_USERS_FILE" 2>/dev/null
}
# Get user details by username
_get_user_details() {
username="$1"
_check_dependencies || return 1
[ ! -f "$GIT_SSH_USERS_FILE" ] && return 1
jq -r --arg user "$username" '.users[$user] // empty' "$GIT_SSH_USERS_FILE" 2>/dev/null
}
# Check if user exists in configuration
_user_exists() {
username="$1"
user_details=$(_get_user_details "$username")
[ -n "$user_details" ] && [ "$user_details" != "null" ] && [ "$user_details" != "empty" ]
}
# Get user field value
_get_user_field() {
username="$1"
field="$2"
user_details=$(_get_user_details "$username")
if [ -n "$user_details" ] && [ "$user_details" != "null" ]; then
printf "%s" "$user_details" | jq -r ".$field // empty"
fi
}
#================================================================#
# SSH UTILITIES
#================================================================#
# Get SSH hosts from config
_get_ssh_hosts() {
if [ -f "$HOME/.ssh/config" ]; then
grep '^Host github-' "$HOME/.ssh/config" | awk '{print $2}'
fi
}
# Test SSH connection
_test_ssh_connection() {
ssh_host="$1"
timeout="${2:-3}"
ssh -o ConnectTimeout="$timeout" -T "git@$ssh_host" 2>&1 | grep -q "successfully authenticated"
}
# Check if SSH agent is running
_is_ssh_agent_running() {
ssh-add -l >/dev/null 2>&1
}
# Get loaded SSH keys
_get_loaded_ssh_keys() {
if _is_ssh_agent_running; then
ssh-add -l
fi
}
#================================================================#
# FILE UTILITIES
#================================================================#
# Validate JSON file structure
_validate_json_file() {
file_path="$1"
expected_structure="$2"
[ ! -f "$file_path" ] && return 1
if [ -n "$expected_structure" ]; then
jq -e "$expected_structure" "$file_path" >/dev/null 2>&1
else
jq -e '.' "$file_path" >/dev/null 2>&1
fi
}
# Create backup of file
_backup_file() {
file_path="$1"
backup_suffix="${2:-backup}"
if [ -f "$file_path" ]; then
cp "$file_path" "${file_path}.${backup_suffix}" 2>/dev/null
return $?
fi
return 1
}
# Create temporary file safely
_create_temp_file() {
mktemp
}
# Safe file replace using temporary file
_safe_file_replace() {
source_file="$1"
target_file="$2"
if [ -f "$source_file" ]; then
mv "$source_file" "$target_file"
return $?
fi
return 1
}
#================================================================#
# INPUT/OUTPUT UTILITIES
#================================================================#
# Print colored output (if terminal supports it)
_print_color() {
color="$1"
text="$2"
if [ -t 1 ]; then # Check if stdout is a terminal
case "$color" in
red) printf "\033[31m%s\033[0m" "$text" ;;
green) printf "\033[32m%s\033[0m" "$text" ;;
yellow) printf "\033[33m%s\033[0m" "$text" ;;
blue) printf "\033[34m%s\033[0m" "$text" ;;
*) printf "%s" "$text" ;;
esac
else
printf "%s" "$text"
fi
}
# Print error message
_print_error() {
_print_color red "Error: $1"
printf "\n"
}
# Print success message
_print_success() {
_print_color green "$1"
printf "\n"
}
# Print warning message
_print_warning() {
_print_color yellow "Warning: $1"
printf "\n"
}
# Print info message
_print_info() {
_print_color blue "$1"
printf "\n"
}
#================================================================#
# VALIDATION UTILITIES
#================================================================#
# Validate email format (basic)
_validate_email() {
email="$1"
case "$email" in
*@*.*) return 0 ;;
*) return 1 ;;
esac
}
# Validate username (alphanumeric, dash, underscore)
_validate_username() {
username="$1"
case "$username" in
*[!a-zA-Z0-9_-]*) return 1 ;;
"") return 1 ;;
*) return 0 ;;
esac
}
# Check if string is empty or whitespace only
_is_empty() {
text="$1"
# Remove all whitespace and check if empty
trimmed=$(printf "%s" "$text" | sed 's/[[:space:]]//g')
[ -z "$trimmed" ]
}
#================================================================#
# CONFIGURATION UTILITIES
#================================================================#
# Get current git config values
_get_git_config() {
config_key="$1"
scope="${2:-}" # Can be --global, --local, or empty for effective
case "$scope" in
global) git config --global "$config_key" 2>/dev/null ;;
local) git config --local "$config_key" 2>/dev/null ;;
*) git config "$config_key" 2>/dev/null ;;
esac
}
# Set git config value
_set_git_config() {
config_key="$1"
config_value="$2"
scope="${3:-local}" # Default to local scope
case "$scope" in
global) git config --global "$config_key" "$config_value" ;;
local) git config --local "$config_key" "$config_value" ;;
*) git config "$config_key" "$config_value" ;;
esac
}
# Get effective git user info
_get_effective_git_user() {
name=$(_get_git_config "user.name")
email=$(_get_git_config "user.email")
if [ -n "$name" ] && [ -n "$email" ]; then
printf "%s <%s>" "$name" "$email"
else
printf "Not configured"
fi
}
#================================================================#
# STRING PROCESSING UTILITIES
#================================================================#
# Trim whitespace from string
_trim() {
text="$1"
# Remove leading and trailing whitespace
printf "%s" "$text" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
}
# Convert to lowercase
_to_lower() {
printf "%s" "$1" | tr '[:upper:]' '[:lower:]'
}
# Convert to uppercase
_to_upper() {
printf "%s" "$1" | tr '[:lower:]' '[:upper:]'
}
# Count words in string
_count_words() {
printf "%s" "$1" | wc -w
}
#================================================================#
# PLATFORM DETECTION
#================================================================#
# Detect operating system
_detect_os() {
case "$(uname -s)" in
Linux*) printf "linux" ;;
Darwin*) printf "macos" ;;
CYGWIN*) printf "windows" ;;
MINGW*) printf "windows" ;;
FreeBSD*) printf "freebsd" ;;
*) printf "unknown" ;;
esac
}
# Get package manager suggestions based on OS
_get_package_manager_hint() {
os=$(_detect_os)
case "$os" in
linux)
if command -v apt >/dev/null 2>&1; then
printf "sudo apt install"
elif command -v yum >/dev/null 2>&1; then
printf "sudo yum install"
elif command -v dnf >/dev/null 2>&1; then
printf "sudo dnf install"
elif command -v pacman >/dev/null 2>&1; then
printf "sudo pacman -S"
else
printf "package manager install"
fi
;;
macos)
if command -v brew >/dev/null 2>&1; then
printf "brew install"
else
printf "brew install (install Homebrew first)"
fi
;;
*)
printf "install via your package manager:"
;;
esac
}