Skip to content

Commit 49e95ba

Browse files
support 'config --print-path'
Added new flag `config --print-path` (`-p`) for easy determination of the currently active configuration. The path is printed on console.
1 parent 09e5e5c commit 49e95ba

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/west/app/config.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ def do_add_parser(self, parser_adder):
100100
"action to perform (give at most one)"
101101
).add_mutually_exclusive_group()
102102

103+
group.add_argument('-p', '--print-path', action='store_true',
104+
help='print path from the active west config file')
103105
group.add_argument('-l', '--list', action='store_true',
104106
help='list all options and their values')
105107
group.add_argument('-d', '--delete', action='store_true',
@@ -135,14 +137,16 @@ def do_run(self, args, user_args):
135137
if args.list:
136138
if args.name:
137139
self.parser.error('-l cannot be combined with name argument')
138-
elif not args.name:
140+
elif not args.name and not args.print_path:
139141
self.parser.error('missing argument name '
140142
'(to list all options and values, use -l)')
141143
elif args.append:
142144
if args.value is None:
143145
self.parser.error('-a requires both name and value')
144146

145-
if args.list:
147+
if args.print_path:
148+
self.print_path(args)
149+
elif args.list:
146150
self.list(args)
147151
elif delete:
148152
self.delete(args)
@@ -153,6 +157,11 @@ def do_run(self, args, user_args):
153157
else:
154158
self.write(args)
155159

160+
def print_path(self, args):
161+
config_path = self.config.get_path(args.configfile or LOCAL)
162+
if config_path:
163+
print(config_path)
164+
156165
def list(self, args):
157166
what = args.configfile or ALL
158167
for option, value in self.config.items(configfile=what):

src/west/configuration.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ def __init__(self, topdir: PathType | None = None):
178178
self._global = _InternalCF.from_path(self._global_path)
179179
self._local = _InternalCF.from_path(self._local_path)
180180

181+
def get_path(self, configfile: ConfigFile = ConfigFile.LOCAL):
182+
if configfile == ConfigFile.ALL:
183+
raise RuntimeError(f'{configfile} not allowed for get_path')
184+
elif configfile == ConfigFile.LOCAL:
185+
return self._local_path
186+
elif configfile == ConfigFile.SYSTEM:
187+
return self._system_path
188+
elif configfile == ConfigFile.GLOBAL:
189+
return self._global_path
190+
181191
def get(self, option: str,
182192
default: str | None = None,
183193
configfile: ConfigFile = ConfigFile.ALL) -> str | None:

tests/test_config.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ def test_config_global():
8282
assert glb['pytest']['global2'] == 'foo2'
8383
assert 'pytest' not in lcl
8484

85+
def test_config_print_path():
86+
stdout = cmd('config --local --print-path')
87+
assert os.environ["WEST_CONFIG_LOCAL"] == stdout.rstrip()
88+
89+
stdout = cmd('config --global --print-path')
90+
assert os.environ["WEST_CONFIG_GLOBAL"] == stdout.rstrip()
91+
92+
stdout = cmd('config --system --print-path')
93+
assert os.environ["WEST_CONFIG_SYSTEM"] == stdout.rstrip()
94+
95+
del os.environ['WEST_CONFIG_LOCAL']
96+
stdout = cmd('config --local --print-path')
97+
assert "" == stdout.rstrip()
98+
8599
def test_config_local():
86100
# test_config_system for local variables.
87101
cmd('config --local pytest.local foo')

0 commit comments

Comments
 (0)