From 114a178238facf0816bd55c4805633162f85e2b6 Mon Sep 17 00:00:00 2001 From: Grigory Alexandrov Date: Wed, 18 Oct 2023 01:54:25 +0300 Subject: [PATCH] Refactoring --- install.sh | 2 +- stitchocker.sh | 682 ++++++++++++++++++++++++------------------------- 2 files changed, 342 insertions(+), 342 deletions(-) diff --git a/install.sh b/install.sh index e94d1a7..f13837f 100755 --- a/install.sh +++ b/install.sh @@ -7,7 +7,7 @@ install() { local destination_path="/tmp" local bin_path="/usr/local/bin" - local stitchocker_stable_release="1.2.1" + local stitchocker_stable_release="1.2.2" local stitchoker_uri="https://raw.githubusercontent.com/alexaandrov/stitchocker/$stitchocker_stable_release/stitchocker.sh" local stitchocker_name="stitchocker" local stitchocker_tmp_path="$destination_path/$stitchocker_name.sh" diff --git a/stitchocker.sh b/stitchocker.sh index 2ba3fcb..99d4d2d 100755 --- a/stitchocker.sh +++ b/stitchocker.sh @@ -9,281 +9,281 @@ ###################################################################################################### stitchocker() { - local self="stitchocker" - local version="1.2.1" - local version_info="Stitchocker version $version" - local help=" - Usage: - $self [--verbose|--debug] [-a ] [docker-compose COMMAND] [SETS...] - $self -h|--help - $self -v|--version - - Options: - -h|--help Shows this help text - -v|--version Shows $self version - --update Updates $self to the latest stable version - --debug Runs all commands in debug mode - --verbose Runs all commands in verbose mode - -p Path to stitching directory - -a Alias to stitching directory - - Examples: - $self up - $self up default backend frontend - $self -a my-projects-alias-from-env up default backend frontend - $self --debug -a my-projects-alias-from-env up default backend frontend - $self --verbose -a my-projects-alias-from-env up default backend frontend - " - - local debug_env=""$self"_debug" - local debug=$(env $debug_env) - if [[ ! -z $debug && $debug == true ]]; then - debug=true - else - debug=false - fi - - local verbose_env=""$self"_verbose" - local verbose=$(env $verbose_env) - if [[ ! -z $verbose && $verbose == true ]]; then - verbose=true - else - verbose=false - fi - - local path_flag="-p" - local exec="$self $path_flag" - - if [ $# -lt 1 ]; then - info "$help" - exit 1 - fi - - # Entrypoint - case $1 in - # -------------------------------------------------------------- - # Help info - # -------------------------------------------------------------- - "-h" | "--help") - info --exit "$help" - ;; - # -------------------------------------------------------------- - # Version info - # -------------------------------------------------------------- - "-v" | "--version") - info --exit "$version_info" - ;; - # -------------------------------------------------------------- - # Updates to the latest stable version - # -------------------------------------------------------------- - "--update") - sudo bash -c "$(curl -H 'Cache-Control: no-cache' -fsSL https://raw.githubusercontent.com/alexaandrov/stitchocker/master/install.sh)" - exit 0 - ;; - # -------------------------------------------------------------- - # Runs all commands in debug mode - # -------------------------------------------------------------- - "--debug") - local debug_export="export $(echo $debug_env | awk '{print toupper($0)}')" - eval "$debug_export=true" - $self ${@:2} - eval "$debug_export=false" - ;; - # -------------------------------------------------------------- - # Runs all commands in verbose mode - # -------------------------------------------------------------- - "--verbose") - local verbose_export="export $(echo $verbose_env | awk '{print toupper($0)}')" - eval "$verbose_export=true" - $self ${@:2} - eval "$verbose_export=false" - ;; - # -------------------------------------------------------------- - # The entry point for all commands - # -------------------------------------------------------------- - $path_flag) - # Function arguments - local path="$2" - local command="$3" - local first_flag="$4" - local second_flag="$5" - local flags="${@:4}" - local config_env="$self.env" - - if [[ -z $path ]]; then - error "Path not specified" - fi - - if [[ -z $command ]]; then - error "Command not specified" - fi - - if [[ $command == "down" ]]; then - $self -p $path stop $flags - fi - - if [[ $command == "reload" ]]; then - $self -p $path down $flags - $self -p $path up $flags - exit 1 - fi - - local available_config_names=("$self.yml" "$self.yaml" "docker-compose.yaml" "docker-compose.yml") - - local custom_config_env=""$self"_config" - local custom_config_name=$(env $custom_config_env) - if [[ ! -z $custom_config_name && $custom_config_name != "null" ]]; then - available_config_names+=($custom_config_name) - fi - - local config_path="" - for config_name in ${available_config_names[@]}; do - if [[ -f "$path/$config_name" ]]; then - config_path="$path/$config_name" - fi - done - - if [[ ! -f $config_path ]]; then - error --no-exit "No config found for: $path" - info "Available config names: ${available_config_names[@]}" - exit 1 - fi - - local default_set=$(env stitchocker_default_set) - if [[ ! -z $default_set && $default_set != "null" ]]; then - default_set="$default_set" - else - default_set="default" - fi - - local sets_field="${self}_config_sets" - - create_yaml_variables $config_path "${self}_config_" - - local sets_data="$(eval echo \$${sets_field}_${default_set})" - - if [[ ! -z $sets_data ]]; then - if [[ ! -z $first_flag ]]; then - if [[ ! -z $second_flag ]]; then - for set in $flags; do - eval "$exec $path $command $set" - done - exit 1 - fi - local set="$first_flag" - else - local set="$default_set" - fi - - local services="$(eval echo \${${sets_field}_${set}[*]})" - - if [[ -z $services ]]; then - error "Your config doesn't have \"$set\" value" - fi - - info "$(echo "$command" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}') $set set:" - - for service_alias in ${services}; do - local service_alias_head="$(echo $service_alias | head -c 1)" - if [[ $service_alias == *"/"* ]]; then - if [[ $service_alias_head == "@" ]]; then - local service_alias="${service_alias//@/}" - local service_path="$(env $service_alias)" - elif [[ $service_alias_head == "/" || $service_alias_head == "~" ]]; then - local service_path="$service_alias" - else - local service_path="$path/$service_alias" - fi - - local cmd="$exec $service_path $command" - if [[ $command != "up" ]]; then - cmd="$cmd" - else - cmd="$cmd -d" - fi - eval $cmd - elif [[ $service_alias_head == "@" ]]; then - local set="${service_alias//@/}" - eval "$exec $path $command $set" - else - local service_path="$path/$service_alias" - local cmd="$exec $service_path $command" - - if [[ $command != "up" ]]; then - cmd="$cmd" - else - cmd="$cmd -d" - fi - eval $cmd - fi - done - else - # -------------------------------------------------------------- - # The main unit where commands are generated for docker compose - # -------------------------------------------------------------- - if [[ -z $config_env ]]; then - local config_env="$self.env" - fi - - env_handle "$initial_path" "$config_env" "$path" - - if [[ -f "$initial_path/.env" ]]; then - local cmd_env="--env-file $initial_path/.env" - else - local cmd_env="" - fi - - if docker compose version | grep "Docker Compose version" &>/dev/null; then - local docker_compose="docker compose" - elif docker-compose version | grep "Docker Compose version" &>/dev/null; then - local docker_compose="docker-compose" - fi - - local cmd="$docker_compose $cmd_env -f $config_path $command $flags" - - if [[ $debug == false ]]; then - if [[ $verbose == true ]]; then - echo $cmd - fi - echo "$( - cd $initial_path - $cmd - )" - else - echo $cmd - fi - - env_handle -c "$initial_path" - fi - return 1 - ;; - # -------------------------------------------------------------- - # Entry point wrapper - # Triggered when stitchocker -a {alias} {command} - # -------------------------------------------------------------- - "-a") - if [[ -z $2 ]]; then - error "Path alias not specified" - fi - - if [[ -z $3 ]]; then - error "Command not specified" - fi - - local path=$(env $2) - local initial_path=$path - eval "$exec $path ${@:3}" - ;; - # -------------------------------------------------------------- - # Default entry point wrapper - # Triggered when stitchocker {command} - # -------------------------------------------------------------- - *) - local path=$(pwd) - local initial_path=$path - eval "$exec $path $command $@" - ;; - esac + local self="stitchocker" + local version="1.2.2" + local version_info="Stitchocker version $version" + local help=" + Usage: + $self [--verbose|--debug] [-a ] [docker-compose COMMAND] [SETS...] + $self -h|--help + $self -v|--version + + Options: + -h|--help Shows this help text + -v|--version Shows $self version + --update Updates $self to the latest stable version + --debug Runs all commands in debug mode + --verbose Runs all commands in verbose mode + -p Path to stitching directory + -a Alias to stitching directory + + Examples: + $self up + $self up default backend frontend + $self -a my-projects-alias-from-env up default backend frontend + $self --debug -a my-projects-alias-from-env up default backend frontend + $self --verbose -a my-projects-alias-from-env up default backend frontend + " + + local debug_env=""$self"_debug" + local debug=$(env $debug_env) + if [[ ! -z $debug && $debug == true ]]; then + debug=true + else + debug=false + fi + + local verbose_env=""$self"_verbose" + local verbose=$(env $verbose_env) + if [[ ! -z $verbose && $verbose == true ]]; then + verbose=true + else + verbose=false + fi + + local path_flag="-p" + local exec="$self $path_flag" + + if [ $# -lt 1 ]; then + info "$help" + exit 1 + fi + + # Entrypoint + case $1 in + # -------------------------------------------------------------- + # Help info + # -------------------------------------------------------------- + "-h" | "--help") + info --exit "$help" + ;; + # -------------------------------------------------------------- + # Version info + # -------------------------------------------------------------- + "-v" | "--version") + info --exit "$version_info" + ;; + # -------------------------------------------------------------- + # Updates to the latest stable version + # -------------------------------------------------------------- + "--update") + sudo bash -c "$(curl -H 'Cache-Control: no-cache' -fsSL https://raw.githubusercontent.com/alexaandrov/stitchocker/master/install.sh)" + exit 0 + ;; + # -------------------------------------------------------------- + # Runs all commands in debug mode + # -------------------------------------------------------------- + "--debug") + local debug_export="export $(echo $debug_env | awk '{print toupper($0)}')" + eval "$debug_export=true" + $self ${@:2} + eval "$debug_export=false" + ;; + # -------------------------------------------------------------- + # Runs all commands in verbose mode + # -------------------------------------------------------------- + "--verbose") + local verbose_export="export $(echo $verbose_env | awk '{print toupper($0)}')" + eval "$verbose_export=true" + $self ${@:2} + eval "$verbose_export=false" + ;; + # -------------------------------------------------------------- + # The entry point for all commands + # -------------------------------------------------------------- + $path_flag) + # Function arguments + local path="$2" + local command="$3" + local first_flag="$4" + local second_flag="$5" + local flags="${@:4}" + local config_env="$self.env" + + if [[ -z $path ]]; then + error "Path not specified" + fi + + if [[ -z $command ]]; then + error "Command not specified" + fi + + if [[ $command == "down" ]]; then + $self -p $path stop $flags + fi + + if [[ $command == "reload" ]]; then + $self -p $path down $flags + $self -p $path up $flags + exit 1 + fi + + local available_config_names=("$self.yml" "$self.yaml" "docker-compose.yaml" "docker-compose.yml") + + local custom_config_env=""$self"_config" + local custom_config_name=$(env $custom_config_env) + if [[ ! -z $custom_config_name && $custom_config_name != "null" ]]; then + available_config_names+=($custom_config_name) + fi + + local config_path="" + for config_name in ${available_config_names[@]}; do + if [[ -f "$path/$config_name" ]]; then + config_path="$path/$config_name" + fi + done + + if [[ ! -f $config_path ]]; then + error --no-exit "No config found for: $path" + info "Available config names: ${available_config_names[@]}" + exit 1 + fi + + local default_set=$(env stitchocker_default_set) + if [[ ! -z $default_set && $default_set != "null" ]]; then + default_set="$default_set" + else + default_set="default" + fi + + local sets_field="${self}_config_sets" + + create_yaml_variables $config_path "${self}_config_" + + local sets_data="$(eval echo \$${sets_field}_${default_set})" + + if [[ ! -z $sets_data ]]; then + if [[ ! -z $first_flag ]]; then + if [[ ! -z $second_flag ]]; then + for set in $flags; do + eval "$exec $path $command $set" + done + exit 1 + fi + local set="$first_flag" + else + local set="$default_set" + fi + + local services="$(eval echo \${${sets_field}_${set}[*]})" + + if [[ -z $services ]]; then + error "Your config doesn't have \"$set\" value" + fi + + info "$(echo "$command" | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}') $set set:" + + for service_alias in ${services}; do + local service_alias_head="$(echo $service_alias | head -c 1)" + if [[ $service_alias == *"/"* ]]; then + if [[ $service_alias_head == "@" ]]; then + local service_alias="${service_alias//@/}" + local service_path="$(env $service_alias)" + elif [[ $service_alias_head == "/" || $service_alias_head == "~" ]]; then + local service_path="$service_alias" + else + local service_path="$path/$service_alias" + fi + + local cmd="$exec $service_path $command" + if [[ $command != "up" ]]; then + cmd="$cmd" + else + cmd="$cmd -d" + fi + eval $cmd + elif [[ $service_alias_head == "@" ]]; then + local set="${service_alias//@/}" + eval "$exec $path $command $set" + else + local service_path="$path/$service_alias" + local cmd="$exec $service_path $command" + + if [[ $command != "up" ]]; then + cmd="$cmd" + else + cmd="$cmd -d" + fi + eval $cmd + fi + done + else + # -------------------------------------------------------------- + # The main unit where commands are generated for docker compose + # -------------------------------------------------------------- + if [[ -z $config_env ]]; then + local config_env="$self.env" + fi + + env_handle "$initial_path" "$config_env" "$path" + + if [[ -f "$initial_path/.env" ]]; then + local cmd_env="--env-file $initial_path/.env" + else + local cmd_env="" + fi + + if docker compose version | grep "Docker Compose version" &>/dev/null; then + local docker_compose="docker compose" + elif docker-compose version | grep "Docker Compose version" &>/dev/null; then + local docker_compose="docker-compose" + fi + + local cmd="$docker_compose $cmd_env -f $config_path $command $flags" + + if [[ $debug == false ]]; then + if [[ $verbose == true ]]; then + echo $cmd + fi + echo "$( + cd $initial_path + $cmd + )" + else + echo $cmd + fi + + env_handle -c "$initial_path" + fi + return 1 + ;; + # -------------------------------------------------------------- + # Entry point wrapper + # Triggered when stitchocker -a {alias} {command} + # -------------------------------------------------------------- + "-a") + if [[ -z $2 ]]; then + error "Path alias not specified" + fi + + if [[ -z $3 ]]; then + error "Command not specified" + fi + + local path=$(env $2) + local initial_path=$path + eval "$exec $path ${@:3}" + ;; + # -------------------------------------------------------------- + # Default entry point wrapper + # Triggered when stitchocker {command} + # -------------------------------------------------------------- + *) + local path=$(pwd) + local initial_path=$path + eval "$exec $path $command $@" + ;; + esac } # ----------------------------------------------- @@ -294,57 +294,57 @@ stitchocker() { # Returns absolute path to user env ## env() { - local env_alias=$(echo $1 | cut -d "/" -f 1) - local env_additional_path=${1//$env_alias\//} - local env=$(echo $env_alias | awk '{print toupper($0)}') + local env_alias=$(echo $1 | cut -d "/" -f 1) + local env_additional_path=${1//$env_alias\//} + local env=$(echo $env_alias | awk '{print toupper($0)}') - local env_path="$(eval "echo \"\$$env\"")" + local env_path="$(eval "echo \"\$$env\"")" - if [[ ! -z "$env_additional_path" && "$env_alias" != "$env_additional_path" ]]; then - env_path="$env_path/$env_additional_path" - fi + if [[ ! -z "$env_additional_path" && "$env_alias" != "$env_additional_path" ]]; then + env_path="$env_path/$env_additional_path" + fi - if [[ $env_path == *"himBHs"* || -z $env_path ]]; then - echo "null" - fi + if [[ $env_path == *"himBHs"* || -z $env_path ]]; then + echo "null" + fi - echo $env_path + echo $env_path } ## # Adds an environment from config to each service ## env_handle() { - local env_name=".env" - if [[ $1 != "-c" ]]; then - local path="$1" - local config_env="$2" - local env_path="$path/$env_name" - - if [[ ! -z $path && ! -z $config_env ]]; then - local config_env_path="$path/$config_env" - local service_env_name=".env" - local service_env_path="$service_path/$service_env_name" - - # If stitchocker env file exist use it - if [[ -f $config_env_path ]]; then - cp $config_env_path $env_path - fi - - # If the service has its own env file, it will expand the existing env file - if [[ -f $service_env_path ]]; then - echo >>$env_path - cat $service_env_path >>$env_path - fi - fi - else - # Remove temporary env file - local path="$2" - local env_path="$path/$env_name" - if [[ -f $env_path ]]; then - rm $env_path - fi - fi + local env_name=".env" + if [[ $1 != "-c" ]]; then + local path="$1" + local config_env="$2" + local env_path="$path/$env_name" + + if [[ ! -z $path && ! -z $config_env ]]; then + local config_env_path="$path/$config_env" + local service_env_name=".env" + local service_env_path="$service_path/$service_env_name" + + # If stitchocker env file exist use it + if [[ -f $config_env_path ]]; then + cp $config_env_path $env_path + fi + + # If the service has its own env file, it will expand the existing env file + if [[ -f $service_env_path ]]; then + echo >>$env_path + cat $service_env_path >>$env_path + fi + fi + else + # Remove temporary env file + local path="$2" + local env_path="$path/$env_name" + if [[ -f $env_path ]]; then + rm $env_path + fi + fi } # -- @@ -352,29 +352,29 @@ env_handle() { # -- info() { - local green=$(tput setaf 2) - local reset=$(tput sgr0) - + local green=$(tput setaf 2) + local reset=$(tput sgr0) + if [[ $1 != "--exit" ]]; then echo -e "${green}$@${reset}" echo else echo -e "${green}${@:2}${reset}" - exit 0 - fi + exit 0 + fi } error() { - local red=$(tput setaf 1) - local reset=$(tput sgr0) - - if [[ $1 != "--no-exit" ]]; then - echo >&2 -e "${red}$@${reset}" - exit 1 - else - echo >&2 -e "${red}${@:2}${reset}" + local red=$(tput setaf 1) + local reset=$(tput sgr0) + + if [[ $1 != "--no-exit" ]]; then + echo >&2 -e "${red}$@${reset}" + exit 1 + else + echo >&2 -e "${red}${@:2}${reset}" echo - fi + fi } # -- @@ -427,23 +427,23 @@ parse_yaml() { } create_yaml_variables() { - local yaml_file="$1" - local prefix="$2" + local yaml_file="$1" + local prefix="$2" - if [[ ! -z $prefix ]]; then - unset_yaml_variables $prefix - fi + if [[ ! -z $prefix ]]; then + unset_yaml_variables $prefix + fi - eval "$(parse_yaml "$yaml_file" "$prefix")" + eval "$(parse_yaml "$yaml_file" "$prefix")" } unset_yaml_variables() { - local prefix="$1" - local variables=$( set -o posix; set | cut -f1 -d"=" | grep $prefix ) + local prefix="$1" + local variables=$( set -o posix; set | cut -f1 -d"=" | grep $prefix ) - for variable in $variables; do - unset $variable - done + for variable in $variables; do + unset $variable + done } # -----------------------------------------------