diff --git a/README.md b/README.md index b088693..7eb83e9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,10 @@ simple process Lists the subject field of every resource in the db ### `query` -A simple command to get the response JSON to a query without `curl`. It pretty-prints the JSON. +A simple command to get the response JSON to a query without `curl`. It pretty-prints the JSON +to console. + +Can also remove individual resources from the database when passed the `--remove` flag. ### `editor` Uses the editor defined in `$EDITOR` (or `vi` if the environment variable isn't set) to edit diff --git a/src/args_parser.rs b/src/args_parser.rs index 1e88b95..dd5c23a 100644 --- a/src/args_parser.rs +++ b/src/args_parser.rs @@ -93,8 +93,15 @@ pub enum Command { List {}, /// Runs a single query against the database and returns the resource associated with the value Query { + #[command(flatten)] + save: SaveSettings, /// The resource to query for resource: String, + /// Remove the resource from the database after finding it + #[arg(long, alias = "rm")] + remove: bool, + #[command(flatten)] + server_reload: ServerReloadOptions, }, /// Open the resource in your system editor Editor { diff --git a/src/editor/commands/query.rs b/src/editor/commands/query.rs index eae225b..1df42e5 100644 --- a/src/editor/commands/query.rs +++ b/src/editor/commands/query.rs @@ -1,10 +1,24 @@ +use crate::args_parser::SaveSettings; use crate::schema::lookup_handler::LookupHandler; use std::io::stdout; use std::path::PathBuf; +use tracing::info; -pub fn query(database_path: PathBuf, handle: String) { - let data = LookupHandler::load(database_path).unwrap(); - let resource = data.lookup(handle.trim()).unwrap(); +pub fn query(database_path: PathBuf, save: SaveSettings, handle: String, remove: bool) { + let data = LookupHandler::load(&database_path).unwrap(); + let (index, resource) = data + .lookup_with_index(handle.trim()) + .expect("Couldn't find a resource for that query"); serde_json::to_writer_pretty(stdout(), resource).unwrap(); + if remove { + info!("Removing resource from database..."); + let mut data = data.into_inner(); + data.0.remove(index); + if save.save { + data.save(database_path).unwrap(); + } else { + info!("To save changes, rerun this command with the -s flag set.") + } + } println!() } diff --git a/src/main.rs b/src/main.rs index f91d145..394ab86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -54,7 +54,16 @@ fn main() { ); reload(data_paths.pid_file_path, server_reload); } - Command::Query { resource } => query(data_paths.database_path, resource), + Command::Query { + save, + resource, + remove, + server_reload, + } => { + query(data_paths.database_path, save, resource, remove); + + reload(data_paths.pid_file_path, server_reload); + } Command::Editor { save, resource,