From d2c18372f43be8d277651a9a4c72a7abf2878837 Mon Sep 17 00:00:00 2001 From: "Lockszmith (@VAST)" Date: Mon, 10 Mar 2025 16:40:17 -0400 Subject: [PATCH] Improve symclone's safety Make sure files are not overwritten, and that symlinks would be reapplied - allowing re-application of already existing links --- symclone.sh | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/symclone.sh b/symclone.sh index e855fad..a5e6cfe 100755 --- a/symclone.sh +++ b/symclone.sh @@ -6,7 +6,15 @@ set -e SRC_DIR=_src.posix # Target directory (new structure with symlinks) -DEST_DIR="${1:?}" +DEST_DIR="${1}" + +is_cmd() { + type -p -- "${@}" 2> /dev/null 1> /dev/null +} +if is_cmd chezmoi && [ -z "$DEST_DIR" ]; then + DEST_DIR="$(chezmoi data | jq -r '.chezmoi.sourceDir | split("/") | last')" +fi +DEST_DIR="${DEST_DIR:?Must supply dest dir name}" # Check if both arguments are provided if [[ -z "$SRC_DIR" || -z "$DEST_DIR" ]]; then @@ -52,7 +60,8 @@ find "$SRC_DIR" -type f | while read -r file; do src_relative_path=$(relpath "$file" "$(dirname "$DEST_DIR/$target_file")") # Create the symlink with relative path - ln -vs "$src_relative_path" "$DEST_DIR/$target_file" + [ -L "$DEST_DIR/$target_file" ] || ! [ -e "$DEST_DIR/$target_file" ] \ + && ln ${FORCE} -vs "$src_relative_path" "$DEST_DIR/$target_file" done echo "Symbolic links created successfully in '$DEST_DIR'."