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: kworkflow#179

Signed-off-by: Aquila Macedo <aquilamacedo@riseup.net>
  • Loading branch information
aquilamacedo committed Apr 13, 2023
1 parent e8046fd commit 398b2de
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 21 deletions.
4 changes: 4 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*) [(-v | \--verbose)]
DESCRIPTION
===========
Expand Down Expand Up @@ -58,6 +59,9 @@ 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.

-v, \--verbose:
Verbose mode allows the user to see the commands executed under the hood.

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 : \
'(-v --verbose)'{-v,--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
76 changes: 56 additions & 20 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,8 +295,8 @@ function list_env_available_envs()

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

kw_parse "$short_options" "$long_options" "$@" > /dev/null
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 | -v)
options_values['VERBOSE']=1
shift
;;
--)
shift
;;
Expand All @@ -345,5 +379,7 @@ 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 (-v | --verbose) - Show a detailed output'

}

0 comments on commit 398b2de

Please sign in to comment.