Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #178 from RedHatProductSecurity/add-component-tree…
Browse files Browse the repository at this point in the history
…-view

add component registry operation to display component dependency tree
  • Loading branch information
JimFuller-RedHat authored Apr 24, 2023
2 parents e490ea0 + b9e41d9 commit 84cc31f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
35 changes: 35 additions & 0 deletions griffon/commands/entities/corgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,41 @@ def get_component_manifest(ctx, component_uuid, purl, spdx_json_format):
return cprint(data, ctx=ctx)


@components.command(name="tree")
@click.option("--uuid", "component_uuid")
@click.option("--nvr", "nvr")
@click.option("--purl", shell_complete=get_component_purls, help="Purl are URI and must be quoted.")
@click.option(
"--show-purl",
"show_purl",
is_flag=True,
default=False,
help="Display purl.",
)
@click.pass_context
@progress_bar
def get_component_tree(ctx, component_uuid, nvr, purl, show_purl):
"""Retrieve Component dependency tree."""
if not component_uuid and not purl and not nvr:
click.echo(ctx.get_help())
exit(0)
ctx.ensure_object(dict)
# ctx.obj["FORMAT"] = "text"
session = CorgiService.create_session()
if purl:
c = session.components.retrieve_list(purl=purl, include_fields="uuid")
component_uuid = c["uuid"]
elif nvr:
c = session.components.retrieve_list(nvr=nvr, include_fields="uuid")
component_uuid = c.results[0].uuid
c = session.components.retrieve(component_uuid, include_fields="uuid,nvr,arch")
if c.arch == "src" or c.arch == "noarch":
data = requests.get(f"{CORGI_API_URL}/api/v1/components/{component_uuid}/taxonomy")
return cprint(data.json(), ctx=ctx)
else:
logger.info(f"{c.nvr},{c.arch} not a root component.")


# PRODUCT STREAM
@corgi_grp.group(help=f"{CORGI_API_URL}/api/v1/product_streams")
@click.pass_context
Expand Down
27 changes: 27 additions & 0 deletions griffon/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from packageurl import PackageURL
from rich.console import Console
from rich.text import Text
from rich.tree import Tree

console = Console(color_system="auto")

Expand Down Expand Up @@ -86,6 +87,30 @@ def output_version(ctx, version):
return version


def walk_component_tree(obj, key, tree, show_purl=False):
if isinstance(obj, dict):
for k, v in obj.items():
if isinstance(v, (dict, list)):
walk_component_tree(v, k, tree, show_purl=show_purl)
elif isinstance(obj, list):
for item in obj:
label = f"{item['node_type']}: {item['nvr']},{item['type']}"
if show_purl:
label = f"{item['node_type']}: {item['purl']}"
child = tree.add(label)
walk_component_tree(item, None, child, show_purl=show_purl)
return tree


def text_output_tree(ctx, output):
tree = Tree(
"component dependency tree",
guide_style="bold magenta",
)
console.print(walk_component_tree(output, None, tree, show_purl=ctx.params["show_purl"]))
ctx.exit()


def text_output_product_summary(ctx, output, format, exclude_products):
ordered_results = sorted(output["results"], key=lambda d: d["name"])

Expand Down Expand Up @@ -823,6 +848,8 @@ def cprint(
text_output_purls(ctx, output, format)
if ctx.info_name == "sources":
text_output_purls(ctx, output, format)
if ctx.info_name == "tree":
text_output_tree(ctx, output)

# last chance text formatted output
text_output_generic(ctx, output, format)
Expand Down

0 comments on commit 84cc31f

Please sign in to comment.