Fix alias_dsn detection for --list-dsn and -D - #1617
Open
ChrisJr404 wants to merge 1 commit into
Open
Conversation
--list-dsn and -D/--dsn read the config with a bare load_config that skips the packaged default, unlike the rest of pgcli. On a fresh install (config not written yet) --list-dsn hit a KeyError on the missing [alias_dsn] section and printed a misleading "Invalid DSNs found" error, and -D could crash on the missing [main] section when the user config only had an [alias_dsn] block. Load config via get_config instead, which writes the default template if needed and always carries the expected sections. Fixes dbcli#1489
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
--list-dsnand-D/--dsnread the config with a bareload_config(pgclirc, config_full_path)that skips the packaged defaultpgclirc, unlike everything else in pgcli which goes throughget_config. That's the root cause of #1489.Two ways it bites:
--list-dsnruns beforePGCli()is constructed, so on a fresh install the config file hasn't been written yet.load_configthen reads a non-existent file,cfg["alias_dsn"]raisesKeyError, and the broadexcept Exceptionswallows it and printsInvalid DSNs found in the config filewith exit 1. No template gets written either, so there's nothing for the user to edit. To someone who did add an alias it just looks like the section is being ignored.-D <alias>has the same bare read plus acfg["main"]access further down (timezone handling). If the user's config only contains an[alias_dsn]block,[main]isn't there and it blows up after resolving the alias.Switching both call sites to
get_config(pgclirc)fixes it:get_configwrites the default template when the file is missing and merges the packaged default, so the expected sections ([main],[alias_dsn]) are always present and aliases resolve the same way they do everywhere else.I couldn't reproduce the original report on a config that already has a populated
[alias_dsn](that path works), but the fresh/minimal-config cases above are reproducible and match the reported symptoms.Tests in
tests/test_alias_dsn.py: fresh config now lists nothing and exits 0 instead of erroring; populated[alias_dsn]is listed;-D <alias>resolves to the right connection;-D <unknown>still errors. The fresh-config and-D-resolve tests fail onmainand pass with this change.Checklist
changelog.rst.AUTHORSfile (or it's already there).pip install pre-commit && pre-commit install).