namespace Bts.DataStreamPublisher; using Bts.PubSub; using CommandLine; using System; using System.Collections.Generic; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; public class DataStreamPublisherConfig { /// /// The login user for sparkd /// [JsonPropertyName("user")] [Option("user", HelpText = "The sparkd login user")] public string? User { get; set; } /// /// The login password for sparkd /// [JsonPropertyName("password")] [Option("pass", HelpText = "The sparkd login password")] public string? Password { get; set; } /// /// The config server url for sparkd /// [JsonPropertyName("sparkd")] [Option("sparkd", HelpText = "The sparkd config server")] public string? Sparkd { get; set; } /// /// The tables to publish on startup. Each key/value represents a table name/query to publish. /// [JsonPropertyName("publishers")] public List? Publishers { get; set; } [JsonPropertyName("simple_echo_client")] [Option("simple-echo-client", HelpText = "Run a simple echo client (won't publish tables or connect to sparkd)")] public bool? SimpleEchoClient { get; set; } [JsonPropertyName("pubsub")] public PubSubConfig? PubSub { get; set; } [Option( "config", MetaValue = "PATH", HelpText = "Path to json file representing these options (DataStreamPublisherConfig)")] [JsonIgnore] public string? ConfigFile { get; set; } public static DataStreamPublisherConfig Default() => new() { User = "bts", Sparkd = "http://localhost:8000/config", SimpleEchoClient = false, PubSub = PubSubConfig.Default(), ConfigFile = null, }; public void MergeWith(DataStreamPublisherConfig other) { User ??= other.User; Password ??= other.Password; Sparkd ??= other.Sparkd; Publishers ??= other.Publishers; PubSub ??= other.PubSub; SimpleEchoClient ??= other.SimpleEchoClient; 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; } }