Added environment variable expansion for tunnel Host field in config#38
Added environment variable expansion for tunnel Host field in config#38nickman wants to merge 5 commits into
Conversation
|
I should add... |
|
Hi @nickman, thanks for your contribution, I think it's a good idea! However, before implementing/merging anything, let's discuss a bit what the scope should be. Few questions arise:
Wdyt? |
|
You make excellent points.
For item #1, I will think on this. It's working nicely for me, but I see there could be issues there. |
|
Great points, but actually I think 1. is fine. Reconnection is mostly for recovering from transient network failures, in which case we wouldn't want a silent switch of (e.g.) host anyway. For 2. that makes sense but I think in this case it's ok for consistency reasons, and actually useful. For 3. maybe we can start without default syntax to keep it simple, we can still iterate on top of it. Let me know if you have any questions and thanks! |
|
Done. |
|
Thank you! I should get to it soon. |
There was a problem hiding this comment.
Pull request overview
Adds environment-variable expansion support for selected tunnel configuration fields during config load, including a ${VAR:-default} fallback syntax, and introduces fixture-based unit tests for the behavior.
Changes:
- Expand env vars in tunnel
host,user,identity,local, andremotefields viaos.Expandwith custom${VAR:-default}handling. - Add
expandWithDefault()to support default values when env vars are unset/empty. - Add config fixtures and unit tests validating expansion, unset behavior, and default fallbacks (including an explicit non-expansion test for
name/group).
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
internal/config/config.go |
Performs env var expansion during Load() and adds ${VAR:-default} mapping helper. |
internal/config/config_test.go |
Adds fixture loader and tests covering env var expansion and default handling. |
test/testdata/config/expand/vars.toml |
Fixture for multi-field expansion. |
test/testdata/config/expand/unset.toml |
Fixture for unset-var expansion to empty string. |
test/testdata/config/expand/literal_fields.toml |
Fixture asserting name/group remain literal (no expansion). |
test/testdata/config/expand/defaults.toml |
Fixture for ${VAR:-fallback} behavior across set/empty/unset vars. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Expand environment variables in tunnel fields
Summary
Adds support for environment variable references (
${VAR_NAME}) in tunnel configuration fields, including default values via${VAR:-default}syntax. This allows users to dynamically configure tunnels without hardcoding values in the config file.Supported Fields
Environment variable expansion is now supported in the following tunnel fields:
Motivation
Users may need to reference hostnames or connection details that vary across environments (e.g., dev vs. prod, CI pipelines, or shared configs among team members). Supporting env var expansion in the supported fields enables more flexible and portable configurations.
Examples
Basic Expansion:
With default values:
If
DB_HOSTis set toprod-db.internal.example.com, the tunnel connects to that host.If
DB_HOSTis unset or empty, the tunnel falls back to localhost.Changes
internal/config/config.go— AddedexpandWithDefault()function that supports${VAR:-default}syntaxAfter loading the config and setting keep-alive defaults, expand environment variable references in all supported tunnel fields using
os.Expandwith the custom mapping functioninternal/config/config_test.go(new) — Unit tests covering:TestEnvVarExpansionInHost— Single and multiple env var references in host fieldTestEnvVarExpansionUnsetVar— Unset env vars resolve to empty stringTestEnvVarExpansionWithDefault— Default values via${VAR:-default}syntaxTestEnvVarExpansionAllFields— Env var expansion in all supported fieldsTestEnvVarExpansionAllFieldsWithDefaults— Default values for all fieldsTestEnvVarExpansionMixedFields— Mix of set vars, defaults, and empty valuesBehavior
os.Expandwhich supports${VAR}and$VARsyntax${VAR:-default}— if the variable is unset or empty, the default value is used${VAR:-}) is valid and results in empty string when var is unset