Add --remove feature to query command
This commit is contained in:
parent
3d69a6dd28
commit
2cf8786e34
4 changed files with 38 additions and 5 deletions
|
@ -53,7 +53,10 @@ simple process
|
||||||
Lists the subject field of every resource in the db
|
Lists the subject field of every resource in the db
|
||||||
|
|
||||||
### `query`
|
### `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`
|
### `editor`
|
||||||
Uses the editor defined in `$EDITOR` (or `vi` if the environment variable isn't set) to edit
|
Uses the editor defined in `$EDITOR` (or `vi` if the environment variable isn't set) to edit
|
||||||
|
|
|
@ -93,8 +93,15 @@ pub enum Command {
|
||||||
List {},
|
List {},
|
||||||
/// Runs a single query against the database and returns the resource associated with the value
|
/// Runs a single query against the database and returns the resource associated with the value
|
||||||
Query {
|
Query {
|
||||||
|
#[command(flatten)]
|
||||||
|
save: SaveSettings,
|
||||||
/// The resource to query for
|
/// The resource to query for
|
||||||
resource: String,
|
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
|
/// Open the resource in your system editor
|
||||||
Editor {
|
Editor {
|
||||||
|
|
|
@ -1,10 +1,24 @@
|
||||||
|
use crate::args_parser::SaveSettings;
|
||||||
use crate::schema::lookup_handler::LookupHandler;
|
use crate::schema::lookup_handler::LookupHandler;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
pub fn query(database_path: PathBuf, handle: String) {
|
pub fn query(database_path: PathBuf, save: SaveSettings, handle: String, remove: bool) {
|
||||||
let data = LookupHandler::load(database_path).unwrap();
|
let data = LookupHandler::load(&database_path).unwrap();
|
||||||
let resource = data.lookup(handle.trim()).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();
|
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!()
|
println!()
|
||||||
}
|
}
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -54,7 +54,16 @@ fn main() {
|
||||||
);
|
);
|
||||||
reload(data_paths.pid_file_path, server_reload);
|
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 {
|
Command::Editor {
|
||||||
save,
|
save,
|
||||||
resource,
|
resource,
|
||||||
|
|
Loading…
Reference in a new issue