From fa7c50b67ab02c8359c22bdd337ef6c37c838cee Mon Sep 17 00:00:00 2001 From: Nicholas Blumhardt Date: Tue, 9 Jul 2024 14:44:37 +1000 Subject: [PATCH] Allow the -i argument to be specified multiple times --- src/SeqCli/Cli/Commands/IngestCommand.cs | 2 +- src/SeqCli/Cli/Commands/PrintCommand.cs | 2 +- .../Cli/Commands/Signal/ImportCommand.cs | 2 +- src/SeqCli/Cli/Features/FileInputFeature.cs | 42 ++++++++++++------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/SeqCli/Cli/Commands/IngestCommand.cs b/src/SeqCli/Cli/Commands/IngestCommand.cs index 2d5ffce8..4137b3a6 100644 --- a/src/SeqCli/Cli/Commands/IngestCommand.cs +++ b/src/SeqCli/Cli/Commands/IngestCommand.cs @@ -47,7 +47,7 @@ class IngestCommand : Command public IngestCommand(SeqConnectionFactory connectionFactory) { _connectionFactory = connectionFactory; - _fileInputFeature = Enable(new FileInputFeature("File(s) to ingest", supportsWildcard: true)); + _fileInputFeature = Enable(new FileInputFeature("File(s) to ingest", allowMultiple: true)); _invalidDataHandlingFeature = Enable(); _properties = Enable(); diff --git a/src/SeqCli/Cli/Commands/PrintCommand.cs b/src/SeqCli/Cli/Commands/PrintCommand.cs index a5d24dda..bfc81113 100644 --- a/src/SeqCli/Cli/Commands/PrintCommand.cs +++ b/src/SeqCli/Cli/Commands/PrintCommand.cs @@ -44,7 +44,7 @@ public PrintCommand(SeqCliOutputConfig outputConfig) _noColor = outputConfig.DisableColor; _forceColor = outputConfig.ForceColor; - _fileInputFeature = Enable(new FileInputFeature("CLEF file to read", supportsWildcard: true)); + _fileInputFeature = Enable(new FileInputFeature("CLEF file to read", allowMultiple: true)); Options.Add("f=|filter=", "Filter expression to select a subset of events", diff --git a/src/SeqCli/Cli/Commands/Signal/ImportCommand.cs b/src/SeqCli/Cli/Commands/Signal/ImportCommand.cs index 29edb542..91133d2a 100644 --- a/src/SeqCli/Cli/Commands/Signal/ImportCommand.cs +++ b/src/SeqCli/Cli/Commands/Signal/ImportCommand.cs @@ -59,7 +59,7 @@ protected override async Task Run() { var connection = _connectionFactory.Connect(_connection); - using var input = _fileInputFeature.OpenInput(); + using var input = _fileInputFeature.OpenSingleInput(); var line = await input.ReadLineAsync(); while (line != null) { diff --git a/src/SeqCli/Cli/Features/FileInputFeature.cs b/src/SeqCli/Cli/Features/FileInputFeature.cs index 230b4266..a38110f2 100644 --- a/src/SeqCli/Cli/Features/FileInputFeature.cs +++ b/src/SeqCli/Cli/Features/FileInputFeature.cs @@ -15,6 +15,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using SeqCli.Util; namespace SeqCli.Cli.Features; @@ -22,22 +23,27 @@ namespace SeqCli.Cli.Features; class FileInputFeature : CommandFeature { readonly string _description; - readonly bool _supportsWildcard; + readonly bool _allowMultiple; + readonly List _inputFilenames = new(); - public FileInputFeature(string description, bool supportsWildcard = false) + public FileInputFeature(string description, bool allowMultiple = false) { _description = description; - _supportsWildcard = supportsWildcard; + _allowMultiple = allowMultiple; } - string? InputFilename { get; set; } - public override void Enable(OptionSet options) { - var wildcardHelp = _supportsWildcard ? $", including the `{DirectoryExt.Wildcard}` wildcard" : ""; + var wildcardHelp = _allowMultiple ? $", including the `{DirectoryExt.Wildcard}` wildcard" : ""; options.Add("i=|input=", $"{_description}{wildcardHelp}; if not specified, `STDIN` will be used", - v => InputFilename = string.IsNullOrWhiteSpace(v) ? null : v.Trim()); + v => + { + if (!string.IsNullOrWhiteSpace(v)) + { + _inputFilenames.Add(v.Trim()); + } + }); } static TextReader OpenText(string filename) @@ -46,21 +52,29 @@ static TextReader OpenText(string filename) File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)); } - public TextReader OpenInput() + public TextReader OpenSingleInput() { - return InputFilename != null ? OpenText(InputFilename) : Console.In; + return _inputFilenames.SingleOrDefault() is {} filename ? OpenText(filename) : Console.In; } public IEnumerable OpenInputs() { - if (InputFilename == null || !DirectoryExt.IncludesWildcard(InputFilename)) + if (_inputFilenames.Count == 0) { - yield return OpenInput(); + yield return OpenSingleInput(); } - else + + foreach (var filename in _inputFilenames) { - foreach (var path in DirectoryExt.GetFiles(InputFilename)) - yield return OpenText(path); + if (!DirectoryExt.IncludesWildcard(filename)) + { + yield return OpenText(filename); + } + else + { + foreach (var path in DirectoryExt.GetFiles(filename)) + yield return OpenText(path); + } } } } \ No newline at end of file