Add batch mode, but it's untested.

This commit is contained in:
Starbeamrainbowlabs 2018-09-11 19:12:56 +01:00
parent 7bef5d9b17
commit 03c2629a35
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
1 changed files with 37 additions and 4 deletions

View File

@ -27,6 +27,7 @@ namespace SearchBoxCLI
class MainClass { class MainClass {
private static OperatingModes Mode = OperatingModes.Query; private static OperatingModes Mode = OperatingModes.Query;
private static bool Batch = false;
private static string Name = string.Empty; private static string Name = string.Empty;
private static IEnumerable<string> Tags; private static IEnumerable<string> Tags;
private static string SearchIndexFilepath = string.Empty; private static string SearchIndexFilepath = string.Empty;
@ -52,6 +53,10 @@ namespace SearchBoxCLI
Name = Name.Length > 0 ? Name : sourceFilename; Name = Name.Length > 0 ? Name : sourceFilename;
break; break;
case "batch":
Batch = true;
break;
case "old-source": case "old-source":
SourceOld = new StreamReader(args[++i]); SourceOld = new StreamReader(args[++i]);
break; break;
@ -62,7 +67,7 @@ namespace SearchBoxCLI
break; break;
case "tags": case "tags":
Tags = Regex.Split(args[++i], @",\s+"); Tags = Regex.Split(args[++i], @",\s*");
break; break;
case "n": case "n":
@ -118,6 +123,7 @@ namespace SearchBoxCLI
Console.WriteLine(" --name, -n Sets the name of the source document {add, remove}"); Console.WriteLine(" --name, -n Sets the name of the source document {add, remove}");
Console.WriteLine(" --index Specifies the location of the search index to use {add, remove, update}"); Console.WriteLine(" --index Specifies the location of the search index to use {add, remove, update}");
Console.WriteLine(" --tags Sets the tags to associate with the document. {add, update}"); Console.WriteLine(" --tags Sets the tags to associate with the document. {add, update}");
Console.WriteLine(" --batch Enters a mode where the operations to process are specified via the source (by default stdin; change with --source as usual) - one per line in the format \"{filename}|{name}|{tags}\" {add}");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Examples:"); Console.WriteLine("Examples:");
Console.WriteLine(" cat books/complex_knots.txt | ./SearchBox.exe add --name \"Complex Knots: How to do and undo them\""); Console.WriteLine(" cat books/complex_knots.txt | ./SearchBox.exe add --name \"Complex Knots: How to do and undo them\"");
@ -127,11 +133,13 @@ namespace SearchBoxCLI
private static int HandleAdd() private static int HandleAdd()
{ {
if (Name == string.Empty) { if (Name == string.Empty && !Batch)
{
Console.Error.WriteLine("Error: The document name must be specified when reading from stdin!"); Console.Error.WriteLine("Error: The document name must be specified when reading from stdin!");
return 1; return 1;
} }
if (SearchIndexFilepath == string.Empty) { if (SearchIndexFilepath == string.Empty)
{
Console.Error.WriteLine("Error: No search index file path specified."); Console.Error.WriteLine("Error: No search index file path specified.");
return 1; return 1;
} }
@ -144,13 +152,36 @@ namespace SearchBoxCLI
else else
searchBox = JsonConvert.DeserializeObject<SearchBox>(File.ReadAllText(SearchIndexFilepath)); searchBox = JsonConvert.DeserializeObject<SearchBox>(File.ReadAllText(SearchIndexFilepath));
searchBox.AddDocument(Name, Tags, Source.ReadToEnd());
if (!Batch)
searchBox.AddDocument(Name, Tags, Source.ReadToEnd());
else {
string nextLine = "";
while ((nextLine = Source.ReadLine()) != null) {
string[] parts = nextLine.Split('|');
try {
searchBox.AddDocument(
parts[0].Trim(),
Regex.Split(parts[1], @",\s*"),
File.ReadAllText(parts[2].Trim())
);
Console.Error.WriteLine($"[Searchbox] [add] {parts[0].Trim()}");
} catch (FileNotFoundException) {
Console.Error.WriteLine($"Error: Can't find file {parts[2].Trim()}.");
return 1;
}
}
}
File.WriteAllText(SearchIndexFilepath, JsonConvert.SerializeObject(searchBox)); File.WriteAllText(SearchIndexFilepath, JsonConvert.SerializeObject(searchBox));
Console.Error.WriteLine($"[Searchbox] [add] {Name} -> {SearchIndexFilepath}");
return 0; return 0;
} }
private static int HandleRemove() private static int HandleRemove()
{ {
if (Name == string.Empty) { if (Name == string.Empty) {
@ -169,6 +200,8 @@ namespace SearchBoxCLI
File.WriteAllText(SearchIndexFilepath, JsonConvert.SerializeObject(searchBox)); File.WriteAllText(SearchIndexFilepath, JsonConvert.SerializeObject(searchBox));
Console.Error.WriteLine($"[Searchbox] [remove] {Name} <- {SearchIndexFilepath}");
return 0; return 0;
} }