-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from GitGuardian/agateau/verbose-everywhere
Allow --verbose, --debug and --allow-self-signed everywhere
- Loading branch information
Showing
35 changed files
with
340 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
from typing import Any | ||
|
||
import click | ||
|
||
from ggshield.cmd.common_options import add_common_options | ||
|
||
from .login import login_cmd | ||
from .logout import logout_cmd | ||
|
||
|
||
@click.group(commands={"login": login_cmd, "logout": logout_cmd}) | ||
def auth_group() -> None: | ||
@add_common_options() | ||
def auth_group(**kwargs: Any) -> None: | ||
"""Commands to manage authentication.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
This module defines options which should be available on all commands, such as the | ||
-v, --verbose option. | ||
To use it: | ||
- Add the `@add_common_options()` decorator after all the `click.option()` calls of the | ||
command function. | ||
- Add a `**kwargs: Any` argument to the command function. | ||
The `kwargs` argument is required because due to the way click works, | ||
`add_common_options()` adds an argument for each option it defines. | ||
""" | ||
from typing import Any, Callable, Optional, cast | ||
|
||
import click | ||
|
||
from ggshield.cmd.debug_logs import setup_debug_logs | ||
from ggshield.core.config.user_config import UserConfig | ||
|
||
|
||
AnyFunction = Callable[..., Any] | ||
|
||
|
||
def _get_config(ctx: click.Context) -> UserConfig: | ||
return cast(UserConfig, ctx.obj["config"].user_config) | ||
|
||
|
||
def _verbose_callback( | ||
ctx: click.Context, param: click.Parameter, value: Optional[bool] | ||
) -> Optional[bool]: | ||
if value is not None: | ||
_get_config(ctx).verbose = value | ||
return value | ||
|
||
|
||
_verbose_option = click.option( | ||
"-v", | ||
"--verbose", | ||
is_flag=True, | ||
default=None, | ||
help="Verbose display mode.", | ||
callback=_verbose_callback, | ||
) | ||
|
||
|
||
def debug_callback( | ||
ctx: click.Context, param: click.Parameter, value: Optional[bool] | ||
) -> Optional[bool]: | ||
# The --debug option is marked as "is_eager" so that we can setup logs as soon as | ||
# possible. If we don't then log commands for the creation of the Config instance | ||
# are ignored | ||
if value is not None: | ||
setup_debug_logs(value is True) | ||
return value | ||
|
||
|
||
_debug_option = click.option( | ||
"--debug", | ||
is_flag=True, | ||
default=None, | ||
is_eager=True, | ||
help="Show debug information.", | ||
callback=debug_callback, | ||
) | ||
|
||
|
||
def allow_self_signed_callback( | ||
ctx: click.Context, param: click.Parameter, value: Optional[bool] | ||
) -> Optional[bool]: | ||
if value is not None: | ||
_get_config(ctx).allow_self_signed = value | ||
return value | ||
|
||
|
||
_allow_self_signed_option = click.option( | ||
"--allow-self-signed", | ||
is_flag=True, | ||
default=None, | ||
help="Ignore ssl verification.", | ||
callback=allow_self_signed_callback, | ||
) | ||
|
||
|
||
def add_common_options() -> Callable[[AnyFunction], AnyFunction]: | ||
def decorator(cmd: AnyFunction) -> AnyFunction: | ||
_verbose_option(cmd) | ||
_debug_option(cmd) | ||
_allow_self_signed_option(cmd) | ||
return cmd | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import logging | ||
import sys | ||
|
||
|
||
LOG_FORMAT = "%(asctime)s %(levelname)s %(process)x:%(thread)x %(name)s:%(funcName)s:%(lineno)d %(message)s" | ||
|
||
|
||
def setup_debug_logs(debug: bool) -> None: | ||
"""Configure Python logger. Disable messages up to logging.ERROR level by default. | ||
The reason we disable error messages is that we call logging.error() in addition to | ||
showing user-friendly error messages, but we don't want the error logs to show up | ||
with the user-friendly error messages, unless --debug has been set. | ||
""" | ||
level = logging.DEBUG if debug else logging.CRITICAL | ||
|
||
if sys.version_info[:2] < (3, 8): | ||
# Simulate logging.basicConfig() `force` argument, introduced in Python 3.8 | ||
root = logging.getLogger() | ||
for handler in root.handlers[:]: | ||
root.removeHandler(handler) | ||
handler.close() | ||
logging.basicConfig(filename=None, level=level, format=LOG_FORMAT) | ||
else: | ||
logging.basicConfig(filename=None, level=level, format=LOG_FORMAT, force=True) | ||
|
||
if debug: | ||
# Silence charset_normalizer, its debug output does not bring much | ||
logging.getLogger("charset_normalizer").setLevel(logging.WARNING) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
from typing import Any | ||
|
||
import click | ||
|
||
from ggshield.cmd.common_options import add_common_options | ||
from ggshield.cmd.iac.scan import scan_cmd | ||
|
||
|
||
@click.group(commands={"scan": scan_cmd}) | ||
def iac_group() -> None: | ||
@add_common_options() | ||
def iac_group(**kwargs: Any) -> None: | ||
"""Commands to work with infrastructure as code.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.