Create util_lib.sh which contains some of the code from upgrade that can be reused to create the restore script

This commit is contained in:
Martin Larsson 2024-01-23 11:08:08 +01:00
parent bc7a3f691d
commit 0a91afa541
2 changed files with 36 additions and 32 deletions

View file

@ -1,42 +1,13 @@
#!/bin/zsh
LOCK_ROOT=".lock"
# Define a function to create key-value pairs
create_mapping() {
local input="$1"
local output="$2"
echo "$input|$output"
}
# Define an array of key-value pairs
mappings=(
$(create_mapping "nvim" "$HOME/.config")
$(create_mapping "kitty" "$HOME/.config")
$(create_mapping ".zshrc" "$HOME")
$(create_mapping "yabai" "$HOME/.config")
$(create_mapping "yabai_launcher" "/private/etc/sudoers.d")
)
source ./util_lib.sh
# Enable the NULL_GLOB option to handle cases where no subdirectories exist
setopt NULL_GLOB
# Figure out the name for the lock-dir.
mkdir -p "$LOCK_ROOT"
latest_lock=0
for subdir in "$LOCK_ROOT"/*/; do
subdir_name=${subdir%"${subdir##*[!/]}"}
subdir_name=${subdir_name##*/}
if [[ $subdir_name =~ ^[0-9]+$ ]] && ((subdir_name > latest_lock)); then
latest_lock=$subdir_name
fi
done
((latest_lock++))
lock_dir="$LOCK_ROOT"/"$latest_lock"
# Create a lock dir for our current configs.
((latest_lock=$(get_latest_lock) + 1))
lock_dir="$LOCK_ROOT"/"$latest_lock"
mkdir -p "$lock_dir"
# Iterate through the array of key-value pairs

33
util_lib.sh Normal file
View file

@ -0,0 +1,33 @@
#!/bin/zsh
LOCK_ROOT=".lock"
# Define a function to create key-value pairs
create_mapping() {
local input="$1"
local output="$2"
echo "$input|$output"
}
# Define an array of key-value pairs
mappings=(
$(create_mapping "nvim" "$HOME/.config")
$(create_mapping "kitty" "$HOME/.config")
$(create_mapping ".zshrc" "$HOME")
$(create_mapping "yabai" "$HOME/.config")
$(create_mapping "yabai_launcher" "/private/etc/sudoers.d")
)
function get_latest_lock() {
local latest_lock=0
for subdir in "$LOCK_ROOT"/*/; do
subdir_name=${subdir%"${subdir##*[!/]}"}
subdir_name=${subdir_name##*/}
if [[ $subdir_name =~ ^[0-9]+$ ]] && ((subdir_name > latest_lock)); then
latest_lock=$subdir_name
fi
done
echo $latest_lock
}