From fba59fa24fe8a4b0f8a7a8369bcd8528a27b3d9b Mon Sep 17 00:00:00 2001 From: masesdevelopers <94312179+masesdevelopers@users.noreply.github.com> Date: Sun, 8 Sep 2024 15:33:23 +0200 Subject: [PATCH] Update configuration management --- test/Common/ProgramConfig.cs | 41 ++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/test/Common/ProgramConfig.cs b/test/Common/ProgramConfig.cs index 6492e8c..816f868 100644 --- a/test/Common/ProgramConfig.cs +++ b/test/Common/ProgramConfig.cs @@ -34,6 +34,8 @@ using System.Text.Json; using Java.Lang; using Java.Util.Concurrent; +using System.Collections.Generic; +using System.Reflection; namespace MASES.EntityFrameworkCore.KNet.Test.Common { @@ -104,19 +106,46 @@ public void ApplyOnContext(KafkaDbContext context) public static void LoadConfig(string[] args) { - if (args.Length > 0) + const string FileFormat = "/f:"; + const string PropertyFormat = "/p:"; + + Dictionary properties = new Dictionary(); + var props = typeof(ProgramConfig).GetProperties(); + string file = null; + foreach (var arg in args) + { + if (arg.StartsWith(FileFormat)) + { + file = arg[FileFormat.Length..]; + if (!File.Exists(file)) { throw new FileNotFoundException($"{file} is not a configuration file.", file); } + } + else if (arg.StartsWith(PropertyFormat)) + { + var argVal = arg[FileFormat.Length..]; + var values = argVal.Split('='); + foreach (var prop in props) + { + if (prop.Name == values[0]) + { + properties.Add(prop, values[1]); + } + } + } + else if (File.Exists(arg)) file = arg; + } + + if (!string.IsNullOrWhiteSpace(file)) { - if (!File.Exists(args[0])) { throw new FileNotFoundException($"{args[0]} is not a configuration file.", args[0]); } - Config = JsonSerializer.Deserialize(File.ReadAllText(args[0])); + Config = JsonSerializer.Deserialize(File.ReadAllText(file)); } else Config = new(); - if (args.Length > 1) + foreach (var property in properties) { - Config.BootstrapServers = args[1]; + property.Key.SetValue(Config, property.Value); } - ReportString(JsonSerializer.Serialize(Config, new JsonSerializerOptions() { WriteIndented = true })); + ReportString(JsonSerializer.Serialize(Config, new JsonSerializerOptions() { WriteIndented = true })); if (!KafkaDbContext.EnableKEFCoreTracing) KafkaDbContext.EnableKEFCoreTracing = Config.EnableKEFCoreTracing;