From 3ae2b286ab3fe97d87e718e33d70698bcb21dc11 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Sun, 15 Apr 2018 09:20:52 +0100 Subject: [PATCH] #296 coming up with ideas for this config merging --- .../ConfigurationBuilderExtensions.cs | 21 +++++++++++++++++++ .../ConfigurationBuilderExtensionsTests.cs | 6 +++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs index 807ba0f31..31bced5e3 100644 --- a/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs +++ b/src/Ocelot/DependencyInjection/ConfigurationBuilderExtensions.cs @@ -5,6 +5,12 @@ namespace Ocelot.DependencyInjection { + using System.IO; + using System.Linq; + using System.Text.RegularExpressions; + using Configuration.File; + using Newtonsoft.Json; + public static class ConfigurationBuilderExtensions { [Obsolete("Please set BaseUrl in ocelot.json GlobalConfiguration.BaseUrl")] @@ -21,6 +27,21 @@ public static IConfigurationBuilder AddOcelotBaseUrl(this IConfigurationBuilder public static IConfigurationBuilder AddOcelot(this IConfigurationBuilder builder) { + const string pattern = "(?i)ocelot(.*).json$"; + + var reg = new Regex(pattern); + + var files = Directory.GetFiles(".") + .Where(path => reg.IsMatch(path)) + .ToList(); + + foreach (var file in files) + { + var lines = File.ReadAllText(file); + var config = JsonConvert.DeserializeObject(lines); + + } + //var load all files with ocelot*.json //merge these files into one //save it as ocelot.json diff --git a/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs index ab20c9410..694a32245 100644 --- a/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs +++ b/test/Ocelot.UnitTests/DependencyInjection/ConfigurationBuilderExtensionsTests.cs @@ -70,13 +70,13 @@ public void should_merge_files() }; var globalJson = JsonConvert.SerializeObject(globalConfig); - //File.WriteAllText("ocelot.global.json", globalJson); + File.WriteAllText("ocelot.global.json", globalJson); var reRouteJson = JsonConvert.SerializeObject(reRoute); - //File.WriteAllText("ocelot.reRoute.json", reRouteJson); + File.WriteAllText("ocelot.reRoute.json", reRouteJson); IConfigurationBuilder builder = new ConfigurationBuilder(); - //builder.AddOcelot(); + builder.AddOcelot(); }