From 183dc2eca60b3692746ef0433bb3ef4ed346d97d Mon Sep 17 00:00:00 2001 From: Veinar Date: Mon, 25 Nov 2024 00:26:03 +0100 Subject: [PATCH] Moved common CLI decorators to separate module + test update --- envcloak/commands/compare.py | 3 +- envcloak/commands/decrypt.py | 17 +++++------ envcloak/commands/encrypt.py | 17 +++++------ envcloak/commands/generate_key.py | 7 ++--- .../commands/generate_key_from_password.py | 7 ++--- envcloak/commands/rotate_keys.py | 7 ++--- envcloak/decorators/common_decorators.py | 30 +++++++++++++++++++ envcloak/utils.py | 8 ----- tests/test_cli.py | 20 ++++++------- 9 files changed, 66 insertions(+), 50 deletions(-) create mode 100644 envcloak/decorators/common_decorators.py diff --git a/envcloak/commands/compare.py b/envcloak/commands/compare.py index 3ae0a12..b585c9f 100644 --- a/envcloak/commands/compare.py +++ b/envcloak/commands/compare.py @@ -4,7 +4,8 @@ from pathlib import Path import click from click import style -from envcloak.utils import debug_log, debug_option +from envcloak.utils import debug_log +from envcloak.decorators.common_decorators import debug_option from envcloak.validation import check_file_exists, check_directory_exists from envcloak.encryptor import decrypt_file from envcloak.exceptions import FileDecryptionException diff --git a/envcloak/commands/decrypt.py b/envcloak/commands/decrypt.py index f6760c7..d46cc96 100644 --- a/envcloak/commands/decrypt.py +++ b/envcloak/commands/decrypt.py @@ -3,7 +3,12 @@ from pathlib import Path import click from click import style -from envcloak.utils import debug_log, debug_option, calculate_required_space +from envcloak.utils import debug_log, calculate_required_space +from envcloak.decorators.common_decorators import ( + debug_option, + dry_run_option, + force_option, +) from envcloak.validation import ( check_file_exists, check_directory_exists, @@ -22,6 +27,8 @@ @click.command() @debug_option +@dry_run_option +@force_option @click.option( "--input", "-i", @@ -43,14 +50,6 @@ @click.option( "--key-file", "-k", required=True, help="Path to the decryption key file." ) -@click.option( - "--dry-run", is_flag=True, help="Perform a dry run without making any changes." -) -@click.option( - "--force", - is_flag=True, - help="Force overwrite of existing decrypted files or directories.", -) def decrypt(input, directory, output, key_file, dry_run, force, debug): """ Decrypt environment variables from a file or all files in a directory. diff --git a/envcloak/commands/encrypt.py b/envcloak/commands/encrypt.py index 9e07283..1b31699 100644 --- a/envcloak/commands/encrypt.py +++ b/envcloak/commands/encrypt.py @@ -3,7 +3,12 @@ from pathlib import Path import click from click import style -from envcloak.utils import debug_log, debug_option, calculate_required_space +from envcloak.utils import debug_log, calculate_required_space +from envcloak.decorators.common_decorators import ( + debug_option, + force_option, + dry_run_option, +) from envcloak.validation import ( check_file_exists, check_directory_exists, @@ -22,6 +27,8 @@ @click.command() @debug_option +@dry_run_option +@force_option @click.option( "--input", "-i", required=False, help="Path to the input file (e.g., .env)." ) @@ -40,14 +47,6 @@ @click.option( "--key-file", "-k", required=True, help="Path to the encryption key file." ) -@click.option( - "--dry-run", is_flag=True, help="Perform a dry run without making any changes." -) -@click.option( - "--force", - is_flag=True, - help="Force overwrite of existing encrypted files or directories.", -) def encrypt(input, directory, output, key_file, dry_run, force, debug): """ Encrypt environment variables from a file or all files in a directory. diff --git a/envcloak/commands/generate_key.py b/envcloak/commands/generate_key.py index e8c3d22..b15539a 100644 --- a/envcloak/commands/generate_key.py +++ b/envcloak/commands/generate_key.py @@ -2,21 +2,20 @@ import click from envcloak.validation import check_output_not_exists, check_disk_space from envcloak.generator import generate_key_file -from envcloak.utils import add_to_gitignore, debug_option, debug_log +from envcloak.utils import add_to_gitignore, debug_log +from envcloak.decorators.common_decorators import debug_option, dry_run_option from envcloak.exceptions import OutputFileExistsException, DiskSpaceException @click.command() @debug_option +@dry_run_option @click.option( "--output", "-o", required=True, help="Path to save the generated encryption key." ) @click.option( "--no-gitignore", is_flag=True, help="Skip adding the key file to .gitignore." ) -@click.option( - "--dry-run", is_flag=True, help="Perform a dry run without making any changes." -) def generate_key(output, no_gitignore, dry_run, debug): """ Generate a new encryption key. diff --git a/envcloak/commands/generate_key_from_password.py b/envcloak/commands/generate_key_from_password.py index 4315ac3..a897726 100644 --- a/envcloak/commands/generate_key_from_password.py +++ b/envcloak/commands/generate_key_from_password.py @@ -2,7 +2,8 @@ import click from envcloak.validation import check_output_not_exists, check_disk_space, validate_salt from envcloak.generator import generate_key_from_password_file -from envcloak.utils import debug_log, debug_option, add_to_gitignore +from envcloak.utils import debug_log, add_to_gitignore +from envcloak.decorators.common_decorators import debug_option, dry_run_option from envcloak.exceptions import ( OutputFileExistsException, DiskSpaceException, @@ -12,6 +13,7 @@ @click.command() @debug_option +@dry_run_option @click.option( "--password", "-p", required=True, help="Password to derive the encryption key." ) @@ -24,9 +26,6 @@ @click.option( "--no-gitignore", is_flag=True, help="Skip adding the key file to .gitignore." ) -@click.option( - "--dry-run", is_flag=True, help="Perform a dry run without making any changes." -) def generate_key_from_password(password, salt, output, no_gitignore, dry_run, debug): """ Derive an encryption key from a password and salt. diff --git a/envcloak/commands/rotate_keys.py b/envcloak/commands/rotate_keys.py index cce3a26..eece152 100644 --- a/envcloak/commands/rotate_keys.py +++ b/envcloak/commands/rotate_keys.py @@ -1,6 +1,7 @@ import os import click -from envcloak.utils import debug_log, debug_option +from envcloak.utils import debug_log +from envcloak.decorators.common_decorators import debug_option, dry_run_option from envcloak.validation import ( check_file_exists, check_permissions, @@ -18,6 +19,7 @@ @click.command() @debug_option +@dry_run_option @click.option( "--input", "-i", required=True, help="Path to the encrypted file to re-encrypt." ) @@ -28,9 +30,6 @@ "--new-key-file", "-nk", required=True, help="Path to the new encryption key." ) @click.option("--output", "-o", required=True, help="Path to the re-encrypted file.") -@click.option( - "--dry-run", is_flag=True, help="Perform a dry run without making any changes." -) def rotate_keys(input, old_key_file, new_key_file, output, dry_run, debug): """ Rotate encryption keys by re-encrypting a file with a new key. diff --git a/envcloak/decorators/common_decorators.py b/envcloak/decorators/common_decorators.py new file mode 100644 index 0000000..a394ebe --- /dev/null +++ b/envcloak/decorators/common_decorators.py @@ -0,0 +1,30 @@ +import click + + +def dry_run_option(func): + """ + Add a `--dry-run` flag to a Click command. + """ + return click.option( + "--dry-run", is_flag=True, help="Perform a dry run without making any changes." + )(func) + + +def debug_option(func): + """ + Add a `--debug` flag to a Click command. + """ + return click.option( + "--debug", is_flag=True, help="Enable debug mode for detailed logs." + )(func) + + +def force_option(func): + """ + Add a `--force` flag to a Click command. + """ + return click.option( + "--force", + is_flag=True, + help="Force overwrite of existing files or directories.", + )(func) diff --git a/envcloak/utils.py b/envcloak/utils.py index 93c3682..4eeddeb 100644 --- a/envcloak/utils.py +++ b/envcloak/utils.py @@ -1,6 +1,5 @@ import os from pathlib import Path -import click def add_to_gitignore(directory: str, filename: str): @@ -61,10 +60,3 @@ def debug_log(message, debug): """ if debug: print(message) - - -def debug_option(func): - """ - A reusable decorator for debug - """ - return click.option("--debug", is_flag=True, help="enable debug mode")(func) diff --git a/tests/test_cli.py b/tests/test_cli.py index 1eb8341..a14e24e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -11,15 +11,15 @@ from envcloak.generator import derive_key # Updated import list for command modularization -from envcloak.commands.encrypt import encrypt_file -from envcloak.commands.decrypt import decrypt_file -from envcloak.commands.generate_key import generate_key_file -from envcloak.commands.generate_key_from_password import generate_key_from_password_file -from envcloak.commands.rotate_keys import ( - encrypt_file as rotate_encrypt_file, - decrypt_file as rotate_decrypt_file, -) -from envcloak.utils import add_to_gitignore +# from envcloak.commands.encrypt import encrypt_file +# from envcloak.commands.decrypt import decrypt_file +# from envcloak.commands.generate_key import generate_key_file +# from envcloak.commands.generate_key_from_password import generate_key_from_password_file +# from envcloak.commands.rotate_keys import ( +# encrypt_file as rotate_encrypt_file, +# decrypt_file as rotate_decrypt_file, +# ) +# from envcloak.utils import add_to_gitignore @pytest.fixture @@ -400,7 +400,6 @@ def test_encrypt_with_mixed_input_and_directory(runner, mock_files): ], ) - assert result.exit_code != 0 assert "You must provide either --input or --directory, not both." in result.output @@ -427,7 +426,6 @@ def test_decrypt_with_mixed_input_and_directory(runner, mock_files): ], ) - assert result.exit_code != 0 assert "You must provide either --input or --directory, not both." in result.output