Added `--log-level` flag
This commit is contained in:
parent
6b8c0d3200
commit
ce5c516de1
|
@ -43,4 +43,6 @@ For example:
|
||||||
#### Deleting blocklists and firewall policy
|
#### Deleting blocklists and firewall policy
|
||||||
To delete the blocklists from Cloudflare and delete the firewall policy, use the `delete` subcommand.
|
To delete the blocklists from Cloudflare and delete the firewall policy, use the `delete` subcommand.
|
||||||
For example:
|
For example:
|
||||||
`cloudflare-gateway-adblocking delete`
|
`cloudflare-gateway-adblocking delete`
|
||||||
|
### Help
|
||||||
|
For help, use the `--help` flag.
|
|
@ -16,20 +16,6 @@ ACCOUNT_ID = None
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Setup logging
|
|
||||||
logger.remove()
|
|
||||||
# ^10 is a formatting directive to center with a padding of 10
|
|
||||||
logger_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> |<level>{level: ^10}</level>| <level>{message}</level>" # noqa E501
|
|
||||||
logger.add(stderr, format=logger_format, colorize=True, level="DEBUG")
|
|
||||||
|
|
||||||
# Load .env if it exists
|
|
||||||
# This must precede the argparse setup as env variables are used as default values
|
|
||||||
if Path(".env").is_file():
|
|
||||||
dotenv.load_dotenv()
|
|
||||||
logger.info("Loaded .env file")
|
|
||||||
else:
|
|
||||||
logger.info("No .env file found")
|
|
||||||
|
|
||||||
# Parse arguments
|
# Parse arguments
|
||||||
|
|
||||||
# Set up argparse
|
# Set up argparse
|
||||||
|
@ -43,17 +29,20 @@ def main():
|
||||||
credential_args = argparser.add_argument_group("Cloudflare Credentials")
|
credential_args = argparser.add_argument_group("Cloudflare Credentials")
|
||||||
|
|
||||||
# Add arguments
|
# Add arguments
|
||||||
|
|
||||||
|
# General arguments
|
||||||
|
argparser.add_argument('--log-level', '-l', help='Log level', default='INFO') # noqa E501
|
||||||
|
|
||||||
|
# Credential arguments
|
||||||
credential_args.add_argument(
|
credential_args.add_argument(
|
||||||
"--account-id",
|
"--account-id",
|
||||||
"-a",
|
"-a",
|
||||||
help="Cloudflare account ID - environment variable: CLOUDFLARE_ACCOUNT_ID",
|
help="Cloudflare account ID - environment variable: CLOUDFLARE_ACCOUNT_ID",
|
||||||
default=os.environ.get("CLOUDFLARE_ACCOUNT_ID"),
|
|
||||||
)
|
)
|
||||||
credential_args.add_argument(
|
credential_args.add_argument(
|
||||||
"--token",
|
"--token",
|
||||||
"-t",
|
"-t",
|
||||||
help="Cloudflare API token - environment variable: CLOUDFLARE_TOKEN",
|
help="Cloudflare API token - environment variable: CLOUDFLARE_TOKEN",
|
||||||
default=os.environ.get("CLOUDFLARE_TOKEN"),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add subcommands
|
# Add subcommands
|
||||||
|
@ -88,19 +77,35 @@ def main():
|
||||||
|
|
||||||
args = argparser.parse_args()
|
args = argparser.parse_args()
|
||||||
|
|
||||||
|
# Set up logging
|
||||||
|
set_primary_logger(args.log_level)
|
||||||
logger.debug(args)
|
logger.debug(args)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Load variables
|
# Load variables
|
||||||
global TOKEN
|
global TOKEN
|
||||||
global ACCOUNT_ID
|
global ACCOUNT_ID
|
||||||
TOKEN = args.token
|
TOKEN = args.token
|
||||||
ACCOUNT_ID = args.account_id
|
ACCOUNT_ID = args.account_id
|
||||||
# Check if variables are set
|
|
||||||
|
# Check if credentials are set, if they are not, attempt to load from environment variables and .env # noqa E501
|
||||||
if TOKEN is None or ACCOUNT_ID is None:
|
if TOKEN is None or ACCOUNT_ID is None:
|
||||||
logger.error(
|
logger.info("No credentials provided with flags")
|
||||||
"No environment variables found. Please create a .env file or .envrc file"
|
if Path(".env").is_file():
|
||||||
) # noqa E501
|
logger.debug("Loading .env")
|
||||||
exit(1)
|
dotenv.load_dotenv()
|
||||||
|
else:
|
||||||
|
logger.debug("No .env file found")
|
||||||
|
try:
|
||||||
|
TOKEN = os.environ["CLOUDFLARE_TOKEN"]
|
||||||
|
ACCOUNT_ID = os.environ["CLOUDFLARE_ACCOUNT_ID"]
|
||||||
|
logger.info("Loaded credentials from environment variables")
|
||||||
|
except KeyError:
|
||||||
|
logger.error("No credentials provided")
|
||||||
|
argparser.print_help()
|
||||||
|
exit(1)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
args.func(args)
|
args.func(args)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -128,6 +133,12 @@ def delete_from_cloudflare(args):
|
||||||
lists = utils.filter_adblock_lists(lists)
|
lists = utils.filter_adblock_lists(lists)
|
||||||
delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)
|
delete.delete_adblock_list(lists, ACCOUNT_ID, TOKEN)
|
||||||
|
|
||||||
|
def set_primary_logger(log_level):
|
||||||
|
logger.remove()
|
||||||
|
# ^10 is a formatting directive to center with a padding of 10
|
||||||
|
logger_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> |<level>{level: ^10}</level>| <level>{message}</level>" # noqa E501
|
||||||
|
logger.add(stderr, format=logger_format, colorize=True, level=log_level)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -78,7 +78,7 @@ def convert_to_list(file: pathlib.Path) -> list:
|
||||||
for match in matches
|
for match in matches
|
||||||
if match and match.group(1) not in loopback
|
if match and match.group(1) not in loopback
|
||||||
]
|
]
|
||||||
print(f"First 5 hosts: {hosts[:5]}")
|
# print(f"First 5 hosts: {hosts[:5]}")
|
||||||
return hosts
|
return hosts
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue