Skip to content

Commit 69ac4f1

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 69ac4f1

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/west/app/config.py

Lines changed: 31 additions & 8 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,18 +140,27 @@ 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)
146158
elif delete:
147159
self.delete(args)
148160
elif args.value is None:
149161
self.read(args)
162+
elif args.append:
163+
self.append(args)
150164
else:
151165
self.write(args)
152166

@@ -182,14 +196,23 @@ def check_config(self, option):
182196
self.die(f'invalid configuration option "{option}"; '
183197
'expected "section.key" format')
184198

199+
def _read(self, name, configfile):
200+
value = self.config.get(name, configfile=configfile)
201+
if value is None:
202+
self.dbg(f'{name} is unset')
203+
raise CommandError(returncode=1)
204+
return value
205+
185206
def read(self, args):
186207
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)
208+
value = self._read(args.name, args.configfile or ALL)
209+
self.inf(value)
210+
211+
def append(self, args):
212+
self.check_config(args.name)
213+
value = self._read(args.name, args.configfile or LOCAL) or ''
214+
args.value = value + args.value
215+
self.write(args)
193216

194217
def write(self, args):
195218
self.check_config(args.name)

0 commit comments

Comments
 (0)