-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathghset
More file actions
243 lines (211 loc) · 5.36 KB
/
ghset
File metadata and controls
243 lines (211 loc) · 5.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
#!/bin/bash
# Script para alternar perfis SSH
# Uso: ghset <perfil>
# Perfis disponíveis: belmo, oppy, personal
set -e
PROFILE="$1"
SSH_CONFIG="$HOME/.ssh/config"
BACKUP_CONFIG="$HOME/.ssh/config.backup"
# Cores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
show_help() {
echo "Uso: ghset <perfil>"
echo ""
echo "Perfis disponíveis:"
echo " belmo - Perfil pessoal (chave id_github)"
echo " oppy - Perfil empresa (chave id_oppy)"
echo " personal - Perfil pessoal (chave id_ed25519)"
echo ""
echo "Exemplos:"
echo " ghset belmo"
echo " ghset oppy"
echo " ghset personal"
}
backup_config() {
if [[ -f "$SSH_CONFIG" ]]; then
cp "$SSH_CONFIG" "$BACKUP_CONFIG"
echo -e "${YELLOW}Backup criado: $BACKUP_CONFIG${NC}"
fi
}
create_config() {
local profile="$1"
case "$profile" in
"belmo")
cat > "$SSH_CONFIG" << 'EOF'
# GitHub - Perfil Belmo (Pessoal)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_github
IdentitiesOnly yes
# Bitbucket
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_bitbucket
IdentitiesOnly yes
# Repositórios da empresa (desabilitado no perfil belmo)
# Host github.com-koware
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_oppy
# IdentitiesOnly yes
EOF
;;
"oppy")
cat > "$SSH_CONFIG" << 'EOF'
# GitHub - Perfil Oppy (Empresa)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_oppy
IdentitiesOnly yes
# Bitbucket
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_bitbucket
IdentitiesOnly yes
# Repositórios da empresa
Host github.com-koware
HostName github.com
User git
IdentityFile ~/.ssh/id_oppy
IdentitiesOnly yes
EOF
;;
"personal")
cat > "$SSH_CONFIG" << 'EOF'
# GitHub - Perfil Personal (Pessoal)
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
# Bitbucket
Host bitbucket.org
HostName bitbucket.org
User git
IdentityFile ~/.ssh/id_bitbucket
IdentitiesOnly yes
# Repositórios da empresa (desabilitado no perfil personal)
# Host github.com-koware
# HostName github.com
# User git
# IdentityFile ~/.ssh/id_oppy
# IdentitiesOnly yes
EOF
;;
*)
echo -e "${RED}Erro: Perfil '$profile' não encontrado${NC}"
show_help
exit 1
;;
esac
}
verify_keys() {
local profile="$1"
local key_file=""
case "$profile" in
"belmo")
key_file="$HOME/.ssh/id_github"
;;
"oppy")
key_file="$HOME/.ssh/id_oppy"
;;
"personal")
key_file="$HOME/.ssh/id_ed25519"
;;
esac
if [[ ! -f "$key_file" ]]; then
echo -e "${RED}Erro: Chave SSH não encontrada: $key_file${NC}"
exit 1
fi
if [[ ! -f "${key_file}.pub" ]]; then
echo -e "${RED}Erro: Chave pública não encontrada: ${key_file}.pub${NC}"
exit 1
fi
}
clear_ssh_agent() {
# Remove todas as chaves do ssh-agent
ssh-add -D 2>/dev/null || true
}
add_ssh_key() {
local profile="$1"
local key_file=""
case "$profile" in
"belmo")
key_file="$HOME/.ssh/id_github"
;;
"oppy")
key_file="$HOME/.ssh/id_oppy"
;;
"personal")
key_file="$HOME/.ssh/id_ed25519"
;;
esac
# Adiciona a chave específica ao ssh-agent
ssh-add "$key_file" 2>/dev/null || {
echo -e "${YELLOW}Aviso: Não foi possível adicionar a chave ao ssh-agent${NC}"
}
}
show_current_profile() {
if [[ ! -f "$SSH_CONFIG" ]]; then
echo -e "${RED}Arquivo de configuração SSH não encontrado${NC}"
return
fi
local github_key=$(grep -A 5 "Host github.com" "$SSH_CONFIG" | grep "IdentityFile" | head -1 | awk '{print $2}' | sed 's/~/$HOME/g')
case "$github_key" in
*"id_github")
echo -e "${GREEN}Perfil atual: belmo (pessoal)${NC}"
;;
*"id_oppy")
echo -e "${GREEN}Perfil atual: oppy (empresa)${NC}"
;;
*"id_ed25519")
echo -e "${GREEN}Perfil atual: personal (pessoal)${NC}"
;;
*)
echo -e "${YELLOW}Perfil atual: desconhecido${NC}"
;;
esac
}
# Função principal
main() {
if [[ $# -eq 0 ]]; then
show_current_profile
echo ""
show_help
exit 0
fi
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi
verify_keys "$PROFILE"
backup_config
create_config "$PROFILE"
clear_ssh_agent
add_ssh_key "$PROFILE"
echo -e "${GREEN}Perfil alterado para: $PROFILE${NC}"
# Mostra informações da chave ativa
case "$PROFILE" in
"belmo")
echo -e "Chave SSH: ${YELLOW}~/.ssh/id_github${NC}"
;;
"oppy")
echo -e "Chave SSH: ${YELLOW}~/.ssh/id_oppy${NC}"
;;
"personal")
echo -e "Chave SSH: ${YELLOW}~/.ssh/id_ed25519${NC}"
;;
esac
# Testa a conexão
echo ""
echo -e "${YELLOW}Testando conexão...${NC}"
ssh -T git@github.com 2>&1 | head -1
}
main "$@"