Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow store_true action with environment variables #187

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions spectacles/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ class EnvVarAction(argparse.Action):

def __init__(self, env_var, required=False, default=None, **kwargs):
self.env_var = env_var
self.in_env = False
if env_var in os.environ:
default = os.environ[env_var]
self.in_env = True
if required and default:
required = False
super().__init__(default=default, required=required, **kwargs)
Expand All @@ -105,6 +103,24 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, values)


class EnvVarStoreTrueAction(argparse._StoreTrueAction):
def __init__(self, env_var, required=False, default=False, **kwargs):
self.env_var = env_var
if env_var in os.environ:
value = os.environ[env_var].lower()
if value not in ("true", "false"):
raise SpectaclesException(
f"Allowed values for {env_var} are 'true' or 'false' (case-insensitive), received '{value}'"
)
default = True if value == "true" else False
if required and default:
required = False
super().__init__(default=default, required=required, **kwargs)

def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, True)


def handle_exceptions(function: Callable) -> Callable:
"""Wrapper for handling custom exceptions by logging them.

Expand Down Expand Up @@ -382,11 +398,11 @@ def _build_sql_subparser(
)
subparser.add_argument(
"--import-projects",
action=EnvVarAction,
env_var="IMPORT_PROJECTS",
action=EnvVarStoreTrueAction,
env_var="SPECTACLES_IMPORT_PROJECTS",
help="When set to true, the SQL Validator will create temporary branches \
that are clones of master for any project that is a local dependency of the \
of the project being tested. These branches are deleted at the end of the run",
of the project being tested. These branches are deleted at the end of the run.",
)
subparser.add_argument(
"--concurrency",
Expand Down