From 6e65780279f5b37f3651c12551226dac028bd686 Mon Sep 17 00:00:00 2001 From: Gord Pollock Date: Tue, 6 Jun 2023 16:35:03 -0700 Subject: [PATCH] Read multiple commands from stdin --- .../SdkExamples/PointZilla/Context.cs | 2 ++ .../SdkExamples/PointZilla/PointZilla.csproj | 3 +++ .../SdkExamples/PointZilla/Program.cs | 24 ++++++++++++++++--- .../SdkExamples/PointZilla/packages.config | 1 + 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/TimeSeries/PublicApis/SdkExamples/PointZilla/Context.cs b/TimeSeries/PublicApis/SdkExamples/PointZilla/Context.cs index 89ca81c..2d28956 100644 --- a/TimeSeries/PublicApis/SdkExamples/PointZilla/Context.cs +++ b/TimeSeries/PublicApis/SdkExamples/PointZilla/Context.cs @@ -115,5 +115,7 @@ public class Context public Field NoteStartField { get; set; } = Field.Parse("StartTime", nameof(Context.NoteStartField)); public Field NoteEndField { get; set; } = Field.Parse("EndTime", nameof(Context.NoteEndField)); public Field NoteTextField { get; set; } = Field.Parse("NoteText", nameof(Context.NoteTextField)); + + public bool MultiRunStdin { get; set; } } } diff --git a/TimeSeries/PublicApis/SdkExamples/PointZilla/PointZilla.csproj b/TimeSeries/PublicApis/SdkExamples/PointZilla/PointZilla.csproj index 4f22d29..968fb5a 100644 --- a/TimeSeries/PublicApis/SdkExamples/PointZilla/PointZilla.csproj +++ b/TimeSeries/PublicApis/SdkExamples/PointZilla/PointZilla.csproj @@ -110,6 +110,9 @@ ..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + ..\packages\System.CommandLine.2.0.0-beta4.22272.1\lib\netstandard2.0\System.CommandLine.dll + diff --git a/TimeSeries/PublicApis/SdkExamples/PointZilla/Program.cs b/TimeSeries/PublicApis/SdkExamples/PointZilla/Program.cs index 79582da..0862a5a 100644 --- a/TimeSeries/PublicApis/SdkExamples/PointZilla/Program.cs +++ b/TimeSeries/PublicApis/SdkExamples/PointZilla/Program.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.CommandLine.Parsing; using System.Diagnostics; using System.Globalization; using System.IO; @@ -35,7 +36,22 @@ public static void Main(string[] args) ServiceStackConfig.ConfigureServiceStack(); var context = ParseArgs(args); - new Program(context).Run(); + if (!context.MultiRunStdin) + { + new Program(context).Run(); + } + else + { + var line = 1; + string argLine; + while (!string.IsNullOrWhiteSpace(argLine = Console.ReadLine())) + { + _log.Info($"Started running {line++} '{argLine}'"); + var currentArgs = CommandLineStringSplitter.Instance.Split(argLine); + var currentContext = ParseArgs(currentArgs); + new Program(currentContext).Run(); + } + } Environment.ExitCode = 0; } @@ -102,7 +118,7 @@ private static string GetExecutingFileVersion() return $"{GetCoreType().Namespace} v{fileVersionInfo.FileVersion}"; } - private static Context ParseArgs(string[] args) + private static Context ParseArgs(IEnumerable args) { var context = new Context { @@ -124,6 +140,8 @@ private static Context ParseArgs(string[] args) new Option {Key = nameof(context.AppendTimeout), Setter = value => context.AppendTimeout = TimeSpan.Parse(value), Getter = () => context.AppendTimeout.ToString(), Description = "Timeout period for append completion, in .NET TimeSpan format."}, new Option {Key = nameof(context.BatchSize), Setter = value => context.BatchSize = int.Parse(value), Getter = () => context.BatchSize.ToString(), Description = "Maximum number of points to send in a single append request"}, + new Option {Key = nameof(context.MultiRunStdin), Setter = value => context.MultiRunStdin = bool.Parse(value), Getter = () => context.MultiRunStdin.ToString(), Description = "Run PointZilla many times based on args in specified file"}, + new Option(), new Option {Description = "Time-series options:"}, new Option {Key = nameof(context.TimeSeries), Setter = value => context.TimeSeries = value, Getter = () => context.TimeSeries, Description = "Target time-series identifier or unique ID"}, new Option {Key = nameof(context.TimeRange), Setter = value => context.TimeRange = ParseInterval(value), Getter = () => context.TimeRange?.ToString(), Description = "Time-range for overwrite in ISO8061/ISO8601 (defaults to start/end points)"}, @@ -329,7 +347,7 @@ var usageMessage if (context.SourceTimeSeries != null && string.IsNullOrEmpty(context.SourceTimeSeries.Server) && string.IsNullOrEmpty(context.Server)) throw new ExpectedException($"A /{nameof(context.Server)} option is required to load the source time-series.\n\n{helpGuidance}"); - if (!context.StopAfterSavingCsv && string.IsNullOrWhiteSpace(context.FindTimezones)) + if (!context.StopAfterSavingCsv && string.IsNullOrWhiteSpace(context.FindTimezones) && !context.MultiRunStdin) { if (string.IsNullOrWhiteSpace(context.Server)) throw new ExpectedException($"A /{nameof(context.Server)} option is required.\n\n{helpGuidance}"); diff --git a/TimeSeries/PublicApis/SdkExamples/PointZilla/packages.config b/TimeSeries/PublicApis/SdkExamples/PointZilla/packages.config index cefea76..c6340dd 100644 --- a/TimeSeries/PublicApis/SdkExamples/PointZilla/packages.config +++ b/TimeSeries/PublicApis/SdkExamples/PointZilla/packages.config @@ -13,6 +13,7 @@ +