Improve symclone's safety

Make sure files are not overwritten, and that symlinks would be reapplied
- allowing re-application of already existing links
This commit is contained in:
Lockszmith (@VAST) 2025-03-10 16:40:17 -04:00
parent 20555a91c9
commit d2c18372f4
1 changed files with 11 additions and 2 deletions

View File

@ -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'."