Add --remove feature to query command

This commit is contained in:
Karcsesz 2024-02-15 23:16:43 +01:00
parent 3d69a6dd28
commit 2cf8786e34
4 changed files with 38 additions and 5 deletions

View file

@ -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

View file

@ -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 {

View file

@ -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!()
}

View file

@ -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,