From f96dae021077a56a49e9e328fbdb986d22485798 Mon Sep 17 00:00:00 2001 From: Aaron Fiore Date: Sun, 22 Sep 2024 18:34:43 -0700 Subject: [PATCH] client: src: lib_gen: impl overhaul for new args - Rewrite to support new arguments * Related design changes and refactoring * Remove prompt for testing (use `dev=on` instead) - Factor out superscript gen from subscript append - Related print formatting changes - Add more internal checks --- .../container/shell/superscript.bash.in | 1 + client/src/docker/lib/internal/lib_gen.bash | 618 ++++++++++-------- 2 files changed, 359 insertions(+), 260 deletions(-) diff --git a/client/docker-finance.d/container/shell/superscript.bash.in b/client/docker-finance.d/container/shell/superscript.bash.in index 734e3c3..482c8a8 100644 --- a/client/docker-finance.d/container/shell/superscript.bash.in +++ b/client/docker-finance.d/container/shell/superscript.bash.in @@ -41,6 +41,7 @@ alias e='exit' alias grep='grep --color=auto' # All subprofiles' scripts +# TODO: remove this tag as it's no longer needed @DOCKER_FINANCE_SUBPROFILE_SOURCE@ # vim: syn=bash sw=2 sts=2 si ai et diff --git a/client/src/docker/lib/internal/lib_gen.bash b/client/src/docker/lib/internal/lib_gen.bash index afcd128..4f7f4ef 100644 --- a/client/src/docker/lib/internal/lib_gen.bash +++ b/client/src/docker/lib/internal/lib_gen.bash @@ -372,8 +372,6 @@ function lib_gen::gen() lib_utils::print_custom "\n" lib_utils::print_info "Generating client/container environment" - # TODO: this warning is likely not needed now that lib_env is ensured - # WARNING: client generation MUST be done before container generation lib_gen::__gen_client if [ $? -eq 0 ]; then @@ -384,10 +382,13 @@ function lib_gen::gen() fi if [ $? -eq 0 ]; then + lib_utils::print_custom "\n" lib_utils::print_info "Congratulations, environment sucessfully generated!" lib_utils::print_custom "\n" else + lib_utils::print_custom "\n" lib_utils::die_fatal "Environment not fully generated! Try again" + lib_utils::print_custom "\n" fi } @@ -401,79 +402,103 @@ function lib_gen::__gen_client() # # Client-side environment file # - # - [ -z "$global_env_file" ] && lib_utils::die_fatal - [ -z "$global_repo_env_file" ] && lib_utils::die_fatal + if [[ -z "${global_arg_type[*]}" || "${global_arg_type[*]}" =~ env ]]; then - # Backup existing file - if [ -f "$global_env_file" ]; then - lib_utils::print_custom " \e[32m│\e[0m\n" - lib_utils::print_custom " \e[32m├─\e[34;1m Client-side environment found, backup then generate new one? [Y/n] \e[0m" + [ -z "$global_env_file" ] && lib_utils::die_fatal + [ -z "$global_repo_env_file" ] && lib_utils::die_fatal - read -p "" _read - local _confirm="${_read:-y}" + # Backup existing file + if [ -f "$global_env_file" ]; then + lib_utils::print_custom " \e[32m│\e[0m\n" + lib_utils::print_custom " \e[32m├─\e[34;1m Client-side environment found, backup then generate new one? [Y/n] \e[0m" - if [[ "$_confirm" == [yY] ]]; then - cp -a "$global_env_file" "${global_env_file}_${global_suffix}" \ - || lib_utils::die_fatal + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" - # Get/Set with repository defaults - lib_utils::print_debug "Reading $global_repo_env_file" - lib_env::__read "$global_repo_env_file" + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then + cp -a "$global_env_file" "${global_env_file}_${global_suffix}" \ + || lib_utils::die_fatal - lib_utils::print_debug "Writing $global_env_file" - lib_env::__write "$global_env_file" + # Get/Set with repository defaults + lib_utils::print_debug "Reading $global_repo_env_file" + lib_env::__read "$global_repo_env_file" + + lib_utils::print_debug "Writing $global_env_file" + lib_env::__write "$global_env_file" + fi fi + + lib_utils::print_custom " \e[32m│ └─\e[34m Edit (new) environment now? [Y/n] \e[0m" + + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$global_env_file" + + # Get/Set new (edited) environment variables + lib_env::__read "$global_env_file" + lib_env::__set_client_globals + fi - lib_utils::print_custom " \e[32m│ └─\e[34m Edit (new) environment now? [Y/n] \e[0m" - read -p "" _read - local _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$global_env_file" - - # Get/Set new (edited) environment variables - lib_env::__read "$global_env_file" - lib_env::__set_client_globals - # # Custom (optional) Dockerfile # - [ -z "$global_custom_dockerfile" ] && lib_utils::die_fatal - [ -z "$global_repo_custom_dockerfile" ] && lib_utils::die_fatal + if [[ -z "${global_arg_type[*]}" || "${global_arg_type[*]}" =~ build ]]; then - # Backup existing custom Dockerfile - if [ -f "$global_custom_dockerfile" ]; then - lib_utils::print_custom " \e[32m│\e[0m\n" - lib_utils::print_custom " \e[32m├─\e[34;1m Custom (optional) Dockerfile found, backup then generate new one? [Y/n] \e[0m" + [ -z "$global_custom_dockerfile" ] && lib_utils::die_fatal + [ -z "$global_repo_custom_dockerfile" ] && lib_utils::die_fatal - read -p "" _read - local _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then - cp -a "$global_custom_dockerfile" "${global_custom_dockerfile}_${global_suffix}" || lib_utils::die_fatal - cp -fa "$global_repo_custom_dockerfile" "$global_custom_dockerfile" || lib_utils::die_fatal + # Backup existing custom Dockerfile + if [ -f "$global_custom_dockerfile" ]; then + lib_utils::print_custom " \e[32m│\e[0m\n" + lib_utils::print_custom " \e[32m├─\e[34;1m Custom (optional) Dockerfile found, backup then generate new one? [Y/n] \e[0m" + + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then + cp -a "$global_custom_dockerfile" "${global_custom_dockerfile}_${global_suffix}" || lib_utils::die_fatal + cp -fa "$global_repo_custom_dockerfile" "$global_custom_dockerfile" || lib_utils::die_fatal + fi + else + lib_utils::print_custom " \e[32m│\e[0m\n" + lib_utils::print_custom " \e[32m├─\e[34;1m Generating new custom (optional) Dockerfile\e[0m\n" + + lib_utils::print_debug "$global_repo_custom_dockerfile" + lib_utils::print_debug "$global_custom_dockerfile" + + cp -a "$global_repo_custom_dockerfile" "$global_custom_dockerfile" fi - else - lib_utils::print_custom " \e[32m│\e[0m\n" - lib_utils::print_custom " \e[32m├─\e[34;1m Generating new custom (optional) Dockerfile\e[0m\n" - lib_utils::print_debug "$global_repo_custom_dockerfile" - lib_utils::print_debug "$global_custom_dockerfile" + lib_utils::print_custom " \e[32m│ └─\e[34m Edit (new) custom Dockerfile now? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$global_custom_dockerfile" || return 0 - cp -a "$global_repo_custom_dockerfile" "$global_custom_dockerfile" fi - - lib_utils::print_custom " \e[32m│ └─\e[34m Edit (new) custom Dockerfile now? [Y/n] \e[0m" - read -p "" _read - local _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$global_custom_dockerfile" || return 0 } function lib_gen::__gen_container() { lib_utils::print_debug "Generating container" + # + # Generate joint client/container superscript + # + + if [[ -z "${global_arg_type[*]}" || "${global_arg_type[*]}" =~ superscript ]]; then + + lib_utils::print_custom " \e[32m│\e[0m\n" + lib_utils::print_custom " \e[32m├─\e[34;1m Generate (or update) joint client/container superscript? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then + lib_gen::__gen_superscript + fi + + fi + # # Generate plugins (custom) # @@ -489,23 +514,84 @@ function lib_gen::__gen_container() # Generate flow # - lib_utils::print_custom " \e[32m│\e[0m\n" - lib_utils::print_custom " \e[32m├─\e[34;1m Generate (or update) container flow (profiles, etc.)? [Y/n] \e[0m" - read -p "" _read - local _confirm="${_read:-y}" - if [[ "$_confirm" != [yY] ]]; then + if [[ "${global_arg_all[*]}" =~ all|type || "${global_arg_type[*]}" =~ flow ]]; then + lib_utils::print_custom " \e[32m│\e[0m\n" - lib_utils::print_normal "" - return 0 - fi + lib_utils::print_custom " \e[32m├─\e[34;1m Generate (or update) container finance flow (layout and profiles)? [Y/n] \e[0m" + # Prompt if type not given + if [[ ! "${global_arg_all[*]}" =~ all|type || -z "${global_arg_type[*]}" ]]; then + if [ -z "$global_arg_confirm" ]; then + lib_utils::print_custom "\n" + else + read -p "" _read + local _confirm="${_read:-y}" + if [[ "$_confirm" != [yY] ]]; then + lib_utils::print_custom " \e[32m│\e[0m\n" + return 0 + fi + fi + fi - [ -z "$DOCKER_FINANCE_CLIENT_FLOW" ] && lib_utils::die_fatal - if [ ! -d "$DOCKER_FINANCE_CLIENT_FLOW" ]; then - mkdir -p "$DOCKER_FINANCE_CLIENT_FLOW" || lib_utils::die_fatal - fi + [ -z "$DOCKER_FINANCE_CLIENT_FLOW" ] && lib_utils::die_fatal + if [ ! -d "$DOCKER_FINANCE_CLIENT_FLOW" ]; then + mkdir -p "$DOCKER_FINANCE_CLIENT_FLOW" || lib_utils::die_fatal + fi - lib_gen::__gen_times - lib_gen::__gen_profile + lib_gen::__gen_times + lib_gen::__gen_profile + + fi +} + +# +# Generate joint client/container superscript +# + +function lib_gen::__gen_superscript() +{ + [ -z "$global_shell_file" ] && lib_utils::die_fatal + + if [ -f "$global_shell_file" ]; then + lib_utils::print_custom " \e[32m│ └─\e[34m Backup existing superscript and generate a new one? [N/y] \e[0m" + + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-n}" + + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then + # Backup + local -r _backup=("cp" "-a" "$global_shell_file" "${global_shell_file}_${global_suffix}") + lib_utils::print_debug "${_backup[@]}" + "${_backup[@]}" || lib_utils::die_fatal + # Write + lib_gen::__gen_superscript_write + fi + local _print_custom=" \e[32m│ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" + else + # Generate new default file + lib_gen::__gen_superscript_write + local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" + fi + lib_utils::print_custom "$_print_custom" + + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$global_shell_file" +} + +function lib_gen::__gen_superscript_write() +{ + [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal + lib_utils::print_debug "global_repo_conf_dir=${global_repo_conf_dir}" + + [ -z "$global_client_version" ] && lib_utils::die_fatal + lib_utils::print_debug "global_client_version=${global_client_version}" + + [ -z "$global_shell_file" ] && lib_utils::die_fatal + lib_utils::print_debug "global_shell_file=${global_shell_file}" + + sed \ + -e "s:@DOCKER_FINANCE_VERSION@:${global_client_version}:g" \ + "${global_repo_conf_dir}/container/shell/superscript.bash.in" >"$global_shell_file" } # @@ -518,6 +604,8 @@ function lib_gen::__gen_container() function lib_gen::__gen_plugins() { + [ -z "$DOCKER_FINANCE_CLIENT_PLUGINS" ] && lib_utils::die_fatal + lib_utils::print_debug "Generating custom plugins layout" local -r _client="${DOCKER_FINANCE_CLIENT_PLUGINS}/client" @@ -567,39 +655,56 @@ function lib_gen::__gen_profile() mkdir -p "$_profiles" || lib_utils::die_fatal fi - # - # Profile: development setup - # - - lib_utils::print_custom " \e[32m│ │\e[0m\n" - lib_utils::print_custom " \e[32m│ ├─\e[34;1m Will this profile be used for development and/or demonstration? [N/y] \e[0m" - read -p "" _read - local _is_testing="${_read:-false}" - if [[ "$_read" == [yY] ]]; then - _is_testing=true - fi - lib_utils::print_debug "_is_testing=${_is_testing}" - # # Profile: construct full path to subprofile # + [ -z "$global_user" ] && lib_utils::die_fatal + + # Profile lib_utils::print_custom " \e[32m│ │ │\e[0m\n" lib_utils::print_custom " \e[32m│ │ ├─\e[34m Enter profile name (e.g., family in 'family/alice'): \e[0m" - read -p "" _read + if [ ! -z "$global_arg_profile" ]; then + lib_utils::print_custom "\n" + _read="$global_arg_profile" + else + if [ -z "$global_arg_confirm" ]; then + lib_utils::print_custom "\n" + _read="" + else + read -p "" _read + fi + fi local -r _profile="${_read:-default}" lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Using profile:\e[0m ${_profile}\n" + # Subprofile lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ └─\e[34m Enter subprofile name (e.g., alice in 'family/alice'): \e[0m" - read -p "" _read + lib_utils::print_custom " \e[32m│ │ ├─\e[34m Enter subprofile name (e.g., alice in 'family/alice'): \e[0m" + if [ ! -z "$global_arg_subprofile" ]; then + lib_utils::print_custom "\n" + _read="$global_arg_subprofile" + else + if [ -z "$global_arg_confirm" ]; then + lib_utils::print_custom "\n" + _read="" + else + read -p "" _read + fi + fi local -r _subprofile="${_read:-${global_user}}" - lib_utils::print_custom " \e[32m│ │ └─\e[34m Using subprofile:\e[0m ${_subprofile}\n" + lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Using subprofile:\e[0m ${_subprofile}\n" + # Check if full profile exists if [ -d "${_profiles}/${_profile}/${_subprofile}" ]; then - lib_utils::print_custom " \e[32m│ │ └─\e[34m Subprofile exists! Continue with backup and generation? [Y/n] \e[0m" + lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Subprofile exists! Continue with backup and generation? [Y/n] \e[0m" - read -p "" _read + if [ -z "$global_arg_confirm" ]; then + lib_utils::print_custom "\n" + _read="" + else + read -p "" _read + fi _confirm="${_read:-y}" if [[ "$_confirm" != [yY] ]]; then lib_utils::print_custom " \e[32m│\e[0m\n" @@ -609,96 +714,16 @@ function lib_gen::__gen_profile() fi # - # Profile: execute profile-specific + # Profile: generate profile-specific # - lib_utils::print_custom " \e[32m│ │\e[0m\n" - lib_utils::print_custom " \e[32m│ ├─\e[34;1m Generate (or update) joint client/container shell script (superscript)? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then - local _gen_path="\${DOCKER_FINANCE_CONTAINER_FLOW}/profiles/${_profile}/${_subprofile}" # \$ for container view - local _gen_conf_path="${_gen_path}/docker-finance.d" - lib_utils::print_debug "_gen_path=${_gen_path}" - lib_utils::print_debug "_gen_conf_path=${_gen_conf_path}" + local -r _gen_path="${_profiles}/${_profile}/${_subprofile}" + lib_utils::print_debug "_gen_path=${_gen_path}" - lib_gen::__gen_subprofile_shell "$_gen_path" "$_gen_conf_path" - fi + local -r _gen_conf_path="${_gen_path}/docker-finance.d" + lib_utils::print_debug "_gen_conf_path=${_gen_conf_path}" - lib_utils::print_custom " \e[32m│ │\e[0m\n" - lib_utils::print_custom " \e[32m│ ├─\e[34;1m Generate (or update) container flow configs and/or accounts? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then - local _gen_path="${_profiles}/${_profile}/${_subprofile}" - local _gen_conf_path="${_gen_path}/docker-finance.d" - lib_utils::print_debug "_gen_path=${_gen_path}" - lib_utils::print_debug "_gen_conf_path=${_gen_conf_path}" - - lib_gen::__gen_subprofile_flow "$_gen_path" "$_gen_conf_path" - fi -} - -# -# Subprofile: generate client-side container superscript -# TODO: separate superscript gen from subprofile append -# - -function lib_gen::__gen_subprofile_shell() -{ - [ -z "$global_shell_file" ] && lib_utils::die_fatal - - local -r _gen_path="$1" - local -r _gen_conf_path="$2" - - # Subprofile source added to superscript - local -r _source="source \"${_gen_conf_path}/shell/subprofile.bash\"" - - local _file="$global_shell_file" - if [ -f "$_file" ]; then - # Append subprofile source to existing file - lib_utils::print_custom " \e[32m│ │ └─\e[34m Shell superscript found, append new subprofile 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 - # 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 - # Backup - local -r _backup=("cp" "-a" "$_file" "${_file}_${global_suffix}") - lib_utils::print_debug "${_backup[@]}" - "${_backup[@]}" || lib_utils::die_fatal - # Write - lib_gen::__gen_subprofile_shell_write - fi - local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" - else - # Generate new default file - lib_gen::__gen_subprofile_shell_write - local _print_custom=" \e[32m│ │ └─\e[34m Edit (new) superscript now? [Y/n] \e[0m" - fi - lib_utils::print_custom "$_print_custom" - read -p "" _read - local _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$_file" -} - -function lib_gen::__gen_subprofile_shell_write() -{ - [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal - lib_utils::print_debug "global_repo_conf_dir=${global_repo_conf_dir}" - - [ -z "$global_client_version" ] && lib_utils::die_fatal - lib_utils::print_debug "global_client_version=${global_client_version}" - - sed \ - -e "s:@DOCKER_FINANCE_SUBPROFILE_SOURCE@:${_source}:g" \ - -e "s:@DOCKER_FINANCE_VERSION@:${global_client_version}:g" \ - "${global_repo_conf_dir}/container/shell/superscript.bash.in" >"$_file" + lib_gen::__gen_subprofile_flow } # @@ -707,38 +732,58 @@ function lib_gen::__gen_subprofile_shell_write() function lib_gen::__gen_subprofile_flow() { - local -r _gen_path="$1" - local -r _gen_conf_path="$2" + [[ -z "$_gen_path" || -z "$_gen_conf_path" ]] && lib_utils::die_fatal - lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's shell script? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && lib_gen::__gen_subprofile_flow_shell + if lib_gen::__gen_subprofile_flow_args_config "subprofile"; then + lib_utils::print_custom " \e[32m│ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's shell script? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then + # Subprofile's shell script + lib_gen::__gen_subprofile_flow_shell + # Append subprofile source to superscript + lib_gen::__gen_subprofile_flow_superscript + fi + fi - lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's fetch configuration? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && lib_gen::__gen_subprofile_flow_fetch + if lib_gen::__gen_subprofile_flow_args_config "fetch"; then + # Prompt for default generation + lib_utils::print_custom " \e[32m│ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's fetch configuration file? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && lib_gen::__gen_subprofile_flow_fetch + fi - lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's financial metadata? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && lib_gen::__gen_subprofile_flow_meta + if lib_gen::__gen_subprofile_flow_args_config "meta"; then + lib_utils::print_custom " \e[32m│ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's financial metadata file? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && lib_gen::__gen_subprofile_flow_meta + fi - lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's hledger configuration file? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && lib_gen::__gen_subprofile_hledger "$_gen_path" "$_gen_conf_path" + if lib_gen::__gen_subprofile_flow_args_config "hledger"; then + lib_utils::print_custom " \e[32m│ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's hledger configuration file? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && lib_gen::__gen_subprofile_flow_hledger + fi - lib_utils::print_custom " \e[32m│ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's hledger-flow accounts? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && lib_gen::__gen_subprofile_flow_accounts + if lib_gen::__gen_subprofile_flow_args_account; then + lib_utils::print_custom " \e[32m│ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ ├─\e[34;1m Generate (or update) subprofile's hledger-flow accounts? [Y/n] \e[0m" + if [ ! -z "${global_arg_account[*]}" ]; then + lib_utils::print_custom "\n" + lib_gen::__gen_subprofile_flow_accounts + else + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && lib_gen::__gen_subprofile_flow_accounts + fi + fi lib_utils::print_custom " \e[32m│\e[0m\n" @@ -752,22 +797,41 @@ function lib_gen::__gen_subprofile_flow() lib_utils::print_custom " \e[32m│\e[0m\n" } +function lib_gen::__gen_subprofile_flow_args_config() +{ + [ -z "$1" ] && lib_utils::die_fatal + if [[ -z "${global_arg_type[*]}" || ("${global_arg_type[*]}" =~ flow && -z "$global_arg_profile") || ("${global_arg_type[*]}" =~ flow && ! -z "$global_arg_profile" && -z "${global_arg_account[*]}" && -z "${global_arg_config[*]}") || "${global_arg_config[*]}" =~ $1 ]]; then + return 0 + fi + return 1 +} + +function lib_gen::__gen_subprofile_flow_args_account() +{ + if [[ -z "${global_arg_type[*]}" || ("${global_arg_type[*]}" =~ flow && -z "$global_arg_profile") || ("${global_arg_type[*]}" =~ flow && ! -z "$global_arg_profile" && -z "${global_arg_account[*]}" && -z "${global_arg_config[*]}") || ("${global_arg_type[*]}" =~ flow && ! -z "$global_arg_profile" && ! -z "${global_arg_account[*]}") ]]; then + return 0 + fi + return 1 +} + # # Subprofile: flow: generate subprofile script # function lib_gen::__gen_subprofile_flow_shell() { + [[ -z "$_gen_path" || -z "$_gen_conf_path" ]] && lib_utils::die_fatal + local _dir="${_gen_conf_path}/shell" [ ! -d "$_dir" ] && mkdir -p "$_dir" local _file="${_dir}/subprofile.bash" if [ -f "$_file" ]; then lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Subprofile script found, backup then generate new one? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then cp -a "$_file" "${_file}_${global_suffix}" || lib_utils::die_fatal lib_gen::__gen_subprofile_flow_shell_write @@ -779,14 +843,20 @@ function lib_gen::__gen_subprofile_flow_shell() fi lib_utils::print_custom "$_print_custom" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$_file" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$_file" } function lib_gen::__gen_subprofile_flow_shell_write() { - [ -z "$DOCKER_FINANCE_CONTAINER_CMD" ] && lib_utils::die_fatal + [[ -z "$DOCKER_FINANCE_CONTAINER_REPO" || -z "$DOCKER_FINANCE_CONTAINER_CMD" ]] && lib_utils::die_fatal + + [[ -z "$_profile" || -z "$_subprofile" || -z "$_file" ]] && lib_utils::die_fatal + + [ -z "$global_client_version" ] && lib_utils::die_fatal + [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal + sed \ -e "s:@DOCKER_FINANCE_CONTAINER_CMD@:${DOCKER_FINANCE_CONTAINER_CMD}:g" \ -e "s:@DOCKER_FINANCE_CONTAINER_REPO@:${DOCKER_FINANCE_CONTAINER_REPO}:g" \ @@ -796,23 +866,49 @@ function lib_gen::__gen_subprofile_flow_shell_write() "${global_repo_conf_dir}/container/shell/subprofile.bash.in" >"$_file" } +# +# Subprofile: flow: append subscript to superscript +# + +function lib_gen::__gen_subprofile_flow_superscript() +{ + [[ -z "$_profile" || -z "$_subprofile" ]] && lib_utils::die_fatal + + [ -z "$global_shell_file" ] && lib_utils::die_fatal + [ ! -f "$global_shell_file" ] && lib_utils::die_fatal "Superscript does not exist!" + + # Append subprofile source to superscript + local -r _source="source \"\${DOCKER_FINANCE_CONTAINER_FLOW}/profiles/${_profile}/${_subprofile}/docker-finance.d/shell/subprofile.bash\"" + lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Appending subprofile to superscript\e[0m\n" + + # If source subprofile does not exist, append + grep "$_source" "$global_shell_file" >&/dev/null \ + || sed -i "$(wc -l <$global_shell_file)i\\$_source\\" "$global_shell_file" + + lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Edit superscript now? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + local _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$global_shell_file" +} + # # Subprofile: flow: generate fetch config # function lib_gen::__gen_subprofile_flow_fetch() { - local _dir="${_gen_conf_path}/fetch" + [[ -z "$_gen_path" || -z "$_gen_conf_path" ]] && lib_utils::die_fatal + + local -r _dir="${_gen_conf_path}/fetch" [ ! -d "$_dir" ] && mkdir -p "$_dir" - local _file="${_dir}/fetch.yaml" + local -r _file="${_dir}/fetch.yaml" if [ -f "$_file" ]; then - lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Subprofile's fetch configuration found, backup then generate new one? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then cp -a "$_file" "${_file}_${global_suffix}" || lib_utils::die_fatal lib_gen::__gen_subprofile_flow_fetch_write @@ -824,20 +920,17 @@ function lib_gen::__gen_subprofile_flow_fetch() fi lib_utils::print_custom "$_print_custom" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$_file" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$_file" } function lib_gen::__gen_subprofile_flow_fetch_write() { - [ -z "$_profile" ] && lib_utils::die_fatal - [ -z "$_subprofile" ] && lib_utils::die_fatal - - [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal - [ -z "$_file" ] && lib_utils::die_fatal + [[ -z "$_profile" || -z "$_subprofile" || -z "$_file" ]] && lib_utils::die_fatal [ -z "$global_client_version" ] && lib_utils::die_fatal + [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal sed \ -e "s:@DOCKER_FINANCE_VERSION@:${global_client_version}:g" \ @@ -852,6 +945,8 @@ function lib_gen::__gen_subprofile_flow_fetch_write() function lib_gen::__gen_subprofile_flow_meta() { + [[ -z "$_gen_path" || -z "$_gen_conf_path" ]] && lib_utils::die_fatal + local _dir="${_gen_conf_path}/meta" [ ! -d "$_dir" ] && mkdir -p "$_dir" @@ -859,10 +954,10 @@ function lib_gen::__gen_subprofile_flow_meta() if [ -f "$_file" ]; then lib_utils::print_custom " \e[32m│ │ │ └─\e[34m Subprofile's financial metadata found, backup then generate new one? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then cp -a "$_file" "${_file}_${global_suffix}" || lib_utils::die_fatal lib_gen::__gen_subprofile_flow_meta_write @@ -874,13 +969,18 @@ function lib_gen::__gen_subprofile_flow_meta() fi lib_utils::print_custom "$_print_custom" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$_file" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$_file" } function lib_gen::__gen_subprofile_flow_meta_write() { + [ -z "$_file" ] && lib_utils::die_fatal + + [ -z "$global_client_version" ] && lib_utils::die_fatal + [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal + # Deletes default comments or else ROOT meta sample won't work out-of-the-box sed \ -e "/\/\/\\!/d" \ @@ -894,8 +994,7 @@ function lib_gen::__gen_subprofile_flow_meta_write() function lib_gen::__gen_subprofile_flow_hledger() { - local -r _gen_path="$1" - local -r _gen_conf_path="$2" + [[ -z "$_gen_path" || -z "$_gen_conf_path" ]] && lib_utils::die_fatal local _dir="${_gen_conf_path}/hledger" [ ! -d "$_dir" ] && mkdir -p "$_dir" @@ -903,37 +1002,36 @@ function lib_gen::__gen_subprofile_flow_hledger() local _file="${_dir}/hledger.conf" if [ -f "$_file" ]; then lib_utils::print_custom " \e[32m│ │ │ └─\e[34m hledger configuration found, backup then generate new one? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - if [[ "$_confirm" == [yY] ]]; then + if [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]]; then cp -a "$_file" "${_file}_${global_suffix}" || lib_utils::die_fatal - lib_gen::__gen_subprofile_hledger_write "$_file" + lib_gen::__gen_subprofile_flow_hledger_write local _print_custom=" \e[32m│ │ │ └─\e[34m Edit (new) hledger configuration now? [Y/n] \e[0m" fi else - lib_gen::__gen_subprofile_hledger_write "$_file" + lib_gen::__gen_subprofile_flow_hledger_write local _print_custom=" \e[32m│ │ │ └─\e[34m Edit (new) hledger configuration now? [Y/n] \e[0m" fi lib_utils::print_custom "$_print_custom" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] && $EDITOR "$_file" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] && $EDITOR "$_file" } function lib_gen::__gen_subprofile_flow_hledger_write() { - local _file="$1" [ -z "$_file" ] && lib_utils::die_fatal - [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal - lib_utils::print_debug "global_repo_conf_dir=${global_repo_conf_dir}" - [ -z "$global_client_version" ] && lib_utils::die_fatal lib_utils::print_debug "global_client_version=${global_client_version}" + [ -z "$global_repo_conf_dir" ] && lib_utils::die_fatal + lib_utils::print_debug "global_repo_conf_dir=${global_repo_conf_dir}" + sed \ -e "s:@DOCKER_FINANCE_VERSION@:${global_client_version}:g" \ "${global_repo_conf_dir}/container/hledger/hledger.conf.in" >"$_file" @@ -945,6 +1043,8 @@ function lib_gen::__gen_subprofile_flow_hledger_write() function lib_gen::__gen_subprofile_flow_accounts() { + [[ -z "$_gen_path" || -z "$_gen_conf_path" || -z "$_subprofile" ]] && lib_utils::die_fatal + lib_utils::print_debug "Generating accounts" local _subprofile_path="${_gen_path}/import/${_subprofile}" @@ -952,51 +1052,49 @@ function lib_gen::__gen_subprofile_flow_accounts() if [ -d "$_subprofile_path" ]; then lib_utils::print_custom " \e[32m│ │ │ ├─\e[34m Subprofile import path exists! Continue with account generation (files will be backed up)? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] || return 0 + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] || return 0 else mkdir -p "$_subprofile_path" fi - # docker-finance templates + # All available hledger-flow account paths local _templates_paths - mapfile -t _templates_paths < <(find ${DOCKER_FINANCE_CLIENT_REPO}/container/src/hledger-flow/accounts -name template | sort) + mapfile -t _templates_paths < <(find "${DOCKER_FINANCE_CLIENT_REPO}"/container/src/hledger-flow/accounts -name template | sort) declare -r _templates_paths - lib_utils::print_debug "_templates_paths=${_templates_paths[*]}" - lib_utils::print_custom " \e[32m│ │ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ │ ├─\e[34;1m Generate individual subprofile accounts instead of generating them all at once? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - - # Cycle through all available docker-finance templates and populate subprofile - if [[ $_confirm == [yY] ]]; then - for _template_path in "${_templates_paths[@]}"; do - lib_utils::print_debug "_template_path=${_template_path}" - lib_utils::print_custom " \e[32m│ │ │ │ │\e[0m\n" - lib_utils::print_custom " \e[32m│ │ │ │ ├─\e[34m\e[1m Generate $(echo $_template_path | rev | cut -d/ -f2 | rev) ? [Y/n] \e[0m" - read -p "" _read - _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] \ - && lib_gen::__gen_subprofile_flow_accounts_populate "$_subprofile_path" "$_template_path" "$_is_testing" + # All available hledger-flow account names + local _accounts + if [ -z "${global_arg_account[*]}" ]; then + for _template in "${_templates_paths[@]}"; do + _accounts+=("$(echo "$_template" | rev | cut -d/ -f2 | rev)") done else - for _template_path in "${_templates_paths[@]}"; do - lib_utils::print_debug "$_subprofile_path $_template_path $_is_testing" - lib_gen::__gen_subprofile_flow_accounts_populate "$_subprofile_path" "$_template_path" "$_is_testing" - done + declare -r _accounts=("${global_arg_account[@]}") fi + + # Cycle through all available docker-finance templates and populate subprofile + for _account in "${_accounts[@]}"; do + for _template_path in "${_templates_paths[@]}"; do + if [[ "$_template_path" =~ \/$_account\/ ]]; then + lib_utils::print_debug "_template_path=${_template_path}" + lib_utils::print_custom " \e[32m│ │ │ │ │\e[0m\n" + lib_utils::print_custom " \e[32m│ │ │ │ ├─\e[34m\e[1m Generate $_account ? [Y/n] \e[0m" + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read + _confirm="${_read:-y}" + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] \ + && lib_gen::__gen_subprofile_flow_accounts_populate "$_subprofile_path" "$_template_path" + fi + done + done } function lib_gen::__gen_subprofile_flow_accounts_populate() { - [ -z "$1" ] && lib_utils::die_fatal - [ -z "$2" ] && lib_utils::die_fatal - [ -z "$3" ] && lib_utils::die_fatal + [[ -z "$1" || -z "$2" ]] && lib_utils::die_fatal local _subprofile_path="$1" local _template_path="$2" - local _is_testing="$3" # Continue if hledger-flow account exists local _continue @@ -1009,9 +1107,9 @@ function lib_gen::__gen_subprofile_flow_accounts_populate() # TODO: doesn't check for blockchain explorer shared rules/bash if [ -d "$_account_path" ]; then lib_utils::print_custom " \e[32m│ │ │ │ │ └─\e[34m Account exists! Continue with backup and generation? [Y/n] \e[0m" - read -p "" _read + [ -z "$global_arg_confirm" ] && lib_utils::print_custom "\n" || read -p "" _read _confirm="${_read:-y}" - [[ "$_confirm" == [yY] ]] || _continue=false + [[ "$_confirm" == [yY] || -z "$global_arg_confirm" ]] || _continue=false fi # Populate with template @@ -1037,7 +1135,7 @@ function lib_gen::__gen_subprofile_flow_accounts_populate() local _mockup="${_in_path}/mockup" lib_utils::print_debug "Getting mockup '${_mockup}'" - if [[ $_is_testing == true ]]; then + if [ ! -z "$global_arg_dev" ]; then lib_utils::print_debug "Copying mockup to '${_in_path}'" cp -a -R --backup --suffix="_${global_suffix}" "${_mockup}"/* "${_in_path}/" || lib_utils::die_fatal fi