use argh::FromArgs;
use regex::Regex;
use solr_post::{solr_post, PostConfig};
use std::io::{self, Write};
use std::sync::{Mutex, OnceLock};
#[derive(FromArgs)]
struct SolrPostArgs {
#[argh(option, short = 'c')]
collection: String,
#[argh(option, short = 'h', default = "String::from(\"localhost\")")]
host: String,
#[argh(option, short = 'p', default = "8983")]
port: u16,
#[argh(option)]
url: Option<String>,
#[argh(option, short = 'u')]
user: Option<String>,
#[argh(option, short = 'd')]
directory: String,
#[argh(
option,
short = 'f',
default = "String::from(\"xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log\")"
)]
file_extensions: String,
#[argh(option, default = "8")]
concurrency: usize,
#[argh(option, short = 'e')]
exclude_regex: Option<String>,
#[argh(option, short = 'i')]
include_regex: Option<String>,
}
impl From<SolrPostArgs> for PostConfig {
fn from(val: SolrPostArgs) -> Self {
PostConfig {
collection: val.collection,
host: val.host,
port: val.port,
directory_path: val.directory.into(),
file_extensions: val
.file_extensions
.split(',')
.map(|s| s.to_string())
.collect(),
update_url: val.url,
concurrency: val.concurrency,
exclued_regex: val
.exclude_regex
.map(|s| Regex::new(&format!("(?i){}", s)).unwrap()),
include_regex: val
.include_regex
.map(|s| Regex::new(&format!("(?i){}", s)).unwrap()),
basic_auth_creds: val.user,
}
}
}
#[tokio::main]
async fn main() {
let args: SolrPostArgs = argh::from_env();
static TOTAL_FILES_TO_INDEX: OnceLock<Mutex<u64>> = OnceLock::new();
TOTAL_FILES_TO_INDEX.get_or_init(|| Mutex::new(0u64));
let on_start = move |total_files: u64| {
let total_files_to_index = TOTAL_FILES_TO_INDEX.get().unwrap();
let mut total_files_to_index = total_files_to_index.lock().unwrap();
*total_files_to_index = total_files;
println!(
"Start indexing {} files with concurrency {}",
total_files_to_index, args.concurrency
);
};
let on_next = |indexed_count: u64| {
let total_files_to_index = TOTAL_FILES_TO_INDEX.get().unwrap();
let total_files_to_index = total_files_to_index.lock().unwrap();
let percetn_complete = (indexed_count as f64 / *total_files_to_index as f64) * 100.0;
print!(
"{}/{} indexed {:.2}%\r",
indexed_count, *total_files_to_index, percetn_complete
);
io::stdout().flush().unwrap(); };
let on_finish = || {
println!("\nFinished indexing.");
};
solr_post(
args.into(),
Some(Box::new(on_start)),
Some(Box::new(on_next)),
Some(Box::new(on_finish)),
)
.await;
}