Skip to content

Commit

Permalink
Enhance environment variable retrieval and defaults
Browse files Browse the repository at this point in the history
- Modify `Helper.cs` to retrieve `EmailAccountPassword` from process-level environment variables and set a default SMTP port of `25`.
- Update `EnvHelper.cs` `Get` method to support a `defaultValue` parameter, returning it if the environment variable is not found.
  • Loading branch information
EdiWang committed Oct 13, 2024
1 parent 8c8a989 commit 43bf2ba
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Moonglade.Function.Email/Core/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public static EmailHelper GetEmailHelper(string functionAppDirectory)

var smtpSettings = new SmtpSettings(EnvHelper.Get<string>("SmtpServer"),
EnvHelper.Get<string>("SmtpUserName"),
EnvHelper.Get<string>("EmailAccountPassword", EnvironmentVariableTarget.Process),
EnvHelper.Get<int>("SmtpServerPort"));
EnvHelper.Get<string>("EmailAccountPassword", target: EnvironmentVariableTarget.Process),
EnvHelper.Get("SmtpServerPort", 25));

if (EnvHelper.Get<bool>("EnableTls"))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Moonglade.Function.Email/EnvHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class EnvHelper
{
public static ICollection AllKeys => Environment.GetEnvironmentVariables().Keys;

public static T Get<T>(string name, EnvironmentVariableTarget? target = null)
public static T Get<T>(string name, T defaultValue = default, EnvironmentVariableTarget? target = null)
{
if (string.IsNullOrEmpty(name))
{
Expand All @@ -17,7 +17,7 @@ public static T Get<T>(string name, EnvironmentVariableTarget? target = null)
string matchedKey = AllKeys.Cast<string>().FirstOrDefault(key => string.Equals(key, name, StringComparison.OrdinalIgnoreCase));
if (matchedKey == null)
{
return default;
return defaultValue;
}

var value = target == null
Expand Down

0 comments on commit 43bf2ba

Please sign in to comment.