Skip to content

Commit

Permalink
improved silent-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aho-dips committed Feb 8, 2024
1 parent 8efe24d commit c2ddcc0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 41 deletions.
51 changes: 30 additions & 21 deletions smud-cli/functions-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ default_upstream="https://github.com/DIPSAS/DIPS-GitOps-Template.git"
set_upstream()
{
new_upstream="$1"
if [ ! "$new_upstream" ]; then
new_upstream="$upstream_url"
fi
if [ ! "$new_upstream" ]; then
new_upstream="$first_param"
fi

if [ "$help" ]; then
echo "${bold}smud set-upstream${normal}: Set upstream"
printf "With Only ${green}set-upstream${normal}, Upstream '$default_upstream' will be configured if not configured yet. \n"
printf "With ${green}set-upstream ${bold}<value>${normal}, Upstream '<value>' will be configured. \n"
printf "With ${green}set-upstream ${bold}--upstream-url <value>${normal}, Upstream '<value>' will be configured. \n"
printf "With ${green}set-upstream ${bold}-${normal}, Upstream will be removed. \n"
return
fi

if [ ! "$is_repo" ]; then
printf "${red}'$(pwd)' is not a git repository! ${normal}\n"
print_not_silent "${red}'$(pwd)' is not a git repository! ${normal}\n"
return
fi

Expand All @@ -25,73 +33,72 @@ set_upstream()
remove_upstream_command="git remote rm upstream"
run_command remove-upstream --command-from-var=remove_upstream_command --debug-title='Removing upstream config URL'
if [ "$new_upstream" = "-" ]; then
printf "${gray}Upstream is removed${normal}\n"
print_not_silent "${gray}Upstream is removed${normal}\n"
exit
else
add_upstream_command="git remote add upstream $new_upstream"
run_command add_upstream_command --command-from-var=add_upstream_command --debug-title='Adding upstream with user specified URL'
printf "${gray}Upstream configured with '$new_upstream' ${normal}\n"
print_not_silent "${gray}Upstream configured with '$new_upstream' ${normal}\n"
fi
elif [ ! "$new_upstream" ]; then
new_upstream=$default_upstream
add_upstream_command="git remote add upstream $new_upstream"
run_command add_upstream_command --command-from-var=add_upstream_command --debug-title='Adding upstream with default URL'
printf "${gray}Upstream configured with '$new_upstream' ${normal}\n"
print_not_silent "${gray}Upstream configured with '$new_upstream' ${normal}\n"
elif [ ! "$caller" ]; then
printf "${gray}Upstream alredy configured with '$new_upstream' ${normal}\n"
print_not_silent "${gray}Upstream alredy configured with '$new_upstream' ${normal}\n"
fi
}

set_origin()
{
if [ ! "$is_repo" ]; then
printf "${red}'$(pwd)' is not a git repository! ${normal}\n"
print_not_silent "${red}'$(pwd)' is not a git repository! ${normal}\n"
return
fi

# Check if origin exists
check_origin_command="git config --get remote.origin.url"
run_command check-origin --command-from-var=check_origin_command --return-in-var=remote_origin --debug-title='Checking if remote.origin.url exist in git config' || return
run_command check-origin --command-from-var=check_origin_command --return-in-var=remote_origin --return-var=dummy --debug-title='Checking if remote.origin.url exist in git config' || return

# If string is empty, set the remote origin url
if [ ! -n "$remote_origin" ]; then
if [ ! "$remote_origin" ]; then
printf "${yellow}Remote repository origin is not set, please enter URL for the remote origin.\nOrigin URL: ${normal}"
read user_set_remote_origin
add_origin_command="git remote add origin $user_set_remote_origin"
run_command set-origin --command-from-var=add_origin_command --debug-title='Adding remote origin' || return
run_command set-origin --command-from-var=add_origin_command --return-var=dummy --debug-title='Adding remote origin' || return
fi
}

merge_upstream()
{
merge_upstream_command="git merge upstream/main"
run_command merge-upstream --command-from-var=merge_upstream_command --debug-title='Merging upstream repository into local branch' || return
run_command merge-upstream --command-from-var=merge_upstream_command --return-var=dummy --debug-title='Merging upstream repository into local branch' || return
}

fetch_origin()
{
fetch_origin_command="git fetch origin"
run_command fetch-origin --command-from-var=fetch_origin_command --debug-title='Fetching origin' || return
run_command fetch-origin --command-from-var=fetch_origin_command --return-var=dummy --debug-title='Fetching origin' || return
}

init_repo()
{
init_command="git init"
run_command init-repo --command-from-var=init_command --debug-title='Initializing repository' || return
run_command init-repo --command-from-var=init_command --return-var=dummy --debug-title='Initializing repository' || return
is_repo="true"

branches=$(git branch)
if [ ! -n "$branches" ]; then
# "main" possibly not default branch name so create it
create_main_branch="git checkout -b main"
run_command checkout-main --command-from-var=create_main_branch --debug-title='Creating main branch' || return
run_command checkout-main --command-from-var=create_main_branch --return-var=dummy --debug-title='Creating main branch' || return
fi
}

fetch_upstream()
{
fetch_upstream_command="git fetch upstream"
run_command fetch-upstream --command-from-var=fetch_upstream_command --debug-title='Fetching upstream' || return
run_command fetch-upstream --command-from-var=fetch_upstream_command --return-var=dummy --debug-title='Fetching upstream' || return
}

# Initalizes repo, upstream and origin if not configured. Will always fetch upstream when called.
Expand All @@ -102,6 +109,7 @@ init()
echo "${bold}smud $func${normal}: Initializes local repository and sets upstream and origin remotes"
printf "With Only ${green}$func${normal}, Upstream '$default_upstream' will be configured if not configured yet. When configured the upstream will be fetched. \n"
printf "With ${green}$func ${bold}<value>${normal}, Upstream '<value>' will be configured before upstream is fetched. \n"
printf "With ${green}$func ${bold}--upstream-url <value>${normal}, Upstream '<value>' will be configured before upstream is fetched. \n"
printf "With ${green}$func ${bold}-${normal}, Upstream will be removed. \n"
return
fi
Expand All @@ -111,7 +119,6 @@ init()
upstream_url="$first_param"
fi
fi

remote_origin=$(git config --get remote.origin.url)
remote_upstream=$(git config --get remote.upstream.url)

Expand All @@ -125,10 +132,12 @@ init()
print_debug "upstream_url: $upstream_url"

if [ ! "$is_repo" ]; then
local yes_no="y"
ask yes_no $yellow "The current directory does not seem to be a git repository\nWould you like to initialize the repository and merge the remote upstream? (yes/no)"
local yes_no="yes"
if [ ! "$silent" ]; then
ask yes_no $yellow "The current directory does not seem to be a git repository\nWould you like to initialize the repository and merge the remote upstream? (yes/no)"
fi
if [ ! "$yes_no" = "yes" ]; then
printf "${yellow}Aborting"
print_not_silent "${yellow}Aborting"
exit 0
fi
init_repo
Expand All @@ -139,9 +148,9 @@ init()
fi

if [ ! -n "$remote_origin" ]; then
echo "Setting and fetching origin"
print_not_silent "Setting and fetching origin"
set_origin
fetch_origin
printf "${green}Initalization complete.\n${normal}"
print_not_silent "${green}Initalization complete.\n${normal}"
fi
}
27 changes: 14 additions & 13 deletions smud-cli/functions-list.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ list()
fi
return
fi

init

if [ ! "$is_repo" ]; then
Expand Down Expand Up @@ -135,7 +134,7 @@ product_infos__find_latest_products_with_version()
return
fi
pos_colon=0
app_files_command="git --no-pager diff $commit_filter --no-merges --pretty=format:'' -- :$filter|grep '+++ .*app.yaml\|+++ .*values.yaml\|+ chartVersion'| sed -e 's/+//g' -e 's/b\///g'|cut -d '#' -f 1 |xargs"
app_files_command="git --no-pager diff $commit_filter --no-merges --pretty=format:'' -- :$filter|grep '+++ .*app.yaml\|+++ .*values.yaml\|+ chartVersion'| sed -e 's/+//g' -e 's/b\///g'|cut -d '#' -f 1"
fi

{
Expand Down Expand Up @@ -666,15 +665,17 @@ product_infos__print()
if [ $iNew -gt 0 ];then summarize="${summarize} New versions:$iNew |"; fi
if [ "$summarize" ]; then
echo "----------------------------------------------------------------------------------------------------------"
echo "| Stage:$product_stage |$summarize"
iProductsTot=$((iProductsTot+iProducts))
iMajorTot=$((iMajorTot+iMajor))
iMinorTot=$((iMinorTot+iMinor))
iPatchTot=$((iPatchTot+iPatch))
iSameTot=$((iSameTot+iSame))
iNewTot=$((iNewTot+iNew))
iStagesTot=$((iStagesTot+1))
echo "=========================================================================================================="
if [ ! "$silent" ]; then
echo "| Stage:$product_stage |$summarize"
iProductsTot=$((iProductsTot+iProducts))
iMajorTot=$((iMajorTot+iMajor))
iMinorTot=$((iMinorTot+iMinor))
iPatchTot=$((iPatchTot+iPatch))
iSameTot=$((iSameTot+iSame))
iNewTot=$((iNewTot+iNew))
iStagesTot=$((iStagesTot+1))
echo "=========================================================================================================="
fi
fi
done
if [ ! "$print_product" ]; then
Expand All @@ -689,7 +690,7 @@ product_infos__print()
if [ $iPatchTot -gt 0 ];then summarize="${summarize} Patches:$iPatchTot |"; fi
if [ $iSameTot -gt 0 ];then summarize="${summarize} Same versions:$iSameTot |"; fi
if [ $iNewTot -gt 0 ];then summarize="${summarize} New versions:$iNewTot |"; fi
if [ "$summarize" ]; then
if [ "$summarize" ] && [ ! "$silent" ]; then
echo "| Stages:$iStagesTot |TOTAL $summarize"
echo "=========================================================================================================="
fi
Expand Down Expand Up @@ -911,7 +912,7 @@ get_latest_version()
if [ "$product_latest_commit_local" ]; then
# echo "**** [$get_latest_version_commit_file]"

latest_version_command="git --no-pager grep "chartVersion:" $product_latest_commit_local:$file|cut -d '#' -f 1 |xargs"
latest_version_command="git --no-pager grep "chartVersion:" $product_latest_commit_local:$file|cut -d '#' -f 1"
{
run_command --latest_version --command-var=latest_version_command --return-var=getlatestversion__latest_version --skip-error --debug-title='Find latest versions from conent'
# echo "getlatestversion__latest_version(0): '$getlatestversion__latest_version'"
Expand Down
24 changes: 17 additions & 7 deletions smud-cli/include.sh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ print_verbose()
fi
}

print_not_silent()
{
if [ ! "$silent" ] && [ "$1" ]; then
printf "$1"
fi
}


lower()
{
local -n str=$1
Expand Down Expand Up @@ -233,14 +241,16 @@ progressbar__increase()
progressbar__linmessage=", (line $progressbar__current/$progressbar__size)"
fi
if [ "$simple" ]; then
echo "$progressbar__message$progressbar__linmessage"
print_not_silent "$progressbar__message$progressbar__linmessage"
else
local bar_percentage_value=$((100 * $progressbar__current/$progressbar__size))
local bar_percentage_chars=$(($bar_percentage_value/2))
local bar_chars="$(printf %${bar_percentage_chars}s "" | tr ' ' '#')"
local bar_print="`printf %-50s $bar_chars`"
local bar_percentage_print="(`printf %-3s $bar_percentage_value`%)"
echo -ne "$bar_print $bar_percentage_print -- $progressbar__message$progressbar__linmessage \\r"
if [ ! "$silent" ]; then
echo -ne "$bar_print $bar_percentage_print -- $progressbar__message$progressbar__linmessage \\r"
fi
fi
progressbar__last_drawn=$progressbar__current
fi
Expand All @@ -255,11 +265,13 @@ progressbar__end()
fi
progressbar__last_drawn=-5
progressbar__increase $progressbar__size "$msg"
echo -ne '\n'
if [ ! "$silent" ]; then
echo -ne '\n'
fi
progressbar__enabled=""
fi

if [ "$2" ]; then
if [ "$2" ] && [ ! "$silent" ]; then
progressbar__stop_time=$(date +"%Y-%m-%d %H:%M:%S")

# Calculate and display the time difference
Expand Down Expand Up @@ -333,11 +345,9 @@ run_command()
fi
return $run_command_error_code
fi

if [ ! "$return_in_var" ];then
if [ ! "$return_in_var" ] && [ "$run_command_result" ];then
echo "$run_command_result"
fi

}
first_param="$3"
parse_arguments ARGS $@
Expand Down

0 comments on commit c2ddcc0

Please sign in to comment.