@ -27,6 +27,7 @@ namespace SearchBoxCLI
@@ -27,6 +27,7 @@ namespace SearchBoxCLI
class MainClass {
private static OperatingModes Mode = OperatingModes . Query ;
private static bool Batch = false ;
private static string Name = string . Empty ;
private static IEnumerable < string > Tags ;
private static string SearchIndexFilepath = string . Empty ;
@ -52,6 +53,10 @@ namespace SearchBoxCLI
@@ -52,6 +53,10 @@ namespace SearchBoxCLI
Name = Name . Length > 0 ? Name : sourceFilename ;
break ;
case "batch" :
Batch = true ;
break ;
case "old-source" :
SourceOld = new StreamReader ( args [ + + i ] ) ;
break ;
@ -62,7 +67,7 @@ namespace SearchBoxCLI
@@ -62,7 +67,7 @@ namespace SearchBoxCLI
break ;
case "tags" :
Tags = Regex . Split ( args [ + + i ] , @",\s+ " ) ;
Tags = Regex . Split ( args [ + + i ] , @",\s* " ) ;
break ;
case "n" :
@ -118,6 +123,7 @@ namespace SearchBoxCLI
@@ -118,6 +123,7 @@ namespace SearchBoxCLI
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 ( " --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 ( "Examples:" ) ;
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
@@ -127,11 +133,13 @@ namespace SearchBoxCLI
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!" ) ;
return 1 ;
}
if ( SearchIndexFilepath = = string . Empty ) {
if ( SearchIndexFilepath = = string . Empty )
{
Console . Error . WriteLine ( "Error: No search index file path specified." ) ;
return 1 ;
}
@ -144,13 +152,36 @@ namespace SearchBoxCLI
@@ -144,13 +152,36 @@ namespace SearchBoxCLI
else
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 ) ) ;
Console . Error . WriteLine ( $"[Searchbox] [add] {Name} -> {SearchIndexFilepath}" ) ;
return 0 ;
}
private static int HandleRemove ( )
{
if ( Name = = string . Empty ) {
@ -169,6 +200,8 @@ namespace SearchBoxCLI
@@ -169,6 +200,8 @@ namespace SearchBoxCLI
File . WriteAllText ( SearchIndexFilepath , JsonConvert . SerializeObject ( searchBox ) ) ;
Console . Error . WriteLine ( $"[Searchbox] [remove] {Name} <- {SearchIndexFilepath}" ) ;
return 0 ;
}