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

Fix CreateFromEnvVars to allow '=' in env var value. #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void CreateFromEnvFile_WithAllSettings()
Assert.False(cs.CleanSession);
Assert.Equal(32, cs.KeepAliveInSeconds);
Assert.Equal("sample_client", cs.ClientId);
Assert.Equal("sample_user", cs.Username);
Assert.Equal("sample_user/api-version=2024-01-01", cs.Username);
Assert.Equal("foo", cs.Password);
Assert.Equal("ca.pem", cs.CaFile);
Assert.Equal("cert.pem", cs.CertFile);
Expand Down Expand Up @@ -132,13 +132,19 @@ private void RemoveTestEnvVars(string envFile)
{
foreach (var line in File.ReadAllLines(envFile))
{
var parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
int index = line.IndexOf('=');
if (index < 0)
{
continue;
}

Environment.SetEnvironmentVariable(parts[0], null);
string key = line[..index].Trim();
if (string.IsNullOrEmpty(key))
{
continue;
}

Environment.SetEnvironmentVariable(key, null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MQTT_USE_TLS=false
MQTT_CLEAN_SESSION=false
MQTT_KEEP_ALIVE_IN_SECONDS=32
MQTT_CLIENT_ID=sample_client
MQTT_USERNAME=sample_user
MQTT_USERNAME=sample_user/api-version=2024-01-01
MQTT_PASSWORD=foo
MQTT_CA_FILE=ca.pem
MQTT_CERT_FILE=cert.pem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,20 @@ public static MqttConnectionSettings CreateFromEnvVars(string? envFile = "")
Trace.TraceInformation("Loading environment variables from {envFile}" + new FileInfo(envFile).FullName);
foreach (string line in File.ReadAllLines(envFile))
{
string[] parts = line.Split('=', StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
int index = line.IndexOf('=');
if (index < 0)
{
continue;
}

Environment.SetEnvironmentVariable(parts[0], parts[1]);
string key = line[..index].Trim();
string value = line[(index + 1)..].Trim();
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(key))
{
continue;
}

Environment.SetEnvironmentVariable(key, value);
}
}
else
Expand Down
Loading