From e46a0d42d0c14c2af031435a4955bcf203eb64c2 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 | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/west/app/config.py b/src/west/app/config.py index cfb5236b..47915f36 100644 --- a/src/west/app/config.py +++ b/src/west/app/config.py @@ -48,6 +48,9 @@ To set a value for , type: west config +To append to a value for , type: + west config -a + To list all options and their values: west config -l @@ -111,6 +114,8 @@ def do_add_parser(self, parser_adder): help='delete an option in one config file') parser.add_argument('-D', '--delete-all', action='store_true', help="delete an option everywhere it's set") + parser.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)') @@ -135,11 +140,18 @@ def do_run(self, args, user_args): self.parser.error('-l cannot be combined with name argument') elif delete: self.parser.error('-l cannot be combined with -d or -D') + elif args.append: + self.parser.error('-l cannot be combined with -a') elif not args.name: self.parser.error('missing argument name ' '(to list all options and values, use -l)') - elif args.delete and args.delete_all: - self.parser.error('-d cannot be combined with -D') + elif args.delete: + if args.delete_all: + self.parser.error('-d cannot be combined with -D') + elif args.append: + self.parser.error('-d cannot be combined with -a') + elif args.value is None and args.append: + self.parser.error('-a requires a value') if args.list: self.list(args) @@ -194,8 +206,14 @@ def read(self, args): def write(self, args): self.check_config(args.name) what = args.configfile or LOCAL + if args.append: + value = self.config.get(args.name, + configfile=args.configfile or ALL) or '' + value += args.value + else: + value = args.value try: - self.config.set(args.name, args.value, configfile=what) + self.config.set(args.name, value, configfile=what) except PermissionError as pe: self._perm_error(pe, what, args.name)