nils-werner / crestic

Configurable Restic Wrapper
https://nils-werner.github.io/crestic/
MIT License
106 stars 10 forks source link

How to set environment variables using commands? #36

Closed danlamanna closed 11 months ago

danlamanna commented 11 months ago

Is it possible to set an environment variable using a command? So instead of:

[@cloud.environ]
B2_ACCOUNT_ID: <MY_APPLICATION_KEY_ID>
B2_ACCOUNT_KEY: <MY_APPLICATION_KEY>

One could use:

[@cloud.environ]
B2_ACCOUNT_ID: <MY_APPLICATION_KEY_ID>
B2_ACCOUNT_KEY: pass backups/b2/account-key
nils-werner commented 11 months ago

Not inside crestic, sorry. But you can set a session wide environment variable that way in your .bashrc.

danlamanna commented 11 months ago

Do you think it's out of scope for the project? Because I have to set these environment variables differently per backup environment, which doesn't seem too unusual.

nils-werner commented 11 months ago

Right now I'm inclined to say its beyond the scope, yes. Deciding whether to take a string literally vs execute it sounds like a major headache to solve...

But: I'm already expanding environment variables in all argument string fragments, but not yet in environment variables. It would make sense to harmonize that behaviour and expand env variables everywhere, in options and [*.environ] sections. I've implemented that behaviour in the envvar-expand branch, you can go have a look if you like it.

You can then interpolate different variables in different settings, like

[preset_a.environ]
B2_ACCOUNT_ID: <MY_APPLICATION_A_KEY_ID>
B2_ACCOUNT_KEY: $$B2_ACCOUNT_KEY_PRESET_A

[preset_b.environ]
B2_ACCOUNT_ID: <MY_APPLICATION_A_KEY_ID>
B2_ACCOUNT_KEY: $$B2_ACCOUNT_KEY_PRESET_B

or whatever structure you prefer. Note the double dollar sign $$ which is needed because of ConfigParser internals.

nils-werner commented 11 months ago

It's now in master and will be in the next release. Thanks for the suggestion!