namespace Bts.PubSubServer; using Bts.PubSub; using CommandLine; using System; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; public class PubSubServerConfig { /// /// The config server url for sparkd /// [JsonPropertyName("sparkd")] [Option("sparkd", HelpText = "The sparkd config server")] public string? Sparkd { get; set; } [JsonPropertyName("simple_echo_server")] [Option("simple-echo-server", HelpText = "Run a simple echo server (won't connect to sparkd)")] public bool? SimpleEchoServer { get; set; } [JsonPropertyName("pubsub")] public PubSubConfig? PubSub { get; set; } [JsonIgnore] [Option( "config", MetaValue = "PATH", HelpText = "Path to json file representing these options (PubSubServerConfig)")] public string? ConfigFile { get; set; } public static PubSubServerConfig Default() => new() { Sparkd = "http://localhost:8000/config", SimpleEchoServer = false, PubSub = PubSubConfig.Default(), ConfigFile = null, }; public void MergeWith(PubSubServerConfig other) { Sparkd ??= other.Sparkd; SimpleEchoServer ??= other.SimpleEchoServer; PubSub ??= other.PubSub; ConfigFile ??= other.ConfigFile; } /// /// Merges options with those defined in `ConfigFile` (does not overwrite /// non-null existing values). /// /// /// JsonSerializer.Deserialize returns null /// public void MergeWithConfigFile() { if (ConfigFile is null) { return; } var cfgString = File.ReadAllText(ConfigFile); var parsedCfg = JsonSerializer.Deserialize(cfgString) ?? throw new InvalidOperationException($"Failed to parse json from {ConfigFile}"); MergeWith(parsedCfg); ConfigFile = null; } }