Skip to content

Commit

Permalink
Add --debug option
Browse files Browse the repository at this point in the history
This changes the previously hard-coded log level of `DEBUG` to the one
dependent on the option passed by the user: it is either `DEBUG` or (by
default) `WARNING` now. The user can enable or disable the debug mode on
demand by using the respective flag.
  • Loading branch information
Toreno96 committed Dec 28, 2023
1 parent 3f28469 commit ad7f0d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 4 additions & 2 deletions onetimepass/logging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import logging

from onetimepass import settings
Expand All @@ -10,9 +11,10 @@
```
"""

logging.basicConfig(
logging_basic_config = functools.partial(
logging.basicConfig,
format="[%(asctime)s] %(levelname)s:%(module)s:%(lineno)d: %(message)s",
level=settings.LOG_LEVEL,
level=settings.DEFAULT_LOG_LEVEL,
)

logger = logging.getLogger(__name__)
17 changes: 16 additions & 1 deletion onetimepass/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import functools
import json
import logging
import pathlib
import time
from typing import Dict
Expand Down Expand Up @@ -32,6 +33,7 @@
from onetimepass.exceptions import UnhandledFormatException
from onetimepass.exceptions import UnhandledOTPTypeException
from onetimepass.logging import logger
from onetimepass.logging import logging_basic_config
from onetimepass.otpauth import ParsingError
from onetimepass.otpauth import Uri

Expand Down Expand Up @@ -122,11 +124,24 @@ def validation_error_to_str(error: pydantic.ValidationError) -> str:
default=master_key.MasterKey.keyring_available(),
show_default="True if keyring installed, False otherwise",
)
@click.option(
"debug",
"-d/-D",
"--debug/--no-debug",
default=False,
show_default=True,
help="Enable/disable debug info.",
)
@click.pass_context
def otp(ctx: click.Context, color: bool, quiet: bool, keyring_: bool):
def otp(ctx: click.Context, color: bool, quiet: bool, keyring_: bool, debug: bool):
ctx.ensure_object(dict)
ctx.obj.update({"color": color, "quiet": quiet, "keyring_": keyring_})

if debug:
logging_basic_config(level=logging.DEBUG)
else:
logging_basic_config()


@otp.command(help="Print the one-time password for the specified ALIAS.")
@click.argument("alias")
Expand Down
4 changes: 1 addition & 3 deletions onetimepass/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@
KEYRING_SERVICE_NAME = APP_NAME
KEYRING_USERNAME = "master key"

LOG_LEVEL = (
logging.DEBUG
) # TODO change this for the production version OR implement CLI option for that
DEFAULT_LOG_LEVEL = logging.WARNING

0 comments on commit ad7f0d5

Please sign in to comment.