Skip to content

Commit 0779d12

Browse files
authored
Merge pull request #114 from Maxteabag/cli-existing-connectionf
Cli existing connection
2 parents 9c45ef3 + 590fad8 commit 0779d12

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ sqlit --mock=sqlite-demo
136136
### CLI
137137

138138
```bash
139+
sqlit -c "MyConnection"
140+
sqlit --connection "MyConnection"
141+
139142
# Run a query
140143
sqlit query -c "MyConnection" -q "SELECT * FROM Users"
141144

sqlit/cli.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,12 @@ def main() -> int:
392392
action="store_true",
393393
help="Show idle scheduler status in the status bar.",
394394
)
395+
parser.add_argument(
396+
"-c",
397+
"--connection",
398+
metavar="NAME",
399+
help="Connect to a saved connection by name (opens TUI with only this connection)",
400+
)
395401

396402
subparsers = parser.add_subparsers(dest="command", help="Available commands")
397403

@@ -512,21 +518,38 @@ def main() -> int:
512518
return 1
513519
services.apply_mock_profile(mock_profile)
514520

515-
temp_config = None
521+
startup_config = None
522+
exclusive_connection = False
516523
try:
517-
# Check for connection URL first (extracted before argparse)
518-
if connection_url:
519-
temp_config = parse_connection_url(
524+
# Check for saved connection by name first
525+
if args.connection:
526+
saved_connections = services.connection_store.load_all(load_credentials=False)
527+
matching = [c for c in saved_connections if c.name == args.connection]
528+
if not matching:
529+
print(f"Error: Connection '{args.connection}' not found")
530+
print("Available connections:")
531+
for conn in saved_connections:
532+
print(f" - {conn.name}")
533+
return 1
534+
startup_config = matching[0]
535+
exclusive_connection = True
536+
# Check for connection URL (extracted before argparse)
537+
elif connection_url:
538+
startup_config = parse_connection_url(
520539
connection_url,
521540
name=getattr(args, "name", None),
522541
)
523542
else:
524-
temp_config = _build_temp_connection(args)
543+
startup_config = _build_temp_connection(args)
525544
except ValueError as exc:
526545
print(f"Error: {exc}")
527546
return 1
528547

529-
app = SSMSTUI(services=services, startup_connection=temp_config)
548+
app = SSMSTUI(
549+
services=services,
550+
startup_connection=startup_config,
551+
exclusive_connection=exclusive_connection,
552+
)
530553
app.run()
531554
if getattr(app, "_restart_requested", False):
532555
argv = getattr(app, "_restart_argv", None) or app._compute_restart_argv()

sqlit/domains/explorer/ui/tree/builder.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def _add_connection_node(
7575
is_connected: bool,
7676
is_connecting: bool,
7777
spinner: str | None,
78+
skip_folder: bool = False,
7879
) -> Any:
7980
if is_connected:
8081
label = host._format_connection_label(config, "connected")
@@ -83,8 +84,11 @@ def _add_connection_node(
8384
else:
8485
label = host._format_connection_label(config, "idle")
8586

86-
folder_parts = _split_folder_path(getattr(config, "folder_path", ""))
87-
parent = _ensure_connection_folder_path(host, folder_parts)
87+
if skip_folder:
88+
parent = host.object_tree.root
89+
else:
90+
folder_parts = _split_folder_path(getattr(config, "folder_path", ""))
91+
parent = _ensure_connection_folder_path(host, folder_parts)
8892

8993
node = parent.add(label)
9094
node.data = ConnectionNode(config=config)
@@ -206,28 +210,35 @@ def refresh_tree(host: TreeMixinHost) -> None:
206210
connecting_name = connecting_config.name if connecting_config else None
207211
connecting_spinner = host._connect_spinner_frame() if connecting_config else None
208212

209-
# Check for active direct connection or pending startup connection
213+
# Check for active direct connection, exclusive connection, or pending startup connection
210214
direct_config = getattr(host, "_direct_connection_config", None)
211215
startup_config = getattr(host, "_startup_connect_config", None)
216+
exclusive_connection = getattr(host, "_exclusive_connection", False)
212217
direct_active = (
213218
direct_config is not None
214219
and host.current_config is not None
215220
and direct_config.name == host.current_config.name
216221
)
217-
# Also hide saved connections when startup connection is pending (before it's connected)
222+
# Hide saved connections when startup connection is pending (before it's connected)
218223
startup_pending = startup_config is not None and not any(
219224
c.name == startup_config.name for c in host.connections
220225
)
226+
# Exclusive mode: only show the specified connection (even if it's saved)
227+
exclusive_active = exclusive_connection and startup_config is not None
221228
if direct_active and host.current_config is not None:
222229
connections = [host.current_config]
230+
elif exclusive_active:
231+
connections = [startup_config]
223232
elif startup_pending:
224233
connections = [startup_config]
225234
else:
226235
connections = list(host.connections)
227236
if connecting_config and not any(c.name == connecting_config.name for c in connections):
228237
connections = connections + [connecting_config]
229238
connections = _sort_connections_for_display(connections)
230-
_build_connection_folders(host, connections)
239+
# Skip folder structure in exclusive mode
240+
if not exclusive_active:
241+
_build_connection_folders(host, connections)
231242

232243
for conn in connections:
233244
is_connected = host.current_config is not None and conn.name == host.current_config.name
@@ -238,6 +249,7 @@ def refresh_tree(host: TreeMixinHost) -> None:
238249
is_connected=is_connected,
239250
is_connecting=is_connecting,
240251
spinner=connecting_spinner,
252+
skip_folder=exclusive_active,
241253
)
242254

243255
restore_subtree_expansion(host, host.object_tree.root)
@@ -263,28 +275,35 @@ def refresh_tree_chunked(
263275
connecting_name = connecting_config.name if connecting_config else None
264276
connecting_spinner = host._connect_spinner_frame() if connecting_config else None
265277

266-
# Check for active direct connection or pending startup connection
278+
# Check for active direct connection, exclusive connection, or pending startup connection
267279
direct_config = getattr(host, "_direct_connection_config", None)
268280
startup_config = getattr(host, "_startup_connect_config", None)
281+
exclusive_connection = getattr(host, "_exclusive_connection", False)
269282
direct_active = (
270283
direct_config is not None
271284
and host.current_config is not None
272285
and direct_config.name == host.current_config.name
273286
)
274-
# Also hide saved connections when startup connection is pending (before it's connected)
287+
# Hide saved connections when startup connection is pending (before it's connected)
275288
startup_pending = startup_config is not None and not any(
276289
c.name == startup_config.name for c in host.connections
277290
)
291+
# Exclusive mode: only show the specified connection (even if it's saved)
292+
exclusive_active = exclusive_connection and startup_config is not None
278293
if direct_active and host.current_config is not None:
279294
connections = [host.current_config]
295+
elif exclusive_active:
296+
connections = [startup_config]
280297
elif startup_pending:
281298
connections = [startup_config]
282299
else:
283300
connections = list(host.connections)
284301
if connecting_config and not any(c.name == connecting_config.name for c in connections):
285302
connections = connections + [connecting_config]
286303
connections = _sort_connections_for_display(connections)
287-
_build_connection_folders(host, connections)
304+
# Skip folder structure in exclusive mode
305+
if not exclusive_active:
306+
_build_connection_folders(host, connections)
288307

289308
def schedule_populate() -> None:
290309
if getattr(host, "_tree_refresh_token", None) is not token:
@@ -343,6 +362,7 @@ def populate_once() -> None:
343362
is_connected=is_connected,
344363
is_connecting=is_connecting,
345364
spinner=connecting_spinner,
365+
skip_folder=exclusive_active,
346366
)
347367

348368
def finish_sync() -> None:
@@ -369,6 +389,7 @@ def add_batch() -> None:
369389
is_connected=is_connected,
370390
is_connecting=is_connecting,
371391
spinner=connecting_spinner,
392+
skip_folder=exclusive_active,
372393
)
373394
idx = end
374395
if idx < len(connections):

sqlit/domains/shell/app/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ def __init__(
9898
services: AppServices | None = None,
9999
runtime: RuntimeConfig | None = None,
100100
startup_connection: ConnectionConfig | None = None,
101+
exclusive_connection: bool = False,
101102
):
102103
super().__init__()
103104
self.services = services or build_app_services(runtime or RuntimeConfig.from_env())
104105
from sqlit.core.connection_manager import ConnectionManager
105106

106107
self._connection_manager = ConnectionManager(self.services)
107108
self._startup_connection = startup_connection
109+
self._exclusive_connection = exclusive_connection
108110
self._startup_connect_config: ConnectionConfig | None = None
109111
self._debug_mode = self.services.runtime.debug_mode
110112
self._debug_idle_scheduler = self.services.runtime.debug_idle_scheduler

0 commit comments

Comments
 (0)