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 format json option to conan cache path #15613

Merged
merged 3 commits into from
Feb 7, 2024
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
8 changes: 7 additions & 1 deletion conan/cli/commands/cache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

from conan.api.conan_api import ConanAPI
from conan.api.model import ListPattern, MultiPackagesList
from conan.api.output import cli_out_write, ConanOutput
Expand All @@ -9,6 +11,10 @@
from conans.model.recipe_ref import RecipeReference


def json_export(data):
cli_out_write(json.dumps({"cache_path": data}))


@conan_command(group="Consumer")
def cache(conan_api: ConanAPI, parser, *args):
"""
Expand All @@ -17,7 +23,7 @@ def cache(conan_api: ConanAPI, parser, *args):
pass


@conan_subcommand(formatters={"text": cli_out_write})
@conan_subcommand(formatters={"text": cli_out_write, "json": json_export})
def cache_path(conan_api: ConanAPI, parser, subparser, *args):
"""
Show the path to the Conan cache for a given reference.
Expand Down
13 changes: 13 additions & 0 deletions conans/test/integration/command_v2/test_cache_path.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import json
import os

import pytest

from conans.test.assets.genconanfile import GenConanfile
Expand Down Expand Up @@ -111,3 +114,13 @@ def test_cache_path_does_not_exist_folder():
client.run(f"install --requires mypkg/0.1")
client.run(f"cache path {pref} --folder build", assert_error=True)
assert f"ERROR: 'build' folder does not exist for the reference {pref}" in client.out

def test_cache_path_output_json():
client = TestClient()
conanfile = GenConanfile("mypkg", "0.1")
client.save({"conanfile.py": conanfile})
client.run("export .")
layout = client.exported_layout()
client.run("cache path mypkg/0.1 --format=json")
output = json.loads(client.stdout)
assert output == {"cache_path": os.path.join(layout.base_folder, "e")}