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

Commit

Permalink
-r search should highlight matched term
Browse files Browse the repository at this point in the history
  • Loading branch information
JimFuller-RedHat committed Jan 18, 2024
1 parent 1dfbd03 commit 944713e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
* refactor output format of service products-contain-component (GRIF-209)
* container roots are excluded by default (need to use --include-container-roots)
* when using -r highlight matched term

### Added
* --include-container_roots in service products-contain-component
Expand Down
1 change: 1 addition & 0 deletions griffon/commands/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ def get_product_contain_component(
if verbose:
ctx.obj["VERBOSE"] = verbose
ctx.obj["INCLUDE_CONTAINER_ROOTS"] = include_container_roots
ctx.obj["REGEX_NAME_SEARCH"] = regex_name_search
if (
not search_latest
and not search_all
Expand Down
80 changes: 38 additions & 42 deletions griffon/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,24 @@ def process_product_color(build_type: str) -> str:
return "magenta"


def highlight_search_term(search_pattern, text_value):
return re.sub(search_pattern, "[b]\\g<0>[/b]", text_value)


def text_output_products_contain_component(
ctx,
output,
exclude_products,
exclude_components,
no_wrap=False,
):
search_component_name = ctx.params["component_name"]
logger.info(ctx.obj["REGEX_NAME_SEARCH"])
# if -r option used we need to escape it
search_component_name = (
re.escape(ctx.params["component_name"])
if not ctx.obj["REGEX_NAME_SEARCH"]
else ctx.params["component_name"]
)

# handle single component
if ctx.params["purl"]:
Expand Down Expand Up @@ -470,10 +480,8 @@ def text_output_products_contain_component(
dep_name = nvr
# highlight search term
try:
dep_name = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
dep_name,
dep_name = highlight_search_term(
search_component_name, dep_name
)
except re.error:
pass
Expand Down Expand Up @@ -541,20 +549,17 @@ def text_output_products_contain_component(
# dep_name = nvr
dep_name = nvr
try:
dep_name = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
dep_name,
dep_name = highlight_search_term(
search_component_name, dep_name
)
except re.error:
pass
dep = f"[grey93]{dep_name} ({result_tree[pv][ps][cn][nvr]['type']})[/grey93]" # noqa
related_url = result_tree[pv][ps][cn][nvr].get("related_url")
try:
if result_tree[pv][ps][cn][nvr]["related_url"]:
related_url = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
related_url = highlight_search_term(
search_component_name,
result_tree[pv][ps][cn][nvr]["related_url"],
)
except re.error:
Expand Down Expand Up @@ -645,9 +650,8 @@ def text_output_products_contain_component(
child_dep_names = ""
if provides_components:
try:
child_dep_names = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
child_dep_names = highlight_search_term(
search_component_name,
", ".join(provides_components),
)
child_dep_names = f"[{child_dep_names}]"
Expand Down Expand Up @@ -677,9 +681,8 @@ def text_output_products_contain_component(
# highlight search term
dep_name = nvr
try:
dep_name = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
dep_name = highlight_search_term(
search_component_name,
dep_name,
)
except re.error:
Expand All @@ -688,11 +691,11 @@ def text_output_products_contain_component(
related_url = result_tree[pv][ps][cn][nvr].get("related_url")
try:
if result_tree[pv][ps][cn][nvr]["related_url"]:
related_url = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
related_url = highlight_search_term(
search_component_name,
result_tree[pv][ps][cn][nvr]["related_url"],
)

except re.error:
pass
build_source_url = ""
Expand Down Expand Up @@ -797,9 +800,8 @@ def text_output_products_contain_component(
child_dep_names = ""
if provides_components:
try:
child_dep_names = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
child_dep_names = highlight_search_term(
search_component_name,
", ".join(provides_components),
)
child_dep_names = f"[{child_dep_names}]"
Expand Down Expand Up @@ -833,9 +835,8 @@ def text_output_products_contain_component(
# highlight search term
dep_name = nvr
try:
dep_name = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
dep_name = highlight_search_term(
search_component_name,
dep_name,
)
except re.error:
Expand All @@ -844,9 +845,8 @@ def text_output_products_contain_component(
related_url = result_tree[pv][ps][cn][nvr].get("related_url")
try:
if result_tree[pv][ps][cn][nvr]["related_url"]:
related_url = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
related_url = highlight_search_term(
search_component_name,
result_tree[pv][ps][cn][nvr]["related_url"],
)
except re.error:
Expand Down Expand Up @@ -920,9 +920,8 @@ def text_output_products_contain_component(
child_dep_names = ""
if provides_components:
try:
child_dep_names = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
child_dep_names = highlight_search_term(
search_component_name,
", ".join(provides_components),
)
child_dep_names = f"[{child_dep_names}]"
Expand Down Expand Up @@ -956,9 +955,8 @@ def text_output_products_contain_component(
# highlight search term
dep_name = result_tree[pv][ps][cn][nvr]["purl"]
try:
dep_name = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
dep_name = highlight_search_term(
search_component_name,
dep_name,
)
except re.error:
Expand All @@ -967,9 +965,8 @@ def text_output_products_contain_component(
related_url = result_tree[pv][ps][cn][nvr].get("related_url")
try:
if result_tree[pv][ps][cn][nvr]["related_url"]:
related_url = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
related_url = highlight_search_term(
search_component_name,
result_tree[pv][ps][cn][nvr]["related_url"],
)
except re.error:
Expand Down Expand Up @@ -1043,9 +1040,8 @@ def text_output_products_contain_component(
child_dep_names = ""
if provides_components:
try:
child_dep_names = re.sub(
re.escape(search_component_name),
f"[b]{search_component_name}[/b]",
child_dep_names = highlight_search_term(
search_component_name,
", ".join(provides_components),
)
child_dep_names = f"[{child_dep_names}]"
Expand Down
1 change: 1 addition & 0 deletions scripts/smoke-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@ griffon service component-flaws npm
griffon service component-flaws python-marshmallow --affectedness AFFECTED
griffon service products-contain-component --search-all --search-upstreams -s libxml2 -a
griffon service products-contain-component -s grep -v --search-all
griffon service products-contain-component -r 'webkit.tk' -vv

0 comments on commit 944713e

Please sign in to comment.