Skip to content

Commit

Permalink
Merge pull request #557 from JrGoodle/config-warning
Browse files Browse the repository at this point in the history
Make invalid config file a warning, unless running config commands
  • Loading branch information
JrGoodle committed May 31, 2020
2 parents e883187 + b234b52 commit e6b49cb
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 102 deletions.
101 changes: 30 additions & 71 deletions src/clowder/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
print_clowder_name,
valid_clowder_yaml_required
)
from clowder.config import Config, ClowderConfigType
from clowder.config import Config

from .util import add_parser_arguments

Expand Down Expand Up @@ -71,24 +71,6 @@ def add_config_get_parser(subparsers: argparse._SubParsersAction) -> None: # noq
config_get_parser = subparsers.add_parser('get', help='Get clowder config options')
config_get_parser.set_defaults(func=config_get_all)

config_get_subparsers = config_get_parser.add_subparsers()

# clowder config get rebase
rebase_parser = config_get_subparsers.add_parser('rebase', help='Get use rebase with herd command')
rebase_parser.set_defaults(func=config_get_rebase)

# clowder config get jobs
jobs_parser = config_get_subparsers.add_parser('jobs', help='Get use jobs commands')
jobs_parser.set_defaults(func=config_get_jobs)

# clowder config get projects
projects_parser = config_get_subparsers.add_parser('projects', help='Get default projects and groups')
projects_parser.set_defaults(func=config_get_projects)

# clowder config get protocol
protocol_parser = config_get_subparsers.add_parser('protocol', help='Get default git protocol')
protocol_parser.set_defaults(func=config_get_protocol)


def add_config_set_parser(subparsers: argparse._SubParsersAction) -> None: # noqa
"""Add clowder config set parser
Expand Down Expand Up @@ -135,9 +117,11 @@ def config_clear_all(args) -> None: # noqa
"""Clowder config clear all command entry point"""

print(' - Clear all config values')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.clear()
config.save()
print()
config.current_clowder_config.print_configuration()


@valid_clowder_yaml_required
Expand All @@ -146,7 +130,7 @@ def config_clear_jobs(args) -> None: # noqa
"""Clowder config clear jobs command entry point"""

print(' - Clear jobs config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.jobs = None
config.save()
print()
Expand All @@ -159,7 +143,7 @@ def config_clear_projects(args) -> None: # noqa
"""Clowder config clear projects command entry point"""

print(' - Clear projects config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.projects = None
config.save()
print()
Expand All @@ -172,7 +156,7 @@ def config_clear_protocol(args) -> None: # noqa
"""Clowder config clear protocol command entry point"""

print(' - Clear protocol config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.protocol = None
config.save()
print()
Expand All @@ -185,7 +169,7 @@ def config_clear_rebase(args) -> None: # noqa
"""Clowder config clear rebase command entry point"""

print(' - Clear rebase config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.rebase = None
config.save()
print()
Expand All @@ -197,59 +181,17 @@ def config_clear_rebase(args) -> None: # noqa
def config_get_all(args) -> None: # noqa
"""Clowder config get all command entry point"""

print(' - Get all config values')
print()
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config(print_newline=False)
config.current_clowder_config.print_configuration()


@valid_clowder_yaml_required
@print_clowder_name
def config_get_jobs(args) -> None: # noqa
"""Clowder config get jobs command entry point"""

print(' - Get jobs config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config.current_clowder_config.print_config_value(ClowderConfigType.JOBS)


@valid_clowder_yaml_required
@print_clowder_name
def config_get_projects(args) -> None: # noqa
"""Clowder config get projects command entry point"""

print(' - Get projects config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config.current_clowder_config.print_config_value(ClowderConfigType.PROJECTS)


@valid_clowder_yaml_required
@print_clowder_name
def config_get_protocol(args) -> None: # noqa
"""Clowder config get protocol command entry point"""

print(' - Get protocol config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config.current_clowder_config.print_config_value(ClowderConfigType.PROTOCOL)


@valid_clowder_yaml_required
@print_clowder_name
def config_get_rebase(args) -> None: # noqa
"""Clowder config get rebase command entry point"""

print(' - Get rebase config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config.current_clowder_config.print_config_value(ClowderConfigType.REBASE)


@valid_clowder_yaml_required
@print_clowder_name
def config_set_jobs(args) -> None: # noqa
"""Clowder config set jobs command entry point"""

print(' - Set jobs config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.jobs = True
config.save()
print()
Expand All @@ -262,7 +204,7 @@ def config_set_projects(args) -> None:
"""Clowder config set projects command entry point"""

print(' - Set projects config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.projects = tuple(args.projects)
config.save()
print()
Expand All @@ -275,7 +217,7 @@ def config_set_protocol(args) -> None:
"""Clowder config set protocol command entry point"""

print(' - Set protocol config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.protocol = args.protocol
config.save()
print()
Expand All @@ -288,8 +230,25 @@ def config_set_rebase(args) -> None: # noqa
"""Clowder config set rebase command entry point"""

print(' - Set rebase config value')
config = Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices)
config = _config()
config.current_clowder_config.rebase = True
config.save()
print()
config.current_clowder_config.print_configuration()


def _config(print_newline: bool = True) -> Config:
"""Get clowder Config instance
:param bool print_newline: Whether to print a newline if an exception is thrown
:return: Clowder Config instance
:rtype: Config
:raise Exception:
"""

try:
return Config(CLOWDER_CONTROLLER.name, CLOWDER_CONTROLLER.project_choices, raise_exceptions=True)
except: # noqa
if print_newline:
print()
raise
4 changes: 3 additions & 1 deletion src/clowder/config/clowder_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def get_yaml(self) -> dict:
def is_config_value_set(self, value: ClowderConfigType) -> None:
"""Determine if config value is set
:param ClowderConfigType value: Clowder config value
:raise ClowderError:
"""

Expand All @@ -131,6 +132,7 @@ def is_config_value_set(self, value: ClowderConfigType) -> None:
def print_config_value(self, value: ClowderConfigType) -> None:
"""Print current configuration
:param ClowderConfigType value: Clowder config value
:raise ClowderError:
"""

Expand Down Expand Up @@ -161,7 +163,7 @@ def print_configuration(self) -> None:
"""Print current configuration"""

cprint('Current config', attrs=['bold'])

print()
if self.is_empty():
print(' - No config values set')
return
Expand Down
56 changes: 39 additions & 17 deletions src/clowder/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,15 @@ class Config(object):
:ivar Optional[Exception] error: Exception from failing to load clowder config yaml file
"""

def __init__(self, current_clowder_name: Optional[str], project_options: Tuple[str, ...]):
"""Config __init__"""
def __init__(self, current_clowder_name: Optional[str],
project_options: Tuple[str, ...], raise_exceptions: bool = False):
"""Config __init__
:param Optional[str] current_clowder_name: Name of currently loaded clowder if present
:param Tuple[str, ...] project_options: Name of current clowder project options
:param bool raise_exceptions: Whether to raise exception when from initializing invalid config file
:raise ClowderError:
"""

self.error: Optional[Exception] = None
self.current_clowder_config: Optional[ClowderConfig] = None
Expand All @@ -53,21 +60,36 @@ def __init__(self, current_clowder_name: Optional[str], project_options: Tuple[s
self.current_clowder_config = current_clowder_config
return

# Config file does exist, try to load
self._load_clowder_config_yaml()

# If current clowder exists, return
if self.current_clowder_config is not None:
return

# If current clowder config doesn't exist, create empty one
self.current_clowder_config = ClowderConfig(current_clowder_name=current_clowder_name)
if not self.clowder_configs:
self.clowder_configs = (self.current_clowder_config,)
else:
configs = list(self.clowder_configs)
configs.append(self.current_clowder_config)
self.clowder_configs = tuple(configs)
try:
# Config file does exist, try to load
self._load_clowder_config_yaml()
except ClowderError as err:
if raise_exceptions:
raise err
LOG_DEBUG('Failed to load clowder config file', err)
self.error = err
print(fmt.warning_invalid_config_file(ENVIRONMENT.clowder_config_yaml))
print()
except Exception as err:
if raise_exceptions:
raise err
LOG_DEBUG('Failed to load clowder config file', err)
self.error = err
print(fmt.warning_invalid_config_file(ENVIRONMENT.clowder_config_yaml))
print()
finally:
# If current clowder exists, return
if self.current_clowder_config is not None:
return

# If current clowder config doesn't exist, create empty one
self.current_clowder_config = ClowderConfig(current_clowder_name=current_clowder_name)
if not self.clowder_configs:
self.clowder_configs = (self.current_clowder_config,)
else:
configs = list(self.clowder_configs)
configs.append(self.current_clowder_config)
self.clowder_configs = tuple(configs)

def process_projects_arg(self, projects: List[str]) -> Tuple[str, ...]:
"""Process project args based on parameters and config
Expand Down
2 changes: 1 addition & 1 deletion src/clowder/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def __init__(self, error_type: ClowderErrorType, messages: Union[str, List[str]]
"""ClowderError __init__
:param ClowderErrorType error_type: Clowder error type
:param Union[str, List[str]] messages: Error message
:param Union[str, List[str]] messages: Error message(s)
:param Optional[Exception] error: Optional error to print
:param Optional[int] exit_code: Custom error code
"""
Expand Down
14 changes: 13 additions & 1 deletion src/clowder/util/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def error_symlink_source_missing(source: Path) -> str:
:rtype: str
"""

source = _yaml_file(source)
source = _yaml_file(str(source))
return f"{ERROR} Symlink source {source} appears to be missing"


Expand Down Expand Up @@ -757,6 +757,18 @@ def warning_clowder_yaml_not_symlink_with_clowder_repo(name: str) -> str:
f"to a file stored in the existing {path_string(Path('.clowder'))} repo"


def warning_invalid_config_file(file_path: Path) -> str:
"""Return warning message for invalid config file
:param Path file_path: Invalid config file path
:return: Formatted invalid config file warning
:rtype: str
"""

file = _yaml_file(str(file_path))
return f"{WARNING} Clowder config file at {file} appears to be invalid"


def _project_name(name: str) -> str:
"""Return formatted string for project name
Expand Down
22 changes: 11 additions & 11 deletions test/scripts/cats/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,7 @@ test_config_projects() {
ls -al $HOME/.config/clowder

begin_command
$COMMAND config get jobs || exit 1
end_command
begin_command
$COMMAND config get projects || exit 1
end_command
begin_command
$COMMAND config get protocol || exit 1
end_command
begin_command
$COMMAND config get rebase || exit 1
$COMMAND config get || exit 1
end_command
begin_command
$COMMAND herd $PARALLEL || exit 1
Expand Down Expand Up @@ -180,7 +171,16 @@ test_invalid_config() {
copy_invalid_config_file "$test"

begin_command
$COMMAND herd $PARALLEL && exit 1
$COMMAND herd $PARALLEL || exit 1
end_command
begin_command
$COMMAND config get && exit 1
end_command
begin_command
$COMMAND config set rebase && exit 1
end_command
begin_command
$COMMAND config clear && exit 1
end_command
done

Expand Down

0 comments on commit e6b49cb

Please sign in to comment.