-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf
executable file
·129 lines (112 loc) · 3.36 KB
/
pf
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
#!/bin/bash
CONFIG_ROOT="$HOME/.config/profile-switcher"
SSH_DIR="$HOME/.ssh"
# Create necessary directories if they don't exist
mkdir -p "$CONFIG_ROOT"
mkdir -p "$SSH_DIR"
# Function to show the current SSH being used
current_profile() {
if [ -L "$SSH_DIR/id_rsa" ]; then
echo "Current SSH key: $(readlink "$SSH_DIR/id_rsa")"
else
echo "Using local SSH key (not managed by profile switcher)"
fi
echo "Testing SSH connection to GitHub..."
echo "----------------------------------------"
ssh -T [email protected]
echo "----------------------------------------"
}
# Function to list available profiles
list_profiles() {
echo "Available profiles:"
for dir in "$CONFIG_ROOT"/*; do
if [ -d "$dir" ]; then
profile_name=$(basename "$dir")
echo " - $profile_name"
fi
done
}
# Function to create a new profile
create_profile() {
local profile_name=$1
local profile_dir="$CONFIG_ROOT/$profile_name"
if [ -d "$profile_dir" ]; then
echo "Profile '$profile_name' already exists!"
exit 1
fi
mkdir -p "$profile_dir"
# Create directories for SSH and Git configs
mkdir -p "$profile_dir/ssh"
mkdir -p "$profile_dir/git"
echo "Created new profile: $profile_name"
echo "Please copy your SSH keys and Git config to:"
echo " SSH keys: $profile_dir/ssh/"
echo " Git config: $profile_dir/git/config"
}
# Function to switch to a profile
switch_profile() {
local profile_name=$1
local profile_dir="$CONFIG_ROOT/$profile_name"
if [ ! -d "$profile_dir" ]; then
echo "Profile '$profile_name' not found!"
exit 1
fi
# Backup current SSH keys if they exist
if [ -f "$SSH_DIR/id_rsa" ]; then
mv "$SSH_DIR/id_rsa" "$SSH_DIR/id_rsa.backup"
mv "$SSH_DIR/id_rsa.pub" "$SSH_DIR/id_rsa.pub.backup"
fi
# Link SSH keys
if [ -f "$profile_dir/ssh/id_rsa" ]; then
ln -sf "$profile_dir/ssh/id_rsa" "$SSH_DIR/id_rsa"
ln -sf "$profile_dir/ssh/id_rsa.pub" "$SSH_DIR/id_rsa.pub"
chmod 600 "$SSH_DIR/id_rsa"
chmod 644 "$SSH_DIR/id_rsa.pub"
else
echo "Warning: No SSH keys found in profile"
fi
# Link Git config
if [ -f "$profile_dir/git/config" ]; then
ln -sf "$profile_dir/git/config" "$HOME/.gitconfig"
else
echo "Warning: No Git config found in profile"
fi
echo "Switched to profile: $profile_name"
}
# Show usage if no arguments provided
if [ $# -eq 0 ]; then
echo "Usage: $0 [command] [profile_name]"
echo "Commands:"
echo " list, l - List available profiles"
echo " create, c <profile> - Create a new profile"
echo " switch, s <profile> - Switch to specified profile"
echo " current, cur <profile> - Shows the current profile"
exit 1
fi
# Process commands
case "$1" in
"list"|"l")
list_profiles
;;
"create"|"c")
if [ -z "$2" ]; then
echo "Error: Profile name required"
exit 1
fi
create_profile "$2"
;;
"switch"|"s")
if [ -z "$2" ]; then
echo "Error: Profile name required"
exit 1
fi
switch_profile "$2"
;;
"current"|"cur")
current_profile
;;
*)
echo "Unknown command: $1"
exit 1
;;
esac