|
1 | 1 | """Example cli using click.""" |
| 2 | +import logging |
2 | 3 | import sys |
3 | 4 |
|
4 | 5 | import click |
5 | 6 |
|
6 | | -from pyurlcheck.check import CheckUrl |
| 7 | +from pyurlcheck.check import split_url, get_ip, is_private |
7 | 8 | from pyurlcheck.find import FindUrls |
8 | 9 | from pyurlcheck.validate import ValidateUrl |
9 | 10 |
|
10 | 11 |
|
| 12 | +LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
11 | 15 | @click.command() |
12 | 16 | @click.argument("input_data", type=click.Path(exists=True, file_okay=True, dir_okay=True), required=True) |
13 | | -def main(input_data): |
| 17 | +@click.option("--log_level", type=click.Choice(["INFO", "DEBUG"], case_sensitive=False), default="INFO") |
| 18 | +def main(input_data, log_level): |
14 | 19 | """Entry point into the pyurlcheck command line tool. |
15 | 20 |
|
16 | 21 | Args: |
17 | 22 | input_data (str): Either filename or directory to search for URLs. |
| 23 | + log_level (str): Logging level to execute with. Choices ['INFO', 'DEBUG']. Default is INFO. |
18 | 24 | """ |
| 25 | + logging.basicConfig(format="%(asctime)s %(message)s", stream=sys.stdout, level=getattr(logging, log_level)) |
19 | 26 | results = [] |
20 | 27 | files_urls = FindUrls(input_data).find_urls() |
21 | 28 | for file_name, url_list in files_urls.items(): |
22 | 29 | for line_num, urls in url_list.items(): |
23 | 30 | for url in urls: |
24 | | - url_details = CheckUrl(url).split_url() |
25 | | - if url_details.scheme == "": |
26 | | - is_valid = ValidateUrl(url, need_scheme=True).validate() |
27 | | - # print(f"URL is {url}") |
28 | | - # print(f"Is RFC1918: {is_private}") |
29 | | - else: |
30 | | - is_valid = ValidateUrl(url).validate() |
31 | | - if not is_valid: |
32 | | - results.append(f"{file_name}:{line_num + 1}\tURL Issue: {url}") |
| 31 | + url_details = split_url(url) |
| 32 | + # RFC 1918 Check, if True don't validate. |
| 33 | + if not is_private(get_ip(url_details.netloc)): |
| 34 | + if url_details.scheme == "": |
| 35 | + is_valid, has_redirects = ValidateUrl(url, need_scheme=True).validate() |
| 36 | + else: |
| 37 | + is_valid, has_redirects = ValidateUrl(url).validate() |
| 38 | + if not is_valid: |
| 39 | + results.append(f"{file_name}:{line_num + 1}\tURL Issue: {url}") |
| 40 | + if has_redirects: |
| 41 | + LOGGER.info( |
| 42 | + "Redirect_Warning: %s had redirects while executing. Redirects are ' => '.join(%s)!", |
| 43 | + url, |
| 44 | + has_redirects, |
| 45 | + ) |
33 | 46 | if len(results) > 0: |
34 | | - print("\n".join(results)) |
| 47 | + LOGGER.info("\n".join(results)) |
35 | 48 | sys.exit(len(results)) |
36 | 49 | else: |
37 | 50 | sys.exit(0) |
|
0 commit comments