Files
2026-09-16 21:44:25 +08:00

70 lines
1.6 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 将 herdr 配置链接到 dotfile
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1091
source "$SCRIPT_DIR/common/help.sh"
usage() {
cat <<'EOF'
用法: herdr-init
将 ~/.dotfile/herdr/config.toml 软链接到 herdr 配置路径:
$XDG_CONFIG_HOME/herdr/config.toml(默认 ~/.config/herdr/config.toml)
已有配置会备份为 *.backup.<时间戳>。
选项:
-h, --help 显示此帮助
EOF
}
dotfile_help_requested "${1:-}" && dotfile_show_help
set -e
SRC="$HOME/.dotfile/herdr/config.toml"
CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
DEST_DIR="$CONFIG_HOME/herdr"
DEST="$DEST_DIR/config.toml"
if [ ! -f "$SRC" ]; then
echo "错误: 未找到 $SRC" >&2
exit 1
fi
echo "初始化 herdr 配置..."
echo "目标路径: $DEST"
mkdir -p "$DEST_DIR"
if [ -L "$DEST" ] && [ "$(readlink -f "$DEST")" = "$(readlink -f "$SRC")" ]; then
echo "软链接已存在且指向正确位置,无需操作。"
exit 0
fi
if [ -e "$DEST" ] || [ -L "$DEST" ]; then
BACKUP="$DEST.backup.$(date +%Y%m%d%H%M%S)"
mv "$DEST" "$BACKUP"
echo "已将现有配置备份到 $BACKUP"
fi
ln -s "$SRC" "$DEST"
if [ -L "$DEST" ] && [ "$(readlink -f "$DEST")" = "$(readlink -f "$SRC")" ]; then
echo "软链接创建成功: $DEST -> $SRC"
else
echo "软链接创建失败,请检查路径和权限。" >&2
exit 1
fi
if command -v herdr >/dev/null 2>&1; then
echo "校验配置:"
herdr config check || true
if pgrep -x herdr >/dev/null 2>&1; then
echo "herdr 正在运行,可在 herdr 内用 prefix+shift+r 热重载"
fi
else
echo "未找到 herdr,跳过配置校验"
fi