257 lines
5.9 KiB
Bash
Executable File
257 lines
5.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# ZSH 配置安装脚本
|
|
# 用于在其他 VPS 上安装和同步 oh-my-zsh 配置
|
|
|
|
set -euo pipefail
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
# 日志函数
|
|
log_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
log_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# 检查是否为 root 用户
|
|
check_root() {
|
|
if [[ $EUID -ne 0 ]]; then
|
|
log_error "此脚本需要 root 权限运行"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# 安装依赖
|
|
install_dependencies() {
|
|
log_info "安装依赖包..."
|
|
|
|
# 更新包列表
|
|
apt update
|
|
|
|
# 安装必要的包
|
|
apt install -y \
|
|
zsh \
|
|
git \
|
|
curl \
|
|
wget \
|
|
htop \
|
|
tree \
|
|
jq \
|
|
tmux \
|
|
fonts-powerline \
|
|
fontconfig
|
|
|
|
log_success "依赖包安装完成"
|
|
}
|
|
|
|
# 安装 oh-my-zsh
|
|
install_oh_my_zsh() {
|
|
log_info "安装 oh-my-zsh..."
|
|
|
|
if [[ -d "$HOME/.oh-my-zsh" ]]; then
|
|
log_warning "oh-my-zsh 已安装,跳过安装步骤"
|
|
return 0
|
|
fi
|
|
|
|
# 安装 oh-my-zsh
|
|
RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
|
|
|
|
log_success "oh-my-zsh 安装完成"
|
|
}
|
|
|
|
# 安装自定义插件
|
|
install_custom_plugins() {
|
|
log_info "安装自定义插件..."
|
|
|
|
local custom_dir="$HOME/.oh-my-zsh/custom/plugins"
|
|
|
|
# zsh-autosuggestions
|
|
if [[ ! -d "$custom_dir/zsh-autosuggestions" ]]; then
|
|
log_info "安装 zsh-autosuggestions..."
|
|
git clone https://github.com/zsh-users/zsh-autosuggestions "$custom_dir/zsh-autosuggestions"
|
|
fi
|
|
|
|
# zsh-syntax-highlighting
|
|
if [[ ! -d "$custom_dir/zsh-syntax-highlighting" ]]; then
|
|
log_info "安装 zsh-syntax-highlighting..."
|
|
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git "$custom_dir/zsh-syntax-highlighting"
|
|
fi
|
|
|
|
# zsh-completions
|
|
if [[ ! -d "$custom_dir/zsh-completions" ]]; then
|
|
log_info "安装 zsh-completions..."
|
|
git clone https://github.com/zsh-users/zsh-completions "$custom_dir/zsh-completions"
|
|
fi
|
|
|
|
log_success "自定义插件安装完成"
|
|
}
|
|
|
|
# 部署配置文件
|
|
deploy_configs() {
|
|
log_info "部署配置文件..."
|
|
|
|
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
# 备份现有配置
|
|
if [[ -f "$HOME/.zshrc" ]]; then
|
|
log_info "备份现有 .zshrc..."
|
|
cp "$HOME/.zshrc" "$HOME/.zshrc.backup.$(date +%Y%m%d_%H%M%S)"
|
|
fi
|
|
|
|
# 部署 .zshrc
|
|
if [[ -f "$script_dir/zshrc.template" ]]; then
|
|
log_info "部署 .zshrc 配置..."
|
|
cp "$script_dir/zshrc.template" "$HOME/.zshrc"
|
|
else
|
|
log_error "找不到 zshrc.template 文件"
|
|
exit 1
|
|
fi
|
|
|
|
# 部署自定义配置
|
|
if [[ -d "$script_dir/oh-my-zsh-custom" ]]; then
|
|
log_info "部署自定义 oh-my-zsh 配置..."
|
|
# 只复制非 git 仓库的文件
|
|
find "$script_dir/oh-my-zsh-custom" -type f -not -path "*/.git/*" -exec cp --parents {} "$HOME/.oh-my-zsh/custom/" \;
|
|
fi
|
|
|
|
log_success "配置文件部署完成"
|
|
}
|
|
|
|
# 设置默认 shell
|
|
set_default_shell() {
|
|
log_info "设置 zsh 为默认 shell..."
|
|
|
|
# 检查 zsh 是否在 /etc/shells 中
|
|
if ! grep -q "$(which zsh)" /etc/shells; then
|
|
log_info "添加 zsh 到 /etc/shells..."
|
|
echo "$(which zsh)" >> /etc/shells
|
|
fi
|
|
|
|
# 设置默认 shell
|
|
chsh -s "$(which zsh)"
|
|
|
|
log_success "默认 shell 设置为 zsh"
|
|
}
|
|
|
|
# 创建同步脚本
|
|
create_sync_script() {
|
|
log_info "创建同步脚本..."
|
|
|
|
cat > /usr/local/bin/sync-zsh-config << 'EOF'
|
|
#!/bin/bash
|
|
|
|
# ZSH 配置同步脚本
|
|
# 从 Gitea 仓库拉取最新配置
|
|
|
|
set -euo pipefail
|
|
|
|
MGMT_DIR="/root/mgmt"
|
|
ZSH_CONFIG_DIR="$MGMT_DIR/configuration/zsh"
|
|
|
|
log_info() {
|
|
echo -e "\033[0;34m[INFO]\033[0m $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "\033[0;32m[SUCCESS]\033[0m $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "\033[0;31m[ERROR]\033[0m $1"
|
|
}
|
|
|
|
# 检查 mgmt 目录是否存在
|
|
if [[ ! -d "$MGMT_DIR" ]]; then
|
|
log_error "mgmt 目录不存在: $MGMT_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# 进入 mgmt 目录
|
|
cd "$MGMT_DIR"
|
|
|
|
# 拉取最新配置
|
|
log_info "拉取最新配置..."
|
|
git pull origin main
|
|
|
|
# 检查 zsh 配置目录
|
|
if [[ ! -d "$ZSH_CONFIG_DIR" ]]; then
|
|
log_error "zsh 配置目录不存在: $ZSH_CONFIG_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# 备份当前配置
|
|
if [[ -f "$HOME/.zshrc" ]]; then
|
|
log_info "备份当前配置..."
|
|
cp "$HOME/.zshrc" "$HOME/.zshrc.backup.$(date +%Y%m%d_%H%M%S)"
|
|
fi
|
|
|
|
# 部署新配置
|
|
log_info "部署新配置..."
|
|
cp "$ZSH_CONFIG_DIR/zshrc.template" "$HOME/.zshrc"
|
|
|
|
if [[ -d "$ZSH_CONFIG_DIR/oh-my-zsh-custom" ]]; then
|
|
cp -r "$ZSH_CONFIG_DIR/oh-my-zsh-custom"/* "$HOME/.oh-my-zsh/custom/"
|
|
fi
|
|
|
|
log_success "ZSH 配置同步完成!"
|
|
log_info "请运行 'source ~/.zshrc' 或重新登录以应用新配置"
|
|
EOF
|
|
|
|
chmod +x /usr/local/bin/sync-zsh-config
|
|
|
|
log_success "同步脚本创建完成: /usr/local/bin/sync-zsh-config"
|
|
}
|
|
|
|
# 显示使用说明
|
|
show_usage() {
|
|
log_success "ZSH 配置安装完成!"
|
|
echo ""
|
|
log_info "使用方法:"
|
|
echo " 1. 重新登录或运行: source ~/.zshrc"
|
|
echo " 2. 同步配置: sync-zsh-config"
|
|
echo " 3. 查看别名: alias"
|
|
echo ""
|
|
log_info "可用命令:"
|
|
echo " - mgmt-status, mgmt-deploy, mgmt-cleanup"
|
|
echo " - ansible-check, ansible-deploy, ansible-ping"
|
|
echo " - tofu-init, tofu-plan, tofu-apply"
|
|
echo " - dps, dex, dlog (Docker)"
|
|
echo " - k, kgp, kgs (Kubernetes)"
|
|
echo ""
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
log_info "开始安装 ZSH 配置..."
|
|
|
|
check_root
|
|
install_dependencies
|
|
install_oh_my_zsh
|
|
install_custom_plugins
|
|
deploy_configs
|
|
set_default_shell
|
|
create_sync_script
|
|
show_usage
|
|
|
|
log_success "安装完成!"
|
|
}
|
|
|
|
# 运行主函数
|
|
main "$@"
|