Skip to content

Commit

Permalink
app: config: Add support for appending to the config string
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
carlescufi committed Nov 14, 2024
1 parent d6b34df commit c97ebcc
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/west/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
To set a value for <name>, type:
west config <name> <value>
To append to a value for <name>, type:
west config -a <name> <value>
To list all options and their values:
west config -l
Expand Down Expand Up @@ -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)')
Expand All @@ -135,18 +140,27 @@ 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)
elif delete:
self.delete(args)
elif args.value is None:
self.read(args)
elif args.append:
self.append(args)
else:
self.write(args)

Expand Down Expand Up @@ -191,6 +205,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)
what = args.configfile or LOCAL
value = self.config.get(args.name, configfile=what)
if value is None:
self.die(f'option {args.name} not found in {what.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
Expand Down

0 comments on commit c97ebcc

Please sign in to comment.