Skip to content

Commit 7a7c2e3

Browse files
committed
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 <[email protected]>
1 parent d6b34df commit 7a7c2e3

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/west/app/config.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
To set a value for <name>, type:
4949
west config <name> <value>
5050
51+
To append to a value for <name>, type:
52+
west config -a <name> <value>
53+
5154
To list all options and their values:
5255
west config -l
5356
@@ -111,6 +114,8 @@ def do_add_parser(self, parser_adder):
111114
help='delete an option in one config file')
112115
parser.add_argument('-D', '--delete-all', action='store_true',
113116
help="delete an option everywhere it's set")
117+
parser.add_argument('-a', '--append', action='store_true',
118+
help='append to an existing value')
114119

115120
group = parser.add_argument_group(
116121
'configuration file to use (give at most one)')
@@ -135,11 +140,18 @@ def do_run(self, args, user_args):
135140
self.parser.error('-l cannot be combined with name argument')
136141
elif delete:
137142
self.parser.error('-l cannot be combined with -d or -D')
143+
elif args.append:
144+
self.parser.error('-l cannot be combined with -a')
138145
elif not args.name:
139146
self.parser.error('missing argument name '
140147
'(to list all options and values, use -l)')
141-
elif args.delete and args.delete_all:
142-
self.parser.error('-d cannot be combined with -D')
148+
elif args.delete:
149+
if args.delete_all:
150+
self.parser.error('-d cannot be combined with -D')
151+
elif args.append:
152+
self.parser.error('-d cannot be combined with -a')
153+
elif args.value is None and args.append:
154+
self.parser.error('-a requires a value')
143155

144156
if args.list:
145157
self.list(args)
@@ -182,20 +194,27 @@ def check_config(self, option):
182194
self.die(f'invalid configuration option "{option}"; '
183195
'expected "section.key" format')
184196

197+
def _read(self, name, configfile):
198+
value = self.config.get(name, configfile=configfile)
199+
if value is None:
200+
self.dbg(f'{name} is unset')
201+
raise CommandError(returncode=1)
202+
185203
def read(self, args):
186204
self.check_config(args.name)
187-
value = self.config.get(args.name, configfile=args.configfile or ALL)
188-
if value is not None:
189-
self.inf(value)
190-
else:
191-
self.dbg(f'{args.name} is unset')
192-
raise CommandError(returncode=1)
205+
value = self._read(args.name, args.configfile or ALL)
206+
self.inf(value)
193207

194208
def write(self, args):
195209
self.check_config(args.name)
196210
what = args.configfile or LOCAL
211+
if args.append:
212+
value = self._read(args.name, args.configfile or ALL) or ''
213+
value += args.value
214+
else:
215+
value = args.value
197216
try:
198-
self.config.set(args.name, args.value, configfile=what)
217+
self.config.set(args.name, value, configfile=what)
199218
except PermissionError as pe:
200219
self._perm_error(pe, what, args.name)
201220

0 commit comments

Comments
 (0)