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

Improve b261af9fbd8bfcc3f412ab48f283c1970e5841c0 (#568) #634

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion detect_secrets/audit/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from ..types import SecretContext
from ..util.color import AnsiColor
from ..util.color import colorize
from detect_secrets.exceptions import SecretNotFoundOnSpecifiedLineError


def print_message(message: str) -> None:
Expand All @@ -34,7 +35,10 @@ def print_context(context: SecretContext) -> None:

context.snippet.add_line_numbers()
if context.secret.secret_value:
context.snippet.highlight_line(context.secret.secret_value)
try:
context.snippet.highlight_line(context.secret.secret_value)
except (ValueError, IndexError):
raise SecretNotFoundOnSpecifiedLineError(context.secret.line_number)
else:
context.snippet.target_line = colorize(context.snippet.target_line, AnsiColor.BOLD)
print_message(str(context.snippet))
Expand Down
22 changes: 9 additions & 13 deletions detect_secrets/util/code_snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from .color import AnsiColor
from .color import colorize
from detect_secrets.exceptions import SecretNotFoundOnSpecifiedLineError


def get_code_snippet(
Expand Down Expand Up @@ -72,19 +71,16 @@ def highlight_line(self, payload: str) -> 'CodeSnippet':
"""
:param payload: string to highlight, on chosen line
"""
try:
index_of_payload = self.target_line.lower().index(payload.lower())
end_of_payload = index_of_payload + len(payload)

self.target_line = u'{}{}{}'.format(
self.target_line[:index_of_payload],
self.apply_highlight(self.target_line[index_of_payload:end_of_payload]),
self.target_line[end_of_payload:],
)
index_of_payload = self.target_line.lower().index(payload.lower())
end_of_payload = index_of_payload + len(payload)

self.target_line = "{}{}{}".format(
self.target_line[:index_of_payload],
self.apply_highlight(self.target_line[index_of_payload:end_of_payload]),
self.target_line[end_of_payload:],
)

return self
except ValueError:
raise SecretNotFoundOnSpecifiedLineError(self.target_index)
return self

def get_line_number(self, line_number: int) -> str:
"""Broken out, for custom colorization."""
Expand Down