Skip to content
Open
Show file tree
Hide file tree
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
27 changes: 20 additions & 7 deletions app/connectors_service/connectors/connectors_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def load_config(ctx, config):
raise FileNotFoundError(msg)


class LazyConfig:
def __init__(self, ctx, config):
self.ctx = ctx
self.config = config
self.value = None

def __getitem__(self, key):
if self.value is None:
try:
self.value = load_config(self.ctx, self.config)
except FileNotFoundError as e:
click.echo(
f"{e} Run `connectors login` first, or make sure that the config is either present at the default location ({CONFIG_FILE_PATH}) or it's passed via the '-c' or '--config' option."
)
self.ctx.exit(1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.value is None:
msg = f"{CONFIG_FILE_PATH} is empty or invalid. Run `connectors login` to (re)create it."
raise click.ClickException(msg)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's close this one and leave another PR that tries to address the same problem?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one actually fixes it, I tested it. The other one doesn't work.
@artem-shelkovnikov

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to merge the one that addresses the issue - if it's this one then let's merge this one :)

return self.value[key]


# Main group
@click.group(
invoke_without_command=True,
Expand All @@ -60,13 +79,7 @@ def cli(ctx, config):
return

ctx.ensure_object(dict)
try:
ctx.obj["config"] = load_config(ctx, config)
except FileNotFoundError as e:
click.echo(
f"{e} Make sure that the config is either present at the default location ({CONFIG_FILE_PATH}) or it's passed via the '-c' or '--config' option."
)
ctx.exit(1)
ctx.obj["config"] = LazyConfig(ctx, config)


@click.command(help="Authenticate Connectors CLI with an Elasticsearch instance")
Expand Down
42 changes: 41 additions & 1 deletion app/connectors_service/tests/test_connectors_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@


@pytest.fixture(autouse=True)
def mock_cli_config():
def mock_cli_config(request):
if "use_real_cli_config" in request.fixturenames:
yield
return

with patch("connectors.connectors_cli.load_config") as mock:
mock.return_value = {"elasticsearch": {"host": "http://localhost:9211/"}}
yield mock


@pytest.fixture
def use_real_cli_config():
pass


@pytest.fixture(autouse=True)
def mock_connector_es_client():
with patch("connectors.cli.connector.CLIClient") as mock:
Expand Down Expand Up @@ -655,6 +664,37 @@ def test_job_help_page_without_subcommands():
assert "Commands:" in result.output


def test_job_help_page_without_config_file(tmp_path, use_real_cli_config):
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(cli, ["job", "--help"])

assert result.exit_code == 0
assert "Usage:" in result.output
assert "Commands:" in result.output
assert f"{CONFIG_FILE_PATH} was not found" not in result.output


def test_job_subcommand_help_page_without_config_file(tmp_path, use_real_cli_config):
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(cli, ["job", "list", "--help"])

assert result.exit_code == 0
assert "Usage:" in result.output
assert f"{CONFIG_FILE_PATH} was not found" not in result.output


def test_command_without_config_file_suggests_login(tmp_path, use_real_cli_config):
runner = CliRunner()
with runner.isolated_filesystem(temp_dir=tmp_path):
result = runner.invoke(cli, ["job", "list", "test-connector-id"])

assert result.exit_code == 1
assert f"{CONFIG_FILE_PATH} was not found" in result.output
assert "connectors login" in result.output


@patch("click.confirm", MagicMock(return_value=True))
def test_job_cancel():
runner = CliRunner()
Expand Down