Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the -i <FILENAME> argument to be specified multiple times #359

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/IngestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidDataHandlingFeature>();
_properties = Enable<PropertiesFeature>();

Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/PrintCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/SeqCli/Cli/Commands/Signal/ImportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override async Task<int> Run()
{
var connection = _connectionFactory.Connect(_connection);

using var input = _fileInputFeature.OpenInput();
using var input = _fileInputFeature.OpenSingleInput();
var line = await input.ReadLineAsync();
while (line != null)
{
Expand Down
42 changes: 28 additions & 14 deletions src/SeqCli/Cli/Features/FileInputFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SeqCli.Util;

namespace SeqCli.Cli.Features;

class FileInputFeature : CommandFeature
{
readonly string _description;
readonly bool _supportsWildcard;
readonly bool _allowMultiple;
readonly List<string> _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)
Expand All @@ -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<TextReader> 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);
}
}
}
}