From 0a91afa541f7fd339a030b3d9d52437d7ca6030f Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Tue, 23 Jan 2024 11:08:08 +0100 Subject: [PATCH] Create util_lib.sh which contains some of the code from upgrade that can be reused to create the restore script --- upgrade.sh | 35 +++-------------------------------- util_lib.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 32 deletions(-) create mode 100644 util_lib.sh diff --git a/upgrade.sh b/upgrade.sh index 55fb85c..3c1fc6f 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -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 diff --git a/util_lib.sh b/util_lib.sh new file mode 100644 index 0000000..c7f069c --- /dev/null +++ b/util_lib.sh @@ -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 +}