mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:44:31 +01:00
Support setting WireMockServerSettings via Environment (#954)
* Support parsing environment variables (WireMockServerSettings__) * case ignore * fix * SimpleSettingsParserTests * . * int * more test
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -24,7 +25,7 @@ public static class StandAloneApp
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start(WireMockServerSettings settings)
|
||||
{
|
||||
Guard.NotNull(settings, nameof(settings));
|
||||
Guard.NotNull(settings);
|
||||
|
||||
var server = WireMockServer.Start(settings);
|
||||
|
||||
@@ -42,7 +43,7 @@ public static class StandAloneApp
|
||||
[PublicAPI]
|
||||
public static WireMockServer Start(string[] args, IWireMockLogger? logger = null)
|
||||
{
|
||||
Guard.NotNull(args, nameof(args));
|
||||
Guard.NotNull(args);
|
||||
|
||||
if (TryStart(args, out var server, logger))
|
||||
{
|
||||
@@ -61,9 +62,9 @@ public static class StandAloneApp
|
||||
[PublicAPI]
|
||||
public static bool TryStart(string[] args, [NotNullWhen(true)] out WireMockServer? server, IWireMockLogger? logger = null)
|
||||
{
|
||||
Guard.NotNull(args, nameof(args));
|
||||
Guard.NotNull(args);
|
||||
|
||||
if (WireMockServerSettingsParser.TryParseArguments(args, out var settings, logger))
|
||||
if (WireMockServerSettingsParser.TryParseArguments(args, Environment.GetEnvironmentVariables(), out var settings, logger))
|
||||
{
|
||||
settings.Logger?.Info("Version [{0}]", Version);
|
||||
settings.Logger?.Debug("Server arguments [{0}]", string.Join(", ", args.Select(a => $"'{a}'")));
|
||||
|
||||
29
src/WireMock.Net/Extensions/DictionaryExtensions.cs
Normal file
29
src/WireMock.Net/Extensions/DictionaryExtensions.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Stef.Validation;
|
||||
|
||||
namespace WireMock.Extensions;
|
||||
|
||||
internal static class DictionaryExtensions
|
||||
{
|
||||
public static bool TryGetStringValue(this IDictionary dictionary, string key, [NotNullWhen(true)] out string? value)
|
||||
{
|
||||
Guard.NotNull(dictionary);
|
||||
|
||||
if (dictionary[key] is string valueIsString)
|
||||
{
|
||||
value = valueIsString;
|
||||
return true;
|
||||
}
|
||||
|
||||
var valueToString = dictionary[key]?.ToString();
|
||||
if (valueToString != null)
|
||||
{
|
||||
value = valueToString;
|
||||
return true;
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,21 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using WireMock.Extensions;
|
||||
|
||||
namespace WireMock.Settings;
|
||||
|
||||
// Based on http://blog.gauffin.org/2014/12/simple-command-line-parser/
|
||||
internal class SimpleCommandLineParser
|
||||
internal class SimpleSettingsParser
|
||||
{
|
||||
private const string Sigil = "--";
|
||||
private const string Prefix = $"{nameof(WireMockServerSettings)}__";
|
||||
private static readonly int PrefixLength = Prefix.Length;
|
||||
|
||||
private IDictionary<string, string[]> Arguments { get; } = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public void Parse(string[] arguments)
|
||||
public void Parse(string[] arguments, IDictionary? environment = null)
|
||||
{
|
||||
string currentName = string.Empty;
|
||||
|
||||
@@ -44,6 +48,18 @@ internal class SimpleCommandLineParser
|
||||
{
|
||||
Arguments[currentName] = values.ToArray();
|
||||
}
|
||||
|
||||
// Now also parse environment
|
||||
if (environment != null)
|
||||
{
|
||||
foreach (string key in environment.Keys)
|
||||
{
|
||||
if (key.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase) && environment.TryGetStringValue(key, out var value))
|
||||
{
|
||||
Arguments[key.Substring(PrefixLength)] = value.Split(' ').ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(string name)
|
||||
@@ -85,7 +101,16 @@ internal class SimpleCommandLineParser
|
||||
return Contains(name);
|
||||
}
|
||||
|
||||
public int? GetIntValue(string name, int? defaultValue = null)
|
||||
public int? GetIntValue(string name)
|
||||
{
|
||||
return GetValue<int?>(name, values =>
|
||||
{
|
||||
var value = values.FirstOrDefault();
|
||||
return !string.IsNullOrEmpty(value) ? int.Parse(value) : null;
|
||||
}, null);
|
||||
}
|
||||
|
||||
public int GetIntValue(string name, int defaultValue)
|
||||
{
|
||||
return GetValue(name, values =>
|
||||
{
|
||||
@@ -22,6 +22,8 @@ namespace WireMock.Settings;
|
||||
/// </summary>
|
||||
public class WireMockServerSettings
|
||||
{
|
||||
internal const int DefaultStartTimeout = 10000;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the http port.
|
||||
/// </summary>
|
||||
@@ -81,7 +83,7 @@ public class WireMockServerSettings
|
||||
/// StartTimeout
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public int StartTimeout { get; set; } = 10000;
|
||||
public int StartTimeout { get; set; } = DefaultStartTimeout;
|
||||
|
||||
/// <summary>
|
||||
/// Allow Partial Mapping (default set to false).
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
@@ -18,16 +19,16 @@ public static class WireMockServerSettingsParser
|
||||
/// Parse commandline arguments into WireMockServerSettings.
|
||||
/// </summary>
|
||||
/// <param name="args">The commandline arguments</param>
|
||||
/// <param name="environment">The environment settings (optional)</param>
|
||||
/// <param name="logger">The logger (optional, can be null)</param>
|
||||
/// <param name="settings">The parsed settings</param>
|
||||
[PublicAPI]
|
||||
public static bool TryParseArguments(string[] args, [NotNullWhen(true)] out WireMockServerSettings? settings,
|
||||
IWireMockLogger? logger = null)
|
||||
public static bool TryParseArguments(string[] args, IDictionary? environment, [NotNullWhen(true)] out WireMockServerSettings? settings, IWireMockLogger? logger = null)
|
||||
{
|
||||
Guard.HasNoNulls(args);
|
||||
|
||||
var parser = new SimpleCommandLineParser();
|
||||
parser.Parse(args);
|
||||
var parser = new SimpleSettingsParser();
|
||||
parser.Parse(args, environment);
|
||||
|
||||
if (parser.GetBoolSwitchValue("help"))
|
||||
{
|
||||
@@ -55,6 +56,7 @@ public static class WireMockServerSettingsParser
|
||||
RequestLogExpirationDuration = parser.GetIntValue("RequestLogExpirationDuration"),
|
||||
SaveUnmatchedRequests = parser.GetBoolValue(nameof(WireMockServerSettings.SaveUnmatchedRequests)),
|
||||
StartAdminInterface = parser.GetBoolValue("StartAdminInterface", true),
|
||||
StartTimeout = parser.GetIntValue(nameof(WireMockServerSettings.StartTimeout), WireMockServerSettings.DefaultStartTimeout),
|
||||
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails"),
|
||||
UseRegexExtended = parser.GetBoolValue(nameof(WireMockServerSettings.UseRegexExtended), true),
|
||||
WatchStaticMappings = parser.GetBoolValue("WatchStaticMappings"),
|
||||
@@ -79,8 +81,7 @@ public static class WireMockServerSettingsParser
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void ParseLoggerSettings(WireMockServerSettings settings, IWireMockLogger? logger,
|
||||
SimpleCommandLineParser parser)
|
||||
private static void ParseLoggerSettings(WireMockServerSettings settings, IWireMockLogger? logger, SimpleSettingsParser parser)
|
||||
{
|
||||
var loggerType = parser.GetStringValue("WireMockLogger");
|
||||
switch (loggerType)
|
||||
@@ -103,7 +104,7 @@ public static class WireMockServerSettingsParser
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseProxyAndRecordSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||
private static void ParseProxyAndRecordSettings(WireMockServerSettings settings, SimpleSettingsParser parser)
|
||||
{
|
||||
var proxyUrl = parser.GetStringValue("ProxyURL") ?? parser.GetStringValue("ProxyUrl");
|
||||
if (!string.IsNullOrEmpty(proxyUrl))
|
||||
@@ -135,7 +136,7 @@ public static class WireMockServerSettingsParser
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParsePortSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||
private static void ParsePortSettings(WireMockServerSettings settings, SimpleSettingsParser parser)
|
||||
{
|
||||
if (parser.Contains(nameof(WireMockServerSettings.Port)))
|
||||
{
|
||||
@@ -147,7 +148,7 @@ public static class WireMockServerSettingsParser
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseCertificateSettings(WireMockServerSettings settings, SimpleCommandLineParser parser)
|
||||
private static void ParseCertificateSettings(WireMockServerSettings settings, SimpleSettingsParser parser)
|
||||
{
|
||||
var certificateSettings = new WireMockCertificateSettings
|
||||
{
|
||||
@@ -163,7 +164,7 @@ public static class WireMockServerSettingsParser
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseWebProxyAddressSettings(ProxyAndRecordSettings settings, SimpleCommandLineParser parser)
|
||||
private static void ParseWebProxyAddressSettings(ProxyAndRecordSettings settings, SimpleSettingsParser parser)
|
||||
{
|
||||
string? proxyAddress = parser.GetStringValue("WebProxyAddress");
|
||||
if (!string.IsNullOrEmpty(proxyAddress))
|
||||
@@ -177,7 +178,7 @@ public static class WireMockServerSettingsParser
|
||||
}
|
||||
}
|
||||
|
||||
private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleCommandLineParser parser)
|
||||
private static void ParseProxyUrlReplaceSettings(ProxyAndRecordSettings settings, SimpleSettingsParser parser)
|
||||
{
|
||||
var proxyUrlReplaceOldValue = parser.GetStringValue("ProxyUrlReplaceOldValue");
|
||||
var proxyUrlReplaceNewValue = parser.GetStringValue("ProxyUrlReplaceNewValue");
|
||||
|
||||
Reference in New Issue
Block a user