Skip to content

Commit

Permalink
add command-line interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MFTabriz committed Jan 23, 2025
1 parent 0f06db1 commit 0cc2b51
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
90 changes: 90 additions & 0 deletions sectxt/command_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Module that provides a command-line interface to SecTXT."""

import argparse
from typing import List

from . import ErrorDict, SecurityTXT, __version__


class TaggedErrorDict(ErrorDict):
tag: str


def tag_messages(
messages_list: List[ErrorDict], message_type: str
) -> List[TaggedErrorDict]:
"""Add message_type to each message in the list as {"tag": message_type}"""

return [dict(msg, **{"tag": message_type}) for msg in messages_list]


def human_readable_print(messages_list: List[TaggedErrorDict]) -> None:
"""Print messages in a human-readable format"""

for message in messages_list:
if message["line"]:
print(
"[" + message["tag"] + "]",
"L" + str(message["line"]) + ":",
message["code"],
":",
message["message"],
)
else:
print("[" + message["tag"] + "]", message["code"], ":", message["message"])

if len(messages_list) == 0:
print("✓ No issues were detected!")

def main() -> int:
"""Main function which parses the arguments, calls SecurityTXT()
and prints output messages.
Returns:
0 if the security.txt file does not contain any errors; otherwise, 1
"""

parser = argparse.ArgumentParser(description="Parse and validate security.txt")
parser.add_argument("address", help="website URL or path to local file to check")
parser.add_argument(
"--json", action="store_true", help="output the results in JSON format"
)
parser.add_argument(
"--no-recommend-unknown-fields",
action="store_true",
help="do not issue notifications for unknown fields",
)
parser.add_argument(
"--show-notifications", action="store_true", help="show notifications"
)
parser.add_argument(
"--show-recommendations", action="store_true", help="show recommendations"
)
parser.add_argument(
"--version", action="version", version="%(prog)s " + __version__
)
args = parser.parse_args()

address_is_local = not args.address.startswith("http")

s = SecurityTXT(
args.address,
recommend_unknown_fields=not args.no_recommend_unknown_fields,
is_local=address_is_local,
)

output_messages = tag_messages(s.errors, "ERROR")

if args.show_recommendations:
output_messages += tag_messages(s.recommendations, "REC")

if args.show_notifications:
output_messages += tag_messages(s.notifications, "NOTE")

if args.json:
print(output_messages)
else:
human_readable_print(output_messages)

return 0 if s.is_valid() else 1
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ install_requires =
langcodes==3.3.0
PGPy-dtc==0.1.0
validators==0.32.0

[options.entry_points]
console_scripts =
sectxt = sectxt.command_line:main

0 comments on commit 0cc2b51

Please sign in to comment.