onboarding/onboarding.sh

73 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Git + SSH onboarding script for developer environments.
# Prompts for missing git config, sets recommended options, and helps create and test SSH keys.
# Author: [ilia] | Created: [Aug 28, 2025]
set -euo pipefail # Exit on error, unset vars, and in pipe failures
## FUNCTIONS
# Prompt for and set global git config if missing
set_git_config() {
local key="$1"
local prompt="$2"
if ! git config --get "user.${key}" &>/dev/null; then
read -rp "$prompt" value
git config --global "user.${key}" "$value"
fi
}
# Create ed25519 SSH key if missing
setup_ssh_key() {
if [ ! -f ~/.ssh/id_ed25519 ]; then
read -rp "No ed25519 SSH key found! Create one for Git usage? (y/n): " sshyn
if [[ "$sshyn" =~ ^[Yy]$ ]]; then
ssh-keygen -t ed25519 -C "$(git config --get user.email)"
fi
fi
}
# Show public SSH key and copy to clipboard
show_and_copy_ssh_key() {
if [ -f ~/.ssh/id_ed25519.pub ]; then
echo "Your public ed25519 SSH key:"
cat ~/.ssh/id_ed25519.pub
if command -v xclip &>/dev/null; then
xclip -sel clip < ~/.ssh/id_ed25519.pub
echo "Public key copied to clipboard."
fi
echo "Add this key to Gitea: http://10.0.30.169:3000/user/settings/keys"
else
echo "No public ed25519 SSH key found. You might need to generate it."
fi
}
# SSH connection test
test_ssh_connection() {
read -rp "Test SSH connection to Gitea server? (y/n): " testssh
if [[ "$testssh" =~ ^[Yy]$ ]]; then
ssh -T gitvm@10.0.30.169
fi
}
## MAIN LOGIC
echo "Checking Git global config..."
set_git_config "name" "Enter your Git user name: "
set_git_config "email" "Enter your Git email: "
# Set best-practice Git global defaults
git config --global credential.helper cache
git config --global color.ui auto
echo
echo "Current global Git configuration:"
git config --global --list
setup_ssh_key
show_and_copy_ssh_key
test_ssh_connection
echo
echo "Setup complete!"