From 4260ed1a07ed8dee2398dafb335b0126c119a0dc Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Thu, 14 Nov 2024 11:40:02 +0100 Subject: [PATCH] app: config: Add support for appending to the config string In some cases, and specifically in the manifest.group-filter and manifest.project-filter options, it is sometimes useful to be able to append to a value instead of replacing it completely. For example, assuming one wants to add to an existing group filter, without this patch the user needs to do: (assuming the group filter is currently +unstable,-optional, and the user wants to add +extras). > west config manifest.group-filter > west config manifest.group-filter +unstable,-optional,+extras With this patch instead: > west config -a manifest.group-filter ,+extras Signed-off-by: Carles Cufi --- src/west/app/config.py | 34 +++++++++++++++++++++++++++++----- tests/test_config.py | 3 +++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/west/app/config.py b/src/west/app/config.py index 9255e402..a058ae12 100644 --- a/src/west/app/config.py +++ b/src/west/app/config.py @@ -48,6 +48,15 @@ To set a value for , type: west config +To append to a value for , type: + west config -a +A value must exist in the selected configuration file in order to be able +to append to it. The existing value can be empty. +Examples: + west config -a build.cmake-args -- "-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \ + -DCMAKE_VERBOSE_MAKEFILE=ON" + west config -a manifest.group-filter ,+optional + To list all options and their values: west config -l @@ -64,7 +73,7 @@ CONFIG_EPILOG = '''\ If the configuration file to use is not set, reads use all three in -precedence order, and writes use the local file.''' +precedence order, and writes (including appends) use the local file.''' ALL = ConfigFile.ALL SYSTEM = ConfigFile.SYSTEM @@ -92,13 +101,14 @@ def do_add_parser(self, parser_adder): "action to perform (give at most one)" ).add_mutually_exclusive_group() - group.add_argument('-l', '--list', action='store_true', - help='list all options and their values') + help='list all options and their values') group.add_argument('-d', '--delete', action='store_true', - help='delete an option in one config file') + help='delete an option in one config file') group.add_argument('-D', '--delete-all', action='store_true', - help="delete an option everywhere it's set") + help="delete an option everywhere it's set") + group.add_argument('-a', '--append', action='store_true', + help='append to an existing value') group = parser.add_argument_group( "configuration file to use (give at most one)" @@ -129,6 +139,8 @@ def do_run(self, args, user_args): elif not args.name: self.parser.error('missing argument name ' '(to list all options and values, use -l)') + elif args.value is None and args.append: + self.parser.error('-a requires both name and value') if args.list: self.list(args) @@ -136,6 +148,8 @@ def do_run(self, args, user_args): self.delete(args) elif args.value is None: self.read(args) + elif args.append: + self.append(args) else: self.write(args) @@ -180,6 +194,16 @@ def read(self, args): self.dbg(f'{args.name} is unset') raise CommandError(returncode=1) + def append(self, args): + self.check_config(args.name) + where = args.configfile or LOCAL + value = self.config.get(args.name, configfile=where) + if value is None: + self.die(f'option {args.name} not found in the {where.name.lower()} ' + 'configuration file') + args.value = value + args.value + self.write(args) + def write(self, args): self.check_config(args.name) what = args.configfile or LOCAL diff --git a/tests/test_config.py b/tests/test_config.py index e31d7cd4..d38657ab 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -229,6 +229,9 @@ def test_local_creation_with_topdir(): assert 'pytest' not in cfg(f=GLOBAL) assert cfg(f=LOCAL, topdir=str(topdir))['pytest']['key'] == 'val' +def test_append_basic(): + + def test_delete_basic(): # Basic deletion test: write local, verify global and system deletions # don't work, then delete local does work.