client: lib_gen: superscript: append subprofile before generating

- Append new subprofile before potentially generating new file
- Simplifies backup/writing process
- Related refactoring
This commit is contained in:
2024-07-15 18:10:52 -07:00
parent 90e7d0adb2
commit f0561d1ebe

View File

@@ -461,40 +461,31 @@ function lib_gen::__gen_shell()
local _file="$global_shell_file" local _file="$global_shell_file"
if [ -f "$_file" ]; then if [ -f "$_file" ]; then
# Backup # Append subprofile source to existing file
lib_utils::print_custom " \e[32m│ │ └─\e[34m Shell superscript found, backup before continuing? [Y/n] \e[0m" lib_utils::print_custom " \e[32m│ │ └─\e[34m Shell superscript found, append new subprofile to existing file? [Y/n] \e[0m"
read -p "" _read read -p "" _read
local _confirm="${_read:-y}" local _confirm="${_read:-y}"
if [[ "$_confirm" == [yY] ]]; then
grep "$_source" "$_file" >&/dev/null || sed -i "$(wc -l <$_file)i\\$_source\\" "$_file"
fi
# Or generate new file
lib_utils::print_custom " \e[32m│ │ └─\e[34m Backup existing superscript and generate a new one? [N/y] \e[0m"
read -p "" _read
local _confirm="${_read:-n}"
if [[ "$_confirm" == [yY] ]]; then if [[ "$_confirm" == [yY] ]]; then
# Backup # Backup
local -r _backup=("cp" "-a" "$_file" "${_file}_${global_suffix}") local -r _backup=("cp" "-a" "$_file" "${_file}_${global_suffix}")
lib_utils::print_debug "${_backup[@]}" lib_utils::print_debug "${_backup[@]}"
"${_backup[@]}" || lib_utils::die_fatal "${_backup[@]}" || lib_utils::die_fatal
fi # Write
lib_utils::print_custom " \e[32m│ │ └─\e[34m Overwrite existing file? [Y/n] \e[0m"
read -p "" _read
local _confirm="${_read:-y}"
if [[ "$_confirm" == [yY] ]]; then
# Overwrite existing file
lib_gen::__gen_shell_write lib_gen::__gen_shell_write
else
# Append subprofile source to existing file
lib_utils::print_custom " \e[32m│ │ └─\e[34m Append to existing file? [Y/n] \e[0m"
read -p "" _read
local _confirm="${_read:-y}"
if [[ "$_confirm" == [yY] ]]; then
grep "$_source" "$_file" >&/dev/null || sed -i "$(wc -l <$_file)i\\$_source\\" "$_file"
fi
fi fi
local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m"
else else
# Create new file # Generate new default file
lib_gen::__gen_shell_write lib_gen::__gen_shell_write
local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m"
fi fi
lib_utils::print_custom "$_print_custom" lib_utils::print_custom "$_print_custom"
read -p "" _read read -p "" _read
local _confirm="${_read:-y}" local _confirm="${_read:-y}"