-
Notifications
You must be signed in to change notification settings - Fork 25
feat: add unix socket discovery and connection support #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6e4caa5
feat: add unix socket discovery and connection support
XYenon ecdc571
fix(discovery): address unix socket review feedback
XYenon 09d6ff4
refactor(discovery): simplify unix socket changes
XYenon e363a66
fix(discovery): use current module imports
XYenon 5a65edf
fix(discovery): use stable keys for dedupe
XYenon 9fdd613
fix(discovery): prefer credential sources when deduping
XYenon 3bc50a7
fix(discovery): align priority and endpoint formatting
XYenon 664d95e
refactor(discovery): simplify port and config helpers
XYenon 6dfbb2f
fix(discovery): avoid leaking env name and persisting env/pgpass pass…
XYenon d6e649d
fix(discovery): trim whitespace from libpq env vars
XYenon 3186ab7
fix(discovery): harden socket test accept and omit pgpass password
XYenon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package discovery | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/pgplex/pgtui/internal/models" | ||
| ) | ||
|
|
||
| // BuildConnectionConfig turns a discovered instance into a connection config. | ||
| // The returned config intentionally omits the password for environment and | ||
| // .pgpass sources: libpq (pgx) reads PGPASSWORD and ~/.pgpass itself, and | ||
| // leaving the password empty prevents it from being persisted to the keyring | ||
| // (those secrets already have their own source). | ||
| func BuildConnectionConfig(instance models.DiscoveredInstance) models.ConnectionConfig { | ||
| switch instance.Source { | ||
| case models.SourceEnvironment: | ||
| if envConfig := GetEnvironmentConfig(); envConfig != nil { | ||
| config := *envConfig | ||
| config.Name = "" // avoid leaking the generic "Environment" label into connection ID/history | ||
| config.Password = "" | ||
| return config | ||
| } | ||
| case models.SourcePgPass: | ||
| if pgpassConfig := buildPgPassConfig(instance.Host, instance.Port); pgpassConfig != nil { | ||
| return *pgpassConfig | ||
| } | ||
| } | ||
|
|
||
| return buildDefaultConfig(instance) | ||
| } | ||
|
|
||
| // buildPgPassConfig maps a .pgpass entry to connection fields. Password is left | ||
| // empty: libpq reads ~/.pgpass itself, and omitting it here keeps secrets out of | ||
| // the keyring (same rationale as BuildConnectionConfig for env/.pgpass). | ||
| func buildPgPassConfig(host string, port int) *models.ConnectionConfig { | ||
| entries, err := ParsePgPass() | ||
| if err != nil { | ||
| return nil | ||
| } | ||
|
|
||
| for _, entry := range entries { | ||
| if entry.Host != host || entry.Port != port { | ||
| continue | ||
| } | ||
|
|
||
| user := entry.User | ||
| if user == "" || user == "*" { | ||
| user = defaultUser() | ||
| } | ||
|
|
||
| database := entry.Database | ||
| if database == "" || database == "*" { | ||
| database = user | ||
| } | ||
|
|
||
| return &models.ConnectionConfig{ | ||
| Host: host, | ||
| Port: port, | ||
| Database: database, | ||
| User: user, | ||
| SSLMode: "prefer", | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func buildDefaultConfig(instance models.DiscoveredInstance) models.ConnectionConfig { | ||
| return models.ConnectionConfig{ | ||
| Host: instance.Host, | ||
| Port: instance.Port, | ||
| Database: "postgres", | ||
| User: defaultUser(), | ||
| SSLMode: "prefer", | ||
| } | ||
| } | ||
|
|
||
| func defaultUser() string { | ||
| for _, key := range []string{"PGUSER", "USER", "USERNAME"} { | ||
| if value := strings.TrimSpace(os.Getenv(key)); value != "" { | ||
| return value | ||
| } | ||
| } | ||
|
|
||
| return "postgres" | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.