Skip to content

Commit

Permalink
Read multiple commands from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
GordPollock-AI committed Jun 6, 2023
1 parent 7085504 commit 6e65780
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions TimeSeries/PublicApis/SdkExamples/PointZilla/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.CommandLine, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\System.CommandLine.2.0.0-beta4.22272.1\lib\netstandard2.0\System.CommandLine.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
Expand Down
24 changes: 21 additions & 3 deletions TimeSeries/PublicApis/SdkExamples/PointZilla/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string> args)
{
var context = new Context
{
Expand All @@ -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)"},
Expand Down Expand Up @@ -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}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<package id="K4os.Compression.LZ4" version="1.1.11" targetFramework="net472" />
<package id="K4os.Compression.LZ4.Streams" version="1.1.11" targetFramework="net472" />
<package id="K4os.Hash.xxHash" version="1.0.6" targetFramework="net472" />
<package id="System.CommandLine" version="2.0.0-beta4.22272.1" targetFramework="net472" />
<package id="log4net" version="2.0.12" targetFramework="net472" />
<package id="Microsoft.Bcl" version="1.1.10" targetFramework="net47" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.0" targetFramework="net472" />
Expand Down

0 comments on commit 6e65780

Please sign in to comment.