Skip to content

Commit

Permalink
src: kw_env: Adding verbose mode
Browse files Browse the repository at this point in the history
This commit adds support for the verbose parameter within `kw env`.  The
verbose parameter gives details of the commands that are executed behind
the scenes.

Note: This is part of the issue: #179

Reviewed-by: Rodrigo Siqueira <siqueirajordao@riseup.net>
Signed-off-by: Aquila Macedo <aquilamacedo@riseup.net>
  • Loading branch information
aquilamacedo authored and rodrigosiqueira committed Apr 30, 2023
1 parent 06e21a0 commit c0efb7f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
6 changes: 6 additions & 0 deletions documentation/man/features/env.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SYNOPSIS
| *kw* (*env*) [(-d | \--destroy <NAME>)]
| *kw* (*env*) [(-l | \--list)]
| *kw* (*env*) [(-e | \--exit-env)]
| *kw* (*env*) [\--verbose]
DESCRIPTION
===========
Expand Down Expand Up @@ -58,6 +59,11 @@ OPTIONS
and doesn't want to use it anymore, the `--exit-env` option will remove all
symbolic links and copy the current env's configuration files to the .kw.

\--verbose:
Verbose mode is an option that causes the kw program to display debug messages to track
its progress. This functionality is very useful during the debugging process, allowing
you to identify possible errors more easily.

EXAMPLES
========
If you want to create a new env, you can use::
Expand Down
1 change: 1 addition & 0 deletions src/_kw
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ _kw_drm()
_kw_env()
{
_arguments : \
'(--verbose)--verbose[enable verbose mode]' \
'(-c --create -u --use -d --destroy -l --list -e --exit-env)'{-c,--create}'[create new env]: : ' \
'(-u --use -c --create -d --destroy -l --list -e --exit-env)'{-u,--use}'[change current env to an existing env]: : ' \
'(-d --destroy -c --create -u --use -l --list -e --exit-env)'{-d,--destroy}'[delete an existing env]: : ' \
Expand Down
2 changes: 1 addition & 1 deletion src/bash_autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function _kw_autocomplete()
kw_options['drm']='--remote --local --gui-on --gui-off --load-module
--unload-module --conn-available --modes --help'

kw_options['env']='--create --list --use --exit-env'
kw_options['env']='--create --list --use --exit-env --verbose'

kw_options['self-update']='--unstable --help'
kw_options['u']="${kw_options['self-update']}"
Expand Down
73 changes: 54 additions & 19 deletions src/kw_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function env_main()
exit 22 # EINVAL
fi

[[ -n "${options_values['VERBOSE']}" ]] && flag='VERBOSE'

if [[ -n "${options_values['CREATE']}" ]]; then
create_new_env
return "$?"
Expand Down Expand Up @@ -62,6 +64,9 @@ function use_target_env()
local target_env="${options_values['USE']}"
local local_kw_configs="${PWD}/.kw"
local tmp_trash
local cmd

flag=${flag:-'SILENT'}

if [[ ! -d "${local_kw_configs}/${ENV_DIR}/${target_env}" ]]; then
return 22 # EINVAL
Expand All @@ -76,14 +81,17 @@ function use_target_env()
# Check if the config file exists before trying to remove it.
[[ ! -f "${local_kw_configs}/${config}.config" ]] && continue

mv "${local_kw_configs}/${config}.config" "$tmp_trash"
cmd="mv ${local_kw_configs}/${config}.config ${tmp_trash}"
cmd_manager "$flag" "$cmd"
fi

# Create symbolic link
ln --symbolic --force "${local_kw_configs}/${ENV_DIR}/${target_env}/${config}.config" "${local_kw_configs}/${config}.config"
cmd="ln --symbolic --force ${local_kw_configs}/${ENV_DIR}/${target_env}/${config}.config ${local_kw_configs}/${config}.config"
cmd_manager "$flag" "$cmd"
done

touch "${local_kw_configs}/${ENV_CURRENT_FILE}"
cmd="touch ${local_kw_configs}/${ENV_CURRENT_FILE}"
cmd_manager "$flag" "$cmd"
printf '%s\n' "$target_env" > "${local_kw_configs}/${ENV_CURRENT_FILE}"
}

Expand All @@ -93,6 +101,9 @@ function exit_env()
{
local current_env
local local_kw_configs="${PWD}/.kw"
local cmd

flag=${flag:-'SILENT'}

if [[ ! -f "${local_kw_configs}/${ENV_CURRENT_FILE}" ]]; then
say 'You are not using any env at the moment'
Expand All @@ -108,15 +119,17 @@ function exit_env()
# will be copied to the .kw folder. We will only need the original files of the
# current env, the symlinks will be removed as they only point to these original files.
if [[ -L "${local_kw_configs}/${config}.config" ]]; then
rm "${local_kw_configs}/${config}.config"
cmd="rm ${local_kw_configs}/${config}.config"
cmd_manager "$flag" "$cmd"
fi

# Check if the config file exists before trying to copy it.
[[ ! -f "${local_kw_configs}/${ENV_DIR}/${current_env}/${config}.config" ]] && continue

cp "${local_kw_configs}/${ENV_DIR}/${current_env}/${config}.config" "${local_kw_configs}"
cmd="cp ${local_kw_configs}/${ENV_DIR}/${current_env}/${config}.config ${local_kw_configs}"
cmd_manager "$flag" "$cmd"
done
rm "${local_kw_configs}/${ENV_CURRENT_FILE}"
cmd="rm ${local_kw_configs}/${ENV_CURRENT_FILE}"
cmd_manager "$flag" "$cmd"
success 'You left the environment feature.'
fi
}
Expand All @@ -138,6 +151,9 @@ function create_new_env()
local current_env_name
local output
local ret
local cmd

flag=${flag:-'SILENT'}

if [[ ! -d "$local_kw_configs" || ! -f "$local_kw_build_config" || ! -f "$local_kw_deploy_config" ]]; then
complain 'It looks like that you did not setup kw in this repository.'
Expand All @@ -146,7 +162,8 @@ function create_new_env()
fi

# Create envs folder
mkdir -p "${local_kw_configs}/${ENV_DIR}"
cmd="mkdir -p ${local_kw_configs}/${ENV_DIR}"
cmd_manager "$flag" "$cmd"

# Check if the env name was not created
output=$(find "${local_kw_configs}/${ENV_DIR}" -type d -name "$env_name")
Expand All @@ -157,37 +174,45 @@ function create_new_env()
fi

# Create env folder
mkdir -p "${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd="mkdir -p ${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"

# Copy local configs
for config in "${config_file_list[@]}"; do
if [[ ! -e "${local_kw_configs}/${config}.config" ]]; then
say "${config}.config does not exist. Creating a default one."
cp "${KW_ETC_DIR}/${config}.config" "${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd="cp ${KW_ETC_DIR}/${config}.config ${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"
else
cp "${local_kw_configs}/${config}.config" "${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd="cp ${local_kw_configs}/${config}.config ${local_kw_configs}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"
fi
done

# Handle build and config folder
mkdir -p "${cache_build_path}/${ENV_DIR}/${env_name}"
cmd="mkdir -p ${cache_build_path}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"

current_env_name=$(get_current_env_name)
ret="$?"
# If we already have an env, we should copy the config file from it.
if [[ "$ret" == 0 ]]; then
cp "${cache_build_path}/${ENV_DIR}/${current_env_name}/.config" "${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd="cp ${cache_build_path}/${ENV_DIR}/${current_env_name}/.config ${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd_manager "$flag" "$cmd"
return
elif [[ -f "${PWD}/.config" ]]; then
cp "${PWD}/.config" "${cache_build_path}/${ENV_DIR}/${env_name}"
cmd="cp ${PWD}/.config ${cache_build_path}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"
return
fi

warning "You don't have a config file, get it from default paths"
if [[ -e /proc/config.gz ]]; then
zcat /proc/config.gz > "${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd="zcat /proc/config.gz > ${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd_manager "$flag" "$cmd"
elif [[ -e "/boot/config-$(uname -r)" ]]; then
cp "/boot/config-$(uname -r)" "${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd="cp /boot/config-$(uname -r) ${cache_build_path}/${ENV_DIR}/${env_name}/.config"
cmd_manager "$flag" "$cmd"
else
warning 'kw was not able to find any valid config file for the new env'
return 22 # EINVAL
Expand All @@ -201,6 +226,9 @@ function destroy_env()
local cache_build_path="$KW_CACHE_DIR"
local current_env
local env_name=${options_values['DESTROY']}
local cmd

flag=${flag:-'SILENT'}

if [[ ! -d "$local_kw_configs" ]]; then
complain 'It looks like that you did not setup kw in this repository.'
Expand All @@ -224,7 +252,8 @@ function destroy_env()
fi
fi

rm -rf "${local_kw_configs:?}/${ENV_DIR}/${env_name}" && rm -rf "${cache_build_path:?}/${ENV_DIR}/${env_name}"
cmd="rm -rf ${local_kw_configs:?}/${ENV_DIR}/${env_name} && rm -rf ${cache_build_path:?}/${ENV_DIR}/${env_name}"
cmd_manager "$flag" "$cmd"
success "The \"${env_name}\" environment has been destroyed."
}

Expand Down Expand Up @@ -266,7 +295,7 @@ function list_env_available_envs()

function parse_env_options()
{
local long_options='help,list,create:,use:,exit-env,destroy:'
local long_options='help,list,create:,use:,exit-env,destroy:,verbose'
local short_options='h,l,c:,u:,e,d:'
local count

Expand All @@ -284,6 +313,7 @@ function parse_env_options()
options_values['USE']=''
options_values['EXIT_ENV']=''
options_values['DESTROY']=''
options_values['VERBOSE']=''

while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand Down Expand Up @@ -322,6 +352,10 @@ function parse_env_options()
options_values['DESTROY']="$2"
shift 2
;;
--verbose)
options_values['VERBOSE']=1
shift
;;
--)
shift
;;
Expand All @@ -345,5 +379,6 @@ function env_help()
' env [-u | --use] <NAME> - Use some specific env' \
' env (-c | --create) - Create a new environment' \
' env (-e | --exit-env) - Exit environment mode' \
' env (-d | --destroy) - Destroy an environment'
' env (-d | --destroy) - Destroy an environment' \
' env (--verbose) - Show a detailed output'
}
4 changes: 4 additions & 0 deletions tests/kw_env_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ function test_parse_env_options()
output=$(parse_env_options -h)
assertEquals "($LINENO)" "$?" 0

# Check verbose
output=$(parse_env_options --verbose)
assertEquals "($LINENO)" "$?" 0

# Check create
parse_env_options --create abc
assert_equals_helper 'Create envs' \
Expand Down

0 comments on commit c0efb7f

Please sign in to comment.