chaostoolkit / chaostoolkit

Chaos Engineering Toolkit & Orchestration for Developers

Home Page:https://chaostoolkit.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

settings command fails ungracefully when file does not exist

cdsre opened this issue · comments

Describe the bug
When running the chaos settings command either with the default config or passing a settings file an unhandled exception occurs.

 chaos --settings foo.json settings show --format json

Traceback (most recent call last):
  File "C:\Users\doyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\doyle\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\Scripts\chaos.exe\__main__.py", line 7, in <module>
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\ctk\lib\site-packages\click\decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "D:\jetbrains\PycharmProjects\chaostoolkit\chaostoolkit\commands\settings.py", line 39, in show_settings
    click.abort(
AttributeError: module 'click' has no attribute 'abort'. Did you mean: 'Abort'?

Runtime versions

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> python --version
Python 3.10.4
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos info core
NAME                VERSION
CLI                 1.19.1.dev1+g10f7d4c.d20240303.editable
Core library        1.42.0
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos info extensions
NAME                                    VERSION   LICENSE                       DESCRIPTION

To Reproduce
Just run the command when you do not have the default ~/.chaostoolkit/settings.yaml or if that's set run the command specifically passing a settings file that doesn't exist

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos --settings foo.json settings show --format json
...
...
  File "D:\jetbrains\PycharmProjects\chaostoolkit\chaostoolkit\commands\settings.py", line 39, in show_settings
    click.abort(
AttributeError: module 'click' has no attribute 'abort'. Did you mean: 'Abort'?

Expected behavior
The command should fail gracefully as the code intended.

@click.pass_context
def show_settings(ctx: click.Context, fmt: str = "json"):
    """
    Show the entire content of the settings file.

    Be aware this will not obfuscate secret data.
    """
    if not os.path.isfile(ctx.obj["settings_path"]):
        click.abort(
            "No settings file found at {}".format(ctx.obj["settings_path"])
        )

    settings = load_settings(ctx.obj["settings_path"]) or {}
    if fmt == "json":
        click.echo(json.dumps(settings, indent=2))
    elif fmt == "yaml":
        click.echo(yaml.dump(settings, indent=2))

Additional context
The issue here is that click.abort is not exported via the __init__.py of the click package. It is actually click.Abort however I dont believe that will have the desired affect in this case since that's just the exception type. Instead i would suggest to use ctx.fail

    if not os.path.isfile(ctx.obj["settings_path"]):
        ctx.fail(
            "No settings file found at {}".format(ctx.obj["settings_path"])
        )

This will then give the right experience both with the default or a custom file

(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos settings show --format json
Usage: chaos settings show [OPTIONS]
Try 'chaos settings show --help' for help.

Error: No settings file found at C:\Users\doyle\.chaostoolkit\settings.yaml
(ctk) PS D:\jetbrains\PycharmProjects\chaostoolkit> chaos --settings my/settings/files/foo.json settings show --format json
Usage: chaos settings show [OPTIONS]
Try 'chaos settings show --help' for help.

Error: No settings file found at my/settings/files/foo.json