fix: resolve connectionName from store when no --connection flag

Commands now automatically use the last-used connection from
connections.json instead of requiring --connection or -c flag.
This commit is contained in:
MinaSaad1 2026-03-26 20:17:42 +02:00
parent 8632f901f6
commit 084809c6d6
3 changed files with 30 additions and 8 deletions

View file

@ -10,6 +10,16 @@ from pbi_cli.core.output import format_mcp_result, print_error
from pbi_cli.main import PbiContext
def resolve_connection_name(ctx: PbiContext) -> str | None:
"""Return the connection name from --connection flag or last-used store."""
if ctx.connection:
return ctx.connection
from pbi_cli.core.connection_store import load_connections
store = load_connections()
return store.last_used or None
def run_tool(
ctx: PbiContext,
tool_name: str,
@ -22,8 +32,9 @@ def run_tool(
In REPL mode the shared client is reused and never stopped.
"""
if ctx.connection:
request.setdefault("connectionName", ctx.connection)
conn_name = resolve_connection_name(ctx)
if conn_name:
request.setdefault("connectionName", conn_name)
client = get_client(repl_mode=ctx.repl_mode)
try:

View file

@ -6,6 +6,7 @@ import sys
import click
from pbi_cli.commands._helpers import resolve_connection_name
from pbi_cli.core.mcp_client import get_client
from pbi_cli.core.output import format_mcp_result, print_error
from pbi_cli.main import PbiContext, pass_context
@ -59,8 +60,9 @@ def execute(
"getExecutionMetrics": metrics or metrics_only,
"executionMetricsOnly": metrics_only,
}
if ctx.connection:
request["connectionName"] = ctx.connection
conn_name = resolve_connection_name(ctx)
if conn_name:
request["connectionName"] = conn_name
if max_rows is not None:
request["maxRows"] = max_rows
@ -94,8 +96,9 @@ def validate(ctx: PbiContext, query: str, query_file: str | None, timeout: int)
"query": resolved_query,
"timeoutSeconds": timeout,
}
if ctx.connection:
request["connectionName"] = ctx.connection
conn_name = resolve_connection_name(ctx)
if conn_name:
request["connectionName"] = conn_name
client = get_client()
try:
@ -113,8 +116,9 @@ def validate(ctx: PbiContext, query: str, query_file: str | None, timeout: int)
def clear_cache(ctx: PbiContext) -> None:
"""Clear the DAX query cache."""
request: dict[str, object] = {"operation": "ClearCache"}
if ctx.connection:
request["connectionName"] = ctx.connection
conn_name = resolve_connection_name(ctx)
if conn_name:
request["connectionName"] = conn_name
client = get_client()
try:

View file

@ -50,6 +50,13 @@ def test_run_tool_adds_connection(monkeypatch: pytest.MonkeyPatch) -> None:
def test_run_tool_no_connection(monkeypatch: pytest.MonkeyPatch) -> None:
mock = MockPbiMcpClient()
monkeypatch.setattr("pbi_cli.commands._helpers.get_client", lambda repl_mode=False: mock)
# Ensure no last-used connection is found
from pbi_cli.core.connection_store import ConnectionStore
monkeypatch.setattr(
"pbi_cli.core.connection_store.load_connections",
lambda: ConnectionStore(),
)
ctx = PbiContext(json_output=True)
run_tool(ctx, "measure_operations", {"operation": "List"})