namespace Spark.Api.Server { using Bts.PubSub; using CommandLine; using System; using System.IO; using System.Text.Json; using System.Text.Json.Serialization; [Verb("run")] public class RunOptions { [Option("user", Default = CommonOptionMetadata.DEFAULT_USER)] public string? user { get; set; } [Option("password", Default = CommonOptionMetadata.DEFAULT_PASSWORD)] public string? password { get; set; } [Option("port", Default = "8080")] public string? port { get; set; } [Option("host", Default = "localhost")] public string? host { get; set; } [Option( "sparkd", MetaValue = CommonOptionMetadata.SPARKD_META_VALUE, HelpText = CommonOptionMetadata.SPARKD_HELP_TEXT)] public string? sparkd { get; set; } [JsonPropertyName("pubsub")] public PubSubConfig? PubSub { get; set; } [JsonIgnore] [Option( "config", MetaValue = "PATH", HelpText = "Path to json file representing these options (RunOptions)")] public string? ConfigFile { get; set; } public static RunOptions Default() => new() { PubSub = PubSubConfig.Default(), }; public void MergeWith(RunOptions other) { sparkd ??= other.sparkd; 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; } } }