GitHub Repository: https://github.com/dday9/System.Environment.Net
Environment variables are key/value pairs that are usually stored in a .env file. A sample file may look like:
These variables are designed to be used during debugging and not in actual production. The class below provides a way to convert environment variables into a strongly typed .NET object.Code:PORT=8800 CONNECTION_STRING=my super cool connection string
Code:
Example:Code:Namespace System Public Class Environment ''' <summary> ''' Parses a file with zero or more environment variables ''' </summary> ''' <param name="path">The location of the .env file</param> ''' <returns><see cref="Dictionary(Of TKey, TValue)"/></returns> ''' <remarks>The method uses <see cref="IO.File.ReadAllLines"/> to read the file, therefore the file extension does necessarily need to be .env.</remarks> Public Shared Function Load(path As String) As Dictionary(Of String, String) If (String.IsNullOrWhiteSpace(path)) Then Throw New ArgumentNullException("path") End If If (Not IO.File.Exists(path)) Then Throw New ArgumentException("The file does not exist.") End If Dim lines = IO.File.ReadAllLines(path) Return Parse(lines) End Function ''' <summary> ''' Parses zero or more environment variables ''' </summary> ''' <param name="variables">A collection of KeyValuePairs in the format: Key=Value</param> ''' <returns><see cref="Dictionary(Of TKey, TValue)"/></returns> Public Shared Function Parse(variables As IEnumerable(Of String)) As Dictionary(Of String, String) Dim parsedVariables = New Dictionary(Of String, String) For Each variable In variables Dim parsedVariable = Parse(variable) If (parsedVariables.ContainsKey(parsedVariable.Key)) Then Throw New Exception($"Multiple variables with the key: {parsedVariable.Key}") End If parsedVariables.Add(parsedVariable.Key, parsedVariable.Value) Next Return parsedVariables End Function ''' <summary> ''' Parses an individual environment variable ''' </summary> ''' <param name="variable">KeyValuePair in the format: Key=Value</param> ''' <returns><see cref="KeyValuePair(Of TKey, TValue)"/></returns> Public Shared Function Parse(variable As String) As KeyValuePair(Of String, String) If (String.IsNullOrWhiteSpace(variable)) Then Throw New ArgumentNullException(NameOf(variable)) End If Dim separator = variable.IndexOf("=") If (separator < 0 OrElse separator = variable.Length - 1) Then Throw New Exception($"The variable is not in a valid format: {variable}") End If Dim key = variable.Substring(0, separator) Dim value = variable.Substring(separator + 1) Return New KeyValuePair(Of String, String)(key, value) End Function End Class End Namespace
Fiddle: Live DemoCode:Imports System Imports System.Collections.Generic Public Module Module1 Public Sub Main() Dim singleVariable = System.Environment.Parse("Key1=Value1") Console.WriteLine("Single Variable") Console.WriteLine("{0}, {1}", singleVariable.Key, singleVariable.Value) Console.WriteLine() Dim multipleValues = System.Environment.Parse({"Key2=Value2", "Key3=Value3", "Key4=Value4"}) Console.WriteLine("Multiple Variables") For Each kvp In multipleValues Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value) Next Console.WriteLine() IO.File.WriteAllLines("test.env", {"Key5=Value5", "Key6=Value6", "Key7=Value7"}) Dim byFile = System.Environment.Load("test.env") Console.WriteLine("Loaded by File") For Each kvp In byFile Console.WriteLine("{0}, {1}", kvp.Key, kvp.Value) Next Console.WriteLine() End Sub End Module


Reply With Quote