Skip to content

Commit

Permalink
src: config: Add verbose mode
Browse files Browse the repository at this point in the history
Add support for the verbose parameter within `kw config`.
The verbose parameter gives details of the commands that are executed
behind the scenes.

Task of issue kworkflow#179

Signed-off-by: Fernando Henrique <fernandohjunqueira@proton.me>
Signed-off-by: André Gustavo Nakagomi Lopez <andregnl@usp.br>
  • Loading branch information
Andregnl committed Sep 22, 2023
1 parent a653137 commit 08a8532
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
11 changes: 9 additions & 2 deletions documentation/man/features/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ kw-config
SYNOPSIS
========
| *kw* (*g* | *config*)
| *kw* (*g* | *config*) [(-g | \--global)] <config.option value>
| *kw* (*g* | *config*) [(-l | \--local)] <config.option value>
| *kw* (*g* | *config*) [\--verbose] [(-g | \--global)] <config.option value>
| *kw* (*g* | *config*) [\--verbose] [(-l | \--local)] <config.option value>
| *kw* (*g* | *config*) (-s | \--show) [<config_target>]...

Expand Down Expand Up @@ -38,6 +38,9 @@ OPTIONS
-s, \--show:
Display current configurations

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

EXAMPLES
========
Suppose that you want to enable llvm compilation for your local kernel; you can
Expand All @@ -57,3 +60,7 @@ If you want to display all configurations you could use::
If you want to display deploy configurations you could use::

kw config -s deploy

If you want to change a configuration with verbose mode you could use::

kw config --verbose kworkflow.alert vs
1 change: 1 addition & 0 deletions src/_kw
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ _kw_config()
fi

_arguments : \
'(--verbose)--verbose[enable verbose mode]' \
'(-g --global -l --local -s --show)'{-g,--global}'[set global configuration]' \
'(-l --local -g --global -s --show)'{-l,--local}'[set local configuration]' \
'(-s --show -g --global -l --local)'{-s,--show}'[display current configurations]: :(vm kworkflow notification build deploy mail)' \
Expand Down
2 changes: 1 addition & 1 deletion src/bash_autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function _kw_autocomplete()
--remove --fetch --output --optimize --remote'
kw_options['k']="${kw_options['kernel-config-manager']}"

kw_options['config']='--local --global --show --help'
kw_options['config']='--local --global --show --help --verbose'

kw_options['remote']='add remove rename --list --global --set-default --verbose'

Expand Down
27 changes: 19 additions & 8 deletions src/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare -gA config_file_list=(

function config_main()
{
local flag
local parameters
local target_config_file
local option_and_value
Expand All @@ -42,6 +43,9 @@ function config_main()
return 22 # EINVAL
fi

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

parameters="${options_values['PARAMETERS']}"
is_show_configurations="${options_values['IS_SHOW_CONFIGURATIONS']}"

Expand Down Expand Up @@ -100,7 +104,7 @@ function config_main()
return 22 # EINVAL
fi

set_config_value "$option" "$value" "$base_path"
set_config_value "$flag" "$option" "$value" "$base_path"
}

function validate_option_parameter()
Expand Down Expand Up @@ -184,23 +188,24 @@ function check_if_target_config_exist()
# In case of success return 0, otherwise, return 22.
function set_config_value()
{
local option="$1"
local value="$2"
local path="$3"
local flag="$1"
local option="$2"
local value="$3"
local path="$4"

path=${path:-"${PWD}/${KW_DIR}/${name}"}

# The 's' option is usually followed by /, however, this convention will not
# work well if we deal with paths. Here we had to break the pattern a little
# bit and use < instead of / after the s option to ensure that we accept
# paths in the config option.$
sed -i -r "s<($option=).*<\1$value<" "$path"
sed -i -r "s<#\s*$option<$option<" "$path"
cmd_manager "$flag" "sed -i -r \"s<(${option}=).*<\1${value}<\" \"${path}\""
cmd_manager "$flag" "sed -i -r \"s<\#\s*${option}<${option}<\" \"${path}\""
}

function parse_config_options()
{
local long_options='help,global,local,show'
local long_options='help,global,local,show,verbose'
local short_options='h,g,l,s'

options="$(kw_parse "$short_options" "$long_options" "$@")"
Expand All @@ -215,6 +220,7 @@ function parse_config_options()
options_values['SCOPE']='local'
options_values['PARAMETERS']=''
options_values['IS_SHOW_CONFIGURATIONS']=''
options_values['VERBOSE']=''

# 'kw config' should list all configurations
if [[ "$#" == 0 ]]; then
Expand All @@ -241,6 +247,10 @@ function parse_config_options()
options_values['IS_SHOW_CONFIGURATIONS']=1
shift
;;
--verbose)
options_values['VERBOSE']=1
shift
;;
--)
shift
;;
Expand Down Expand Up @@ -321,5 +331,6 @@ function config_help()
' config - Show config values' \
' config (-g | --global) <config.option value> - Change global config' \
' config (-l | --local) <config.option value> - Change local config' \
' config (-s | --show) [config]... - Show all or specific current configurations'
' config (-s | --show) [config]... - Show all or specific current configurations' \
' config (--verbose) - Show a detailed output'
}
6 changes: 6 additions & 0 deletions tests/config_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ function test_parse_config_options()

parse_config_options --invalid
assertEquals "($LINENO)" 22 "$?"

unset options_values
declare -gA options_values

parse_config_options --verbose
assertEquals "($LINENO):" '1' "${options_values['VERBOSE']}"
}

function test_show_configurations_without_parameters()
Expand Down

0 comments on commit 08a8532

Please sign in to comment.