Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conan config show <conf> command #13354

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conan/api/subapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ def install(self, path_or_url, verify_ssl, config_type=None, args=None,
def get(self, name, default=None, check_type=None):
app = ConanApp(self.conan_api.cache_folder)
return app.cache.new_config.get(name, default=default, check_type=check_type)

def show(self, pattern):
app = ConanApp(self.conan_api.cache_folder)
return app.cache.new_config.show(pattern)
11 changes: 11 additions & 0 deletions conan/cli/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ def config_list(conan_api, parser, subparser, *args):
"""
parser.parse_args(*args)
return BUILT_IN_CONFS


@conan_subcommand(formatters={"text": list_text_formatter, "json": default_json_formatter})
def config_show(conan_api, parser, subparser, *args):
"""
Get the value of the specified conf
"""
subparser.add_argument('pattern', help='Conf item(s) pattern for which to query their value')
args = parser.parse_args(*args)

return conan_api.config.show(args.pattern)
26 changes: 25 additions & 1 deletion conans/model/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import os
import fnmatch

from collections import OrderedDict

Expand Down Expand Up @@ -306,6 +307,11 @@ def pop(self, conf_name, default=None):
self._values.pop(conf_name, None)
return value

def show(self, fnpattern, pattern=""):
return {key: self.get(key)
for key in self._values.keys()
if fnmatch.fnmatch(pattern + key, fnpattern)}

def copy(self):
c = Conf()
c._values = self._values.copy()
Expand Down Expand Up @@ -475,12 +481,30 @@ def __bool__(self):

def get(self, conf_name, default=None, check_type=None):
"""
Get the value of the conf name requested and convert it to the [type]-like passed.
Get the value of the conf name requested and convert it to the [type]-like passed.
"""
pattern, name = self._split_pattern_name(conf_name)
return self._pattern_confs.get(pattern, Conf()).get(name, default=default,
check_type=check_type)

def show(self, fnpattern):
"""
Get the value of the confs that match the requested pattern
"""
result = {}

for patter_key, patter_conf in self._pattern_confs.items():
if patter_key is None:
patter_key = ""
else:
patter_key += ":"

pattern_values = patter_conf.show(fnpattern, patter_key)
result.update({patter_key + pattern_subkey: pattern_subvalue
for pattern_subkey, pattern_subvalue in pattern_values.items()})

return result

def pop(self, conf_name, default=None):
"""
Remove the conf name passed.
Expand Down
43 changes: 43 additions & 0 deletions conans/test/integration/command/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,46 @@ def _assert_config_not_exists(path):
_assert_config_exists("foo")

os.listdir(tc.current_folder)


def test_config_show():
globalconf = textwrap.dedent("""
tools.build:jobs=42
AbrilRBS marked this conversation as resolved.
Show resolved Hide resolved
tools.files.download:retry_wait=10
tools.files.download:retry=7
core.net.http:timeout=30
core.net.http:max_retries=5
zlib/*:user.mycategory:retry=True
zlib/*:user.mycategory:foo=0
zlib/*:user.myothercategory:foo=0
""")
tc = TestClient()
tc.save_home({"global.conf": globalconf})
tc.run("config show tools.build:jobs")
assert "42" in tc.out

tc.run("config show core*")
assert "core.net.http:timeout" in tc.out
assert "30" in tc.out
assert "core.net.http:max_retries" in tc.out
assert "5" in tc.out

tc.run("config show *retr*")
assert "tools.files.download:retry_wait" in tc.out
assert "tools.files.download:retry" in tc.out
assert "core.net.http:max_retries" in tc.out
assert "zlib/*:user.mycategory:retry" in tc.out

tc.run("config show zlib*")
assert "zlib/*:user.mycategory:retry" in tc.out
assert "zlib/*:user.mycategory:foo" in tc.out
assert "zlib/*:user.myothercategory:foo" in tc.out

tc.run("config show zlib/*")
assert "zlib/*:user.mycategory:retry" in tc.out
assert "zlib/*:user.mycategory:foo" in tc.out
assert "zlib/*:user.myothercategory:foo" in tc.out

tc.run("config show zlib/*:foo")
assert "zlib/*:user.mycategory:foo" in tc.out
assert "zlib/*:user.myothercategory:foo" in tc.out