Created
May 1, 2025 23:09
-
-
Save yogithesymbian/da5773b93364422df787bc31dbae052d to your computer and use it in GitHub Desktop.
script-setup-github-auto-v2.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# π¨ Colors | |
GREEN='\033[0;32m' | |
BLUE='\033[0;34m' | |
YELLOW='\033[1;33m' | |
RED='\033[0;31m' | |
NC='\033[0m' | |
IDENTITIES_FILE="$HOME/.gh-identities.json" | |
echo -e "${BLUE}=== GitHub SSH Setup + Git Identity ===${NC}" | |
# Fungsi validasi email | |
is_valid_email() { | |
[[ "$1" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]] | |
} | |
# Fungsi validasi nama | |
is_valid_name() { | |
[[ "$1" =~ ^[A-Za-z0-9][A-Za-z0-9\ \.\-]*$ ]] | |
} | |
# Prompt untuk preset | |
if [[ -f "$IDENTITIES_FILE" ]]; then | |
echo -e "${BLUE}ποΈ Preset tersedia:${NC}" | |
jq -r 'to_entries[] | "\(.key): \(.value.email) (\(.value.alias))"' "$IDENTITIES_FILE" | |
read -p "πΈ Gunakan preset? (masukkan nama preset atau kosongkan untuk lanjut): " PRESET | |
if [[ "$PRESET" != "" ]]; then | |
if jq -e ".\"$PRESET\"" "$IDENTITIES_FILE" >/dev/null; then | |
EMAIL=$(jq -r ".\"$PRESET\".email" "$IDENTITIES_FILE") | |
GIT_NAME=$(jq -r ".\"$PRESET\".name" "$IDENTITIES_FILE") | |
SSH_ALIAS=$(jq -r ".\"$PRESET\".alias" "$IDENTITIES_FILE") | |
SSH_KEY_NAME=$(jq -r ".\"$PRESET\".key" "$IDENTITIES_FILE") | |
echo -e "${GREEN}β Preset '$PRESET' digunakan.${NC}" | |
else | |
echo -e "${RED}β Preset tidak ditemukan.${NC}" | |
exit 1 | |
fi | |
fi | |
fi | |
# Prompt jika tidak dari preset | |
if [[ -z "$EMAIL" ]]; then | |
read -rp "πΉ Email GitHub: " EMAIL | |
if ! is_valid_email "$EMAIL"; then | |
echo -e "${RED}β Format email tidak valid.${NC}" | |
exit 1 | |
fi | |
fi | |
if [[ -z "$GIT_NAME" ]]; then | |
read -rp "πΉ Nama untuk commit: " GIT_NAME | |
if ! is_valid_name "$GIT_NAME"; then | |
echo -e "${RED}β Nama tidak valid.${NC}" | |
exit 1 | |
fi | |
fi | |
if [[ -z "$SSH_ALIAS" ]]; then | |
read -rp "πΉ Alias SSH (contoh: github.com-personal): " SSH_ALIAS | |
fi | |
if [[ -z "$SSH_KEY_NAME" ]]; then | |
read -rp "πΉ Nama file SSH key (contoh: id_ed25519_$SSH_ALIAS): " SSH_KEY_NAME | |
fi | |
KEY_PATH="$HOME/.ssh/$SSH_KEY_NAME" | |
# Generate SSH key | |
if [[ -f "$KEY_PATH" ]]; then | |
echo -e "${YELLOW}β οΈ SSH key sudah ada. Lewati pembuatan.${NC}" | |
else | |
echo -e "${BLUE}[1/6] Membuat SSH key baru...${NC}" | |
ssh-keygen -t ed25519 -C "$EMAIL" -f "$KEY_PATH" | |
fi | |
# Tambah ke ssh-agent | |
echo -e "${BLUE}[2/6] Menambahkan ke ssh-agent...${NC}" | |
eval "$(ssh-agent -s)" | |
ssh-add "$KEY_PATH" | |
# Tambah ke ~/.ssh/config | |
if grep -q "Host $SSH_ALIAS" "$HOME/.ssh/config"; then | |
echo -e "${YELLOW}β οΈ Alias SSH sudah ada di ~/.ssh/config.${NC}" | |
else | |
echo -e "${BLUE}[3/6] Tambah ke ~/.ssh/config...${NC}" | |
cat <<EOF >> ~/.ssh/config | |
# GitHub - $SSH_ALIAS | |
Host $SSH_ALIAS | |
HostName github.com | |
User git | |
IdentityFile $KEY_PATH | |
IdentitiesOnly yes | |
EOF | |
fi | |
# Tampilkan SSH key | |
echo -e "${BLUE}[4/6] Public key untuk GitHub:${NC}" | |
echo -e "${GREEN}----------------------------------------------" | |
cat "$KEY_PATH.pub" | |
echo -e "----------------------------------------------${NC}" | |
# Auto buka browser | |
read -rp "π Mau buka halaman SSH Keys GitHub? (y/n): " OPEN_GH | |
if [[ "$OPEN_GH" == "y" || "$OPEN_GH" == "Y" ]]; then | |
open "https://github.com/settings/keys" 2>/dev/null || xdg-open "https://github.com/settings/keys" | |
fi | |
# Set git identity | |
read -rp "πΈ Set git identity di repo sekarang? (y/n): " SET_GIT | |
if [[ "$SET_GIT" == "y" || "$SET_GIT" == "Y" ]]; then | |
git config user.name "$GIT_NAME" | |
git config user.email "$EMAIL" | |
echo -e "${GREEN}β Git identity diset untuk repo ini.${NC}" | |
fi | |
# Simpan preset | |
read -rp "πΎ Simpan konfigurasi ini sebagai preset? (y/n): " SAVE_PRESET | |
if [[ "$SAVE_PRESET" == "y" || "$SAVE_PRESET" == "Y" ]]; then | |
read -rp "π Nama preset: " PRESET_NAME | |
TMP_JSON=$(mktemp) | |
# Buat atau update preset | |
if [[ -f "$IDENTITIES_FILE" ]]; then | |
jq ".\"$PRESET_NAME\" = {\"email\":\"$EMAIL\", \"name\":\"$GIT_NAME\", \"alias\":\"$SSH_ALIAS\", \"key\":\"$SSH_KEY_NAME\"}" "$IDENTITIES_FILE" >"$TMP_JSON" && | |
mv "$TMP_JSON" "$IDENTITIES_FILE" | |
else | |
jq -n --arg email "$EMAIL" --arg name "$GIT_NAME" --arg alias "$SSH_ALIAS" --arg key "$SSH_KEY_NAME" \ | |
"{\"$PRESET_NAME\": {\"email\": \$email, \"name\": \$name, \"alias\": \$alias, \"key\": \$key}}" >"$IDENTITIES_FILE" | |
fi | |
echo -e "${GREEN}β Preset '$PRESET_NAME' disimpan ke $IDENTITIES_FILE.${NC}" | |
fi | |
# Saran penggunaan | |
echo -e "${BLUE}β Selesai! Gunakan SSH clone URL seperti:${NC}" | |
echo -e "${GREEN}git@${SSH_ALIAS}:USERNAME/NAMA_REPO.git${NC}" |
Author
yogithesymbian
commented
May 1, 2025
save solut
git config user.name "Nama Akun Kerja"
git config user.email "[email protected]"
cek
git config user.name
git config user.email
git config --global user.name
git config --global user.email
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment