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

Rebase and changes to PR 1647 #1813

Merged
merged 2 commits into from Jan 4, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,44 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EventStore.Rags.Tests.EnvironmentTests
{
[TestFixture]
public class when_referenced_environment_variable_is_parsed
{
[Test]
public void should_return_the_referenced_environment_variable()
{
Environment.SetEnvironmentVariable("EVENTSTORE_NAME", "${env:TEST_REFERENCE_VAR}", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TEST_REFERENCE_VAR", "foo", EnvironmentVariableTarget.Process);

var envVariable = Environment.GetEnvironmentVariable("EVENTSTORE_NAME");
var result = EnvironmentVariables.Parse<TestType>(x => NameTranslators.PrefixEnvironmentVariable(x, "EVENTSTORE_"));
Assert.AreEqual(1, result.Count());
Assert.AreEqual("Name", result.First().Name);
Assert.AreEqual(false, result.First().IsTyped);
Assert.AreEqual("foo", result.First().Value.ToString());
Assert.AreEqual(true, result.First().IsReference);
Environment.SetEnvironmentVariable("EVENTSTORE_NAME", null, EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("TEST_REFERENCE_VAR", null, EnvironmentVariableTarget.Process);
}

[Test]
public void should_return_null_if_referenced_environment_variable_does_not_exist()
{
Environment.SetEnvironmentVariable("EVENTSTORE_NAME", "${env:TEST_REFERENCE_VAR}", EnvironmentVariableTarget.Process);
var envVariable = Environment.GetEnvironmentVariable("EVENTSTORE_NAME");
var result = EnvironmentVariables.Parse<TestType>(x => NameTranslators.PrefixEnvironmentVariable(x, "EVENTSTORE_"));
Assert.AreEqual(1, result.Count());
Assert.AreEqual("Name", result.First().Name);
Assert.AreEqual(false, result.First().IsTyped);
Assert.AreEqual(null, result.First().Value);
Assert.AreEqual(true, result.First().IsReference);
Environment.SetEnvironmentVariable("EVENTSTORE_NAME", null, EnvironmentVariableTarget.Process);
}
}
}
24 changes: 18 additions & 6 deletions src/EventStore.Rags/EnvironmentVariables.cs
@@ -1,19 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace EventStore.Rags
{
public static class EnvironmentVariables
{
static readonly Regex _regex = new Regex(@"^\$\{env\:(\w+)\}$");

public static IEnumerable<OptionSource> Parse<TOptions>(Func<string, string> nameTranslator) where TOptions : class
{
return
(from property in typeof (TOptions).GetProperties()
let environmentVariableName = nameTranslator(property.Name)
let environmentVariableValue = Environment.GetEnvironmentVariable(environmentVariableName.ToUpper())
where !String.IsNullOrEmpty(environmentVariableValue)
select OptionSource.String("Environment Variable", property.Name, environmentVariableValue));
(from property in typeof(TOptions).GetProperties()
let environmentVariableName = nameTranslator(property.Name)
let environmentVariableValue = Environment.GetEnvironmentVariable(environmentVariableName.ToUpper())
where !String.IsNullOrEmpty(environmentVariableValue)
select ParseReferenceEnvironmentVariable(
OptionSource.String("Environment Variable", property.Name, environmentVariableValue, _regex.IsMatch(environmentVariableValue)
)));
}

public static OptionSource ParseReferenceEnvironmentVariable(OptionSource source)
{
if (!source.IsReference) return source;
source.Value = Environment.GetEnvironmentVariable(_regex.Match(source.Value.ToString()).Groups[1].Value);
return source;
}
}
}
}
16 changes: 10 additions & 6 deletions src/EventStore.Rags/OptionSource.cs
@@ -1,28 +1,32 @@
namespace EventStore.Rags
using System.Text.RegularExpressions;

namespace EventStore.Rags
{
public struct OptionSource
{
public string Source;
public string Name;
public bool IsTyped;
public bool IsReference;
public object Value;

public static OptionSource Typed(string source, string name, object value)
public static OptionSource Typed(string source, string name, object value, bool isReference = false)
{
return new OptionSource(source, name, true, value);
return new OptionSource(source, name, true, value, isReference);
}

public static OptionSource String(string source, string name, object value)
public static OptionSource String(string source, string name, object value, bool isReference = false)
{
return new OptionSource(source, name, false, value);
return new OptionSource(source, name, false, value, isReference);
}

public OptionSource(string source, string name, bool isTyped, object value)
public OptionSource(string source, string name, bool isTyped, object value, bool isReference = false)
{
Source = source;
Name = name;
IsTyped = isTyped;
Value = value;
IsReference = isReference;
}
}
}