Skip to content
Merged
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
@@ -1,4 +1,3 @@
#nullable disable
namespace Microsoft.ComponentDetection.Common;

using System;
Expand All @@ -13,14 +12,19 @@ public bool DoesEnvironmentVariableExist(string name)
return this.GetEnvironmentVariable(name) != null;
}

public string GetEnvironmentVariable(string name)
public string? GetEnvironmentVariable(string name)
{
// Environment variables are case-insensitive on Windows, and case-sensitive on
// Linux and MacOS.
// https://docs.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable
if (OperatingSystem.IsWindows())
{
return Environment.GetEnvironmentVariable(name);
}
Comment thread
JamieMagee marked this conversation as resolved.

var caseInsensitiveName = Environment.GetEnvironmentVariables().Keys
.OfType<string>()
.FirstOrDefault(x => string.Compare(x, name, true) == 0);
.FirstOrDefault(x => string.Equals(x, name, StringComparison.OrdinalIgnoreCase));

return caseInsensitiveName != null ? Environment.GetEnvironmentVariable(caseInsensitiveName) : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IEnvironmentVariableService
/// </summary>
/// <param name="name">Name of the environment variable.</param>
/// <returns>Returns a string of the environment variable value.</returns>
Comment thread
JamieMagee marked this conversation as resolved.
string GetEnvironmentVariable(string name);
string? GetEnvironmentVariable(string name);

/// <summary>
/// Returns the value of an environment variable which is formatted as a delimited list.
Expand Down
Loading