Skip to content

Commit 50135d9

Browse files
authored
Merge pull request #1589 from dbcli/RW/accept-literal-dsns-along-with-aliases
Liberally accept literal DSN arguments to `--dsn`
2 parents 4a79274 + 1d7c207 commit 50135d9

3 files changed

Lines changed: 45 additions & 17 deletions

File tree

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Upcoming (TBD)
22
==============
33

4+
Features
5+
---------
6+
* Let the `--dsn` argument accept literal DSNs as well as aliases.
7+
8+
49
Bug Fixes
510
---------
611
* Make `--ssl-capath` argument a directory.

mycli/main.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,9 +1679,11 @@ def get_last_query(self) -> str | None:
16791679
)
16801680
@click.version_option(__version__, "-V", "--version", help="Output mycli's version.")
16811681
@click.option("-v", "--verbose", is_flag=True, help="Verbose output.")
1682-
@click.option("-D", "--database", "dbname", help="Database to use.")
1683-
@click.option("-d", "--dsn", default="", envvar="DSN", help="Use DSN configured into the [alias_dsn] section of myclirc file.")
1684-
@click.option("--list-dsn", "list_dsn", is_flag=True, help="list of DSN configured into the [alias_dsn] section of myclirc file.")
1682+
@click.option("-D", "--database", "dbname", help="Database or DSN to use for the connection.")
1683+
@click.option("-d", "--dsn", 'dsn_alias', default="", envvar="DSN", help="DSN alias configured in the ~/.myclirc file, or a full DSN.")
1684+
@click.option(
1685+
"--list-dsn", "list_dsn", is_flag=True, help="list of DSN aliases configured in the [alias_dsn] section of the ~/.myclirc file."
1686+
)
16851687
@click.option("--list-ssh-config", "list_ssh_config", is_flag=True, help="list ssh configurations in the ssh config (requires paramiko).")
16861688
@click.option("--ssh-warning-off", is_flag=True, help="Suppress the SSH deprecation notice.")
16871689
@click.option("-R", "--prompt", "prompt", help=f'Prompt format (Default: "{MyCli.default_prompt}").')
@@ -1762,7 +1764,7 @@ def cli(
17621764
warn: bool | None,
17631765
execute: str | None,
17641766
myclirc: str,
1765-
dsn: str,
1767+
dsn_alias: str,
17661768
list_dsn: str | None,
17671769
ssh_user: str | None,
17681770
ssh_host: str | None,
@@ -1928,23 +1930,27 @@ def get_password_from_file(password_file: str | None) -> str | None:
19281930
and not any([user, password, host, port, login_path])
19291931
and database in mycli.config.get("alias_dsn", {})
19301932
):
1931-
dsn, database = database, ""
1933+
dsn_alias, database = database, ""
19321934

19331935
if database and "://" in database:
19341936
dsn_uri, database = database, ""
19351937

1936-
if dsn:
1938+
if dsn_alias:
19371939
try:
1938-
dsn_uri = mycli.config["alias_dsn"][dsn]
1940+
dsn_uri = mycli.config["alias_dsn"][dsn_alias]
19391941
except KeyError:
1940-
click.secho(
1941-
"Could not find the specified DSN in the config file. Please check the \"[alias_dsn]\" section in your myclirc.",
1942-
err=True,
1943-
fg="red",
1944-
)
1945-
sys.exit(1)
1942+
is_valid_scheme, scheme = is_valid_connection_scheme(dsn_alias)
1943+
if is_valid_scheme:
1944+
dsn_uri = dsn_alias
1945+
else:
1946+
click.secho(
1947+
"Could not find the specified DSN in the config file. Please check the \"[alias_dsn]\" section in your myclirc.",
1948+
err=True,
1949+
fg="red",
1950+
)
1951+
sys.exit(1)
19461952
else:
1947-
mycli.dsn_alias = dsn
1953+
mycli.dsn_alias = dsn_alias
19481954

19491955
if dsn_uri:
19501956
uri = urlparse(dsn_uri)
@@ -2039,10 +2045,10 @@ def get_password_from_file(password_file: str | None) -> str | None:
20392045
elif val:
20402046
init_cmds.append(val)
20412047
# 2) DSN-specific init-commands
2042-
if dsn:
2048+
if dsn_alias:
20432049
alias_section = mycli.config.get("alias_dsn.init-commands", {})
2044-
if dsn in alias_section:
2045-
val = alias_section.get(dsn)
2050+
if dsn_alias in alias_section:
2051+
val = alias_section.get(dsn_alias)
20462052
if isinstance(val, (list, tuple)):
20472053
init_cmds.extend(val)
20482054
elif val:

test/test_main.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,23 @@ def run_query(self, query, new_line=True):
912912
and MockMyCli.connect_args["ssl"]["enable"] is True
913913
)
914914

915+
# Accept a literal DSN with the --dsn flag (not only an alias)
916+
result = runner.invoke(
917+
mycli.main.cli,
918+
args=[
919+
'--dsn',
920+
'mysql://dsn_user:dsn_passwd@dsn_host:6/dsn_database',
921+
],
922+
)
923+
assert result.exit_code == 0, result.output + ' ' + str(result.exception)
924+
assert (
925+
MockMyCli.connect_args['user'] == 'dsn_user'
926+
and MockMyCli.connect_args['passwd'] == 'dsn_passwd'
927+
and MockMyCli.connect_args['host'] == 'dsn_host'
928+
and MockMyCli.connect_args['port'] == 6
929+
and MockMyCli.connect_args['database'] == 'dsn_database'
930+
)
931+
915932

916933
def test_ssh_config(monkeypatch):
917934
# Setup classes to mock mycli.main.MyCli

0 commit comments

Comments
 (0)