Skip to content
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

Question: Conventions to overwrite nested values with env vars #103

Closed
josecelano opened this issue May 8, 2024 · 3 comments
Closed

Question: Conventions to overwrite nested values with env vars #103

josecelano opened this issue May 8, 2024 · 3 comments

Comments

@josecelano
Copy link

Hello, I'm migrating to Figment in one of the projects I'm contributing to.

I have this toml file:

announce_interval = 120
db_driver = "Sqlite3"
db_path = "./storage/tracker/lib/database/sqlite3.db"
external_ip = "0.0.0.0"
inactive_peer_cleanup_interval = 600
log_level = "info"
max_peer_timeout = 900
min_announce_interval = 120
mode = "public"
on_reverse_proxy = false
persistent_torrent_completed_stat = false
remove_peerless_torrents = true
tracker_usage_statistics = true

[[udp_trackers]]
bind_address = "0.0.0.0:6969"
enabled = true

[[http_trackers]]
bind_address = "0.0.0.0:7070"
enabled = true
ssl_cert_path = ""
ssl_enabled = false
ssl_key_path = ""

[http_api]
bind_address = "127.0.0.1:1212"
enabled = true
ssl_cert_path = ""
ssl_enabled = false
ssl_key_path = ""

[http_api.access_tokens]
admin = "MyAccessToken"

[health_check_api]
bind_address = "127.0.0.1:1313"

And I want to overwrite this value http_api.access_tokens.admin with an env var:

[http_api.access_tokens]
admin = "MyAccessToken"

It seems that I can do it using this env var name: TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN (prefix TORRUST_TRACKER_), but that's not a valid var name in Bash.

Any suggestion about how to solve this problem? I could solve it if there is a way to use a different separator, for example, __ instead of ..

bash --version
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)

By the way, http_api.access_tokens is a dynamic dictionary (HashMap<String, String>) but it looks that's not a problem. I was able to write a test with jail and overwrite the value. My problem is using that name in Bash.

@josecelano
Copy link
Author

export TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN=NewToken
bash: export: `TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN=NewToken': not a valid identifier

@SergioBenitez
Copy link
Owner

Check out the docs for the Env provider.

Any suggestion about how to solve this problem? I could solve it if there is a way to use a different separator, for example, __ instead of ..

Use Env::split("__").

You can also provide structured data as the top level key. APP_VAR={key="value",num=10}

@josecelano
Copy link
Author

Check out the docs for the Env provider.

Any suggestion about how to solve this problem? I could solve it if there is a way to use a different separator, for example, __ instead of ..

Use Env::split("__").

You can also provide structured data as the top level key. APP_VAR={key="value",num=10}

Thank you, @SergioBenitez, for your fast response, and sorry 🙏🏼 I did not see that. I try searching in the docs first but I did not find that. I should have made a bigger effort.

Finally, I'm using a double underscore:

TORRUST_TRACKER__HTTP_API__ACCESS_TOKENS__ADMIN

Thank you for sharing this crate! (and others).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants