bin: chorus_cmd

This commit is contained in:
Mike Dilger 2024-10-25 10:21:32 +13:00
parent 42a0f0136b
commit 2794515ab7
2 changed files with 70 additions and 0 deletions

65
src/bin/chorus_cmd.rs Normal file
View File

@ -0,0 +1,65 @@
use chorus::error::{ChorusError, Error};
use pocket_types::{Filter, Id, Pubkey, Tags};
use std::env;
const USAGE: &'static str = "usage: chorus_cmd <config_path> <command> [args...]";
fn main() -> Result<(), Error> {
// Get args (config path)
let mut args = env::args();
if args.len() <= 1 {
panic!("USAGE: chorus_moderate <config_path>");
}
let _ = args.next(); // ignore program name
// Load config
let config_path = args
.next()
.ok_or::<Error>(ChorusError::General(USAGE.to_owned()).into())?;
let mut config = chorus::load_config(config_path)?;
// Force allow of scraping (this program is a scraper)
config.allow_scraping = true;
// Setup store
let store = chorus::setup_store(&config)?;
// Setup logging
chorus::setup_logging(&config);
// Handle command
let command = args
.next()
.ok_or::<Error>(ChorusError::General(USAGE.to_owned()).into())?;
match &*command {
"delete_by_id" => {
let idstr = args
.next()
.ok_or::<Error>(ChorusError::General("ID argument missing".to_owned()).into())?;
let id: Id = Id::read_hex(idstr.as_bytes())?;
store.remove_event(id)?;
println!("Done.");
}
"delete_by_pubkey" => {
let pubstr = args.next().ok_or::<Error>(
ChorusError::General("Pubkey argument missing".to_owned()).into(),
)?;
let pk: Pubkey = Pubkey::read_hex(pubstr.as_bytes())?;
let mut tags_buffer: [u8; 128] = [0; 128];
let (_, tags) = Tags::from_json(b"[]", &mut tags_buffer)?;
let mut filter_buffer: [u8; 128] = [0; 128];
let filter =
Filter::from_parts(&[], &[pk], &[], &tags, None, None, None, &mut filter_buffer)?;
let events = store.find_events(&filter, true, 0, 0, |_| true)?;
for event in events.iter() {
store.remove_event(event.id())?;
}
println!("Done.");
}
_ => {
return Err(ChorusError::General("Unknown command.".to_owned()).into());
}
}
Ok(())
}

View File

@ -70,6 +70,9 @@ pub enum ChorusError {
// From UTF8
FromUtf8(std::string::FromUtf8Error),
// General
General(String),
// Http
Http(hyper::http::Error),
@ -176,6 +179,7 @@ impl std::fmt::Display for ChorusError {
ChorusError::ErrorClose => write!(f, "Closing due to error(s)"),
ChorusError::EventIsInvalid(s) => write!(f, "Event is invalid: {s}"),
ChorusError::FromUtf8(e) => write!(f, "{e}"),
ChorusError::General(s) => write!(f, "{s}"),
ChorusError::Http(e) => write!(f, "{e}"),
ChorusError::Hyper(e) => write!(f, "{e}"),
ChorusError::InvalidUri(e) => write!(f, "{e}"),
@ -261,6 +265,7 @@ impl ChorusError {
ChorusError::ErrorClose => 1.0,
ChorusError::EventIsInvalid(_) => 0.2,
ChorusError::FromUtf8(_) => 0.2,
ChorusError::General(_) => 0.0,
ChorusError::Http(_) => 0.0,
ChorusError::Hyper(_) => 0.0,
ChorusError::InvalidUri(_) => 0.0,