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

Optionally use Environment Variables for Config #57

Merged
merged 3 commits into from
Oct 8, 2020

Conversation

stevekirks
Copy link
Contributor

@stevekirks stevekirks commented Sep 4, 2020

Hi Nick

I've had a go at implementing Environment Variables for the config. Hopefully it's what you had in mind but you may have a better strategy. Here's some explanations for the pull request;

Example Environment Variable names:
FORWARDER_OUTPUT_RAWPAYLOADLIMITBYTES
FORWARDER_DIAGNOSTICS_INTERNALLOGGINGLEVEL

Its using the Microsoft.Extensions.Configuration.ConfigurationBuilder to load the Environment Variables and the GetValue() method to get each variable the right type.

Its renamed property EncodedApiKey to ApiKey so that the environment variable FORWARDER_OUTPUT_APIKEY would match. (Also updated the check that excludes it in ConfigCommand).

With these changes, the Config command lists the config file with environment variables overwritten, and writes update the config file with any changes as well as any overwritten environment variables. Not sure if that is the expectation.

If you have ideas for improvement I'm all ears. If you think a different way would be better that's ok too.

Addresses #55

@nblumhardt
Copy link
Member

That's great - thanks Steve! Will check this out ASAP 👍

@stevekirks stevekirks changed the title Optionally use Environment Variables for Config Addresses #55 Optionally use Environment Variables for Config Oct 2, 2020
@stevekirks stevekirks changed the title Addresses #55 Optionally use Environment Variables for Config Optionally use Environment Variables for Config Oct 4, 2020
Copy link
Member

@nblumhardt nblumhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! A few minor comments.

I think it's possible the ./seqfwd config -k <key> -v <value> command will now copy the contents of environment variables into the config file, which we might not want.

@@ -45,8 +48,28 @@ public static SeqForwarderConfig ReadOrInit(string filename)
}

var content = File.ReadAllText(filename);
return JsonConvert.DeserializeObject<SeqForwarderConfig>(content, SerializerSettings) ??
throw new ArgumentException("Configuration content is null.");
var seqForwarderConfig = JsonConvert.DeserializeObject<SeqForwarderConfig>(content, SerializerSettings);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combinedConfig, possibly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think throwing when DeserializeObject returns null might be safer here, since it'd indicate either a bug or malformed configuration file, both of which we might want to know about. The later check won't be triggered if any environment variable is set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I thought the config file might be optional but on second thoughts just the env vars are. I've made these two changes now (1be8759)

@stevekirks
Copy link
Contributor Author

For the Config command, I've changed it so it only reads from the config file (env vars aren't loaded). More what the user expects? (6744940)

@nblumhardt
Copy link
Member

Looks great, thanks Steve 👍

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

Successfully merging this pull request may close these issues.

2 participants