Skip to content

Commit

Permalink
Moved common CLI decorators to separate module + test update
Browse files Browse the repository at this point in the history
  • Loading branch information
Veinar committed Nov 24, 2024
1 parent 29998be commit 183dc2e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 50 deletions.
3 changes: 2 additions & 1 deletion envcloak/commands/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions envcloak/commands/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,6 +27,8 @@

@click.command()
@debug_option
@dry_run_option
@force_option
@click.option(
"--input",
"-i",
Expand All @@ -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.
Expand Down
17 changes: 8 additions & 9 deletions envcloak/commands/encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)."
)
Expand All @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions envcloak/commands/generate_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions envcloak/commands/generate_key_from_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -12,6 +13,7 @@

@click.command()
@debug_option
@dry_run_option
@click.option(
"--password", "-p", required=True, help="Password to derive the encryption key."
)
Expand All @@ -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.
Expand Down
7 changes: 3 additions & 4 deletions envcloak/commands/rotate_keys.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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."
)
Expand All @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions envcloak/decorators/common_decorators.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 0 additions & 8 deletions envcloak/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from pathlib import Path
import click


def add_to_gitignore(directory: str, filename: str):
Expand Down Expand Up @@ -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)
20 changes: 9 additions & 11 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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


Expand Down

0 comments on commit 183dc2e

Please sign in to comment.