Skip to content

Commit f43aacf

Browse files
committed
Expand env vars in DSN aliases
1 parent df74f06 commit f43aacf

6 files changed

Lines changed: 47 additions & 0 deletions

File tree

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Upcoming (TBD)
33

44
Bug Fixes
55
---------
6+
* Expand environment variables in configured DSN aliases.
67
* Keep completion-menu Escape cancellation eager only in Vi mode so Emacs Alt-key bindings keep working while completions are open.
78

89

mycli/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Contributors:
117117
* yurenchen000
118118
* Linuxdazhao
119119
* Puneet Dixit
120+
* n8himmel
120121

121122

122123
Created by:

mycli/cli_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ def run_from_cli_args(cli_args: 'CliArgs', client_factory: ClientFactory) -> Non
152152
)
153153
sys.exit(1)
154154
else:
155+
dsn_uri = os.path.expandvars(dsn_uri)
155156
mycli.dsn_alias = cli_args.dsn
156157

157158
if dsn_uri:

mycli/myclirc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ matching-bracket.other = '#000000 bg:#aacccc'
395395

396396
# Use the -d option to reference a DSN.
397397
# Special characters in passwords and other strings can be escaped with URL encoding.
398+
# Environment variables in the form $VAR or ${VAR} are expanded in alias DSNs.
398399
[alias_dsn]
399400
# example_dsn = mysql://[user[:password]@][host][:port][/dbname]
400401

test/myclirc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ global_limit = set sql_select_limit=9999
398398

399399
# Use the -d option to reference a DSN.
400400
# Special characters in passwords and other strings can be escaped with URL encoding.
401+
# Environment variables in the form $VAR or ${VAR} are expanded in alias DSNs.
401402
[alias_dsn]
402403
# example_dsn = mysql://[user[:password]@][host][:port][/dbname]
403404

test/pytests/test_main.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,48 @@ def run_query(self, query, new_line=True):
982982
and MockMyCli.connect_args["database"] == "alias_dsn_database"
983983
)
984984

985+
monkeypatch.setenv("MYCLI_TEST_DSN_USER", "env_dsn_user")
986+
monkeypatch.setenv("MYCLI_TEST_DSN_PASSWORD", "env_dsn_passwd")
987+
monkeypatch.setenv("MYCLI_TEST_DSN_HOST", "env_dsn_host")
988+
monkeypatch.setenv("MYCLI_TEST_DSN_PORT", "9")
989+
monkeypatch.setenv("MYCLI_TEST_DSN_DATABASE", "env_dsn_database")
990+
MockMyCli.config = {
991+
"main": {},
992+
"alias_dsn": {
993+
"env_test": "mysql://$MYCLI_TEST_DSN_USER:${MYCLI_TEST_DSN_PASSWORD}@$MYCLI_TEST_DSN_HOST:${MYCLI_TEST_DSN_PORT}/${MYCLI_TEST_DSN_DATABASE}"
994+
},
995+
"connection": {
996+
"default_keepalive_ticks": 0,
997+
},
998+
}
999+
MockMyCli.connect_args = None
1000+
1001+
# When a DSN alias uses environment variables, expand those before parsing
1002+
# the URI.
1003+
result = runner.invoke(click_entrypoint, args=["--dsn", "env_test"])
1004+
assert result.exit_code == 0, result.output + " " + str(result.exception)
1005+
assert (
1006+
MockMyCli.connect_args["user"] == "env_dsn_user"
1007+
and MockMyCli.connect_args["passwd"] == "env_dsn_passwd"
1008+
and MockMyCli.connect_args["host"] == "env_dsn_host"
1009+
and MockMyCli.connect_args["port"] == 9
1010+
and MockMyCli.connect_args["database"] == "env_dsn_database"
1011+
)
1012+
1013+
MockMyCli.connect_args = None
1014+
1015+
# The same expansion applies when the alias is supplied as the positional
1016+
# database argument.
1017+
result = runner.invoke(click_entrypoint, args=["env_test"])
1018+
assert result.exit_code == 0, result.output + " " + str(result.exception)
1019+
assert (
1020+
MockMyCli.connect_args["user"] == "env_dsn_user"
1021+
and MockMyCli.connect_args["passwd"] == "env_dsn_passwd"
1022+
and MockMyCli.connect_args["host"] == "env_dsn_host"
1023+
and MockMyCli.connect_args["port"] == 9
1024+
and MockMyCli.connect_args["database"] == "env_dsn_database"
1025+
)
1026+
9851027
MockMyCli.config = {
9861028
"main": {},
9871029
"alias_dsn": {"test": "mysql://alias_dsn_user:alias_dsn_passwd@alias_dsn_host:4/alias_dsn_database"},

0 commit comments

Comments
 (0)