From 55e352a74ad1a02af7c68dc5354bac46438be5e0 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Sun, 30 Apr 2023 14:41:56 -0500 Subject: refactor linking function --- deploy.sh | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/deploy.sh b/deploy.sh index 4fae34d..a5e7876 100755 --- a/deploy.sh +++ b/deploy.sh @@ -23,6 +23,11 @@ fi set -u +if [ ! "$(pwd)" = "$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" ]; then + echo "expects to be run from the root of the dotfiles directory" + exit 1 +fi + uname=$(uname | tr '[:upper:]' '[:lower:]') # XDG layout @@ -41,37 +46,35 @@ if [ "linux" = "$uname" ]; then fi function link { - link_src="$1" - link_dest="$2" - if [ -e "$link_dest" ] && [ ! -L "$link_dest" ]; then - mkdir -p "dotfiles-backup" - mv "$link_dest" "dotfiles-backup/." + local src="$1" + local dest="$2" + if [ -e "$dest" ] && [ ! -L "$dest" ]; then + mkdir -p dotfiles-backup + mv "$dest" dotfiles-backup/. fi - ln -snf "$link_src" "$link_dest" + ln -snf "$src" "$dest" } -function deploy { - deploy_src="$1" - deploy_dest="$2" - if [ -e "$deploy_src/all" ]; then - for f in "$deploy_src/all"/* +function link_contents { + local src="$1" + local dest="$2" + if [ -e "$src" ]; then + for f in "$src"/* do - link "$(pwd)/$f" "$deploy_dest/$(basename "$f")" - done - fi - if [ -e "$deploy_src/$uname" ]; then - for f in "$deploy_src/$uname"/* - do - link "$(pwd)/$f" "$deploy_dest/$(basename "$f")" + link "$(pwd)/$f" "$dest/$(basename "$f")" done fi } +function deploy { + local src="$1" + local dest="$2" + link_contents "$src/all" "$dest" + link_contents "$src/$uname" "$dest" +} + # deploy to home -for f in home/* -do - link "$(pwd)/$f" "$HOME/.$(basename "$f")" -done +link_contents home "$HOME" # deploy to XDG dirs deploy config "$xconfig" @@ -92,10 +95,7 @@ if [ ! -e "$HOME/.vim/autoload/plug.vim" ]; then fi # deploy custom scripts to vim autoload -for f in vim/* -do - link "$(pwd)/$f" "$autoload/$(basename "$f")" -done +link_contents vim "$autoload" # create local files if [ ! -e "$HOME/.config/zsh/00-local.zsh" ]; then -- cgit v1.2.3