dot-add: support multiple files and directories

This commit is contained in:
Jonas H
2026-02-27 22:39:18 +01:00
parent 977e839eb7
commit 3769cb3fa3

50
dot-add
View File

@@ -1,50 +1,50 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# dot-add: Move a config file into the dotfiles repo and stow it. # dot-add: Move config files/dirs into the dotfiles repo and stow them.
# #
# Usage: dot-add <package-name> <file-path> # Usage: dot-add <package> <path> [<path> ...]
# #
# Example: dot-add sway ~/.config/sway/config # Examples:
# dot-add alacritty ~/.config/alacritty/alacritty.toml # dot-add sway ~/.config/sway/config
# dot-add sway ~/.config/sway/
# dot-add sway ~/.config/sway/*
set -euo pipefail set -euo pipefail
DOTFILES_DIR="$(cd "$(dirname "$0")" && pwd)" DOTFILES_DIR="$(cd "$(dirname "$0")" && pwd)"
if [[ $# -ne 2 ]]; then if [[ $# -lt 2 ]]; then
echo "Usage: dot-add <package> <file>" echo "Usage: dot-add <package> <path> [<path> ...]"
echo " package: name for the stow package (e.g. sway, alacritty)"
echo " file: path to the file (absolute or relative to HOME)"
exit 1 exit 1
fi fi
PACKAGE="$1" PACKAGE="$1"
FILE="$(realpath "$2")" shift
add_one() {
local FILE
FILE="$(realpath "$1")"
if [[ ! -e "$FILE" ]]; then if [[ ! -e "$FILE" ]]; then
echo "Error: '$FILE' does not exist" echo "Error: '$FILE' does not exist" >&2
exit 1 return 1
fi fi
if [[ ! "$FILE" == "$HOME/"* ]]; then if [[ ! "$FILE" == "$HOME/"* ]]; then
echo "Error: file must be under \$HOME ($HOME)" echo "Error: '$FILE' is not under \$HOME" >&2
exit 1 return 1
fi fi
# Relative path from HOME (e.g. .config/sway/config) local REL="${FILE#$HOME/}"
REL="${FILE#$HOME/}" local DEST="$DOTFILES_DIR/$PACKAGE/$REL"
DEST_DIR="$DOTFILES_DIR/$PACKAGE/$(dirname "$REL")" mkdir -p "$(dirname "$DEST")"
DEST="$DOTFILES_DIR/$PACKAGE/$REL"
mkdir -p "$DEST_DIR"
mv "$FILE" "$DEST" mv "$FILE" "$DEST"
echo "Moved: $FILE -> $DEST" echo "Moved: $FILE -> $DEST"
}
for PATH_ARG in "$@"; do
add_one "$PATH_ARG"
done
# Stow the package (restow if already stowed)
stow --dir="$DOTFILES_DIR" --target="$HOME" --restow "$PACKAGE" stow --dir="$DOTFILES_DIR" --target="$HOME" --restow "$PACKAGE"
echo "Stowed: $PACKAGE" echo "Stowed: $PACKAGE"
echo ""
echo "Next steps:"
echo " cd ~/dotfiles"
echo " git add $PACKAGE/$REL"
echo " git commit -m 'add $REL'"