feat(cli): add query cli subcmd skeleton
This commit is contained in:
parent
f2df172a46
commit
81910ce608
5 changed files with 64 additions and 29 deletions
|
@ -96,9 +96,7 @@ All datetime are UTC only (at least internally).
|
|||
- [x] handle one language quick labels as (name: description:)
|
||||
- [x] !labels shortcut
|
||||
- handle recursive entries definition
|
||||
|
||||
- for today:
|
||||
|
||||
- [ ] function to take a ParsedEntry with details and turn it to a Entry struct removing every intermediate ParseOutput
|
||||
|
||||
## Definition of the MVP
|
||||
|
||||
|
|
|
@ -31,18 +31,22 @@ fn define_cli() -> Command {
|
|||
arg!(--watch)
|
||||
)
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("status")
|
||||
.about("Get the status of the database")
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("list")
|
||||
.about("List the notes in the database")
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("status")
|
||||
.about("Check the status of the database")
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("get")
|
||||
.about("Get details of a particular item")
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("query")
|
||||
.about("Get a list of entries that meet a or many criterions")
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -201,32 +205,29 @@ fn main() {
|
|||
|
||||
match matches.subcommand() {
|
||||
Some(("status", _sub_matches)) => {
|
||||
println!("status...");
|
||||
// // output manifest
|
||||
// dbg!(manifest);
|
||||
|
||||
|
||||
// // output the number of entries in the db
|
||||
// dbg!(notebook.entries.len());
|
||||
todo!();
|
||||
},
|
||||
|
||||
Some(("list", _sub_matches)) => {
|
||||
match cli::list::get_list(¬ebook_context) {
|
||||
Err(err) => {
|
||||
println!("{:?}", err);
|
||||
},
|
||||
Ok(res) => {
|
||||
println!("{}", res);
|
||||
}
|
||||
Err(err) => eprintln!("{:?}", err),
|
||||
Ok(res) => println!("{}", res)
|
||||
}
|
||||
},
|
||||
|
||||
Some(("query", _sub_matches)) => {
|
||||
match cli::list::get_list(¬ebook_context) {
|
||||
Err(err) => eprintln!("{:?}", err),
|
||||
Ok(res) => println!("{}", res)
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Some(("index", sub_matches)) => {
|
||||
println!("Indexing...");
|
||||
|
||||
// have clap handle the options with a struct
|
||||
if *sub_matches.get_one::<bool>("watch").unwrap_or(&false) {
|
||||
dbg!("watched flag");
|
||||
match indexer::index_watch_daemon(¬ebook_context) {
|
||||
Ok(()) => {
|
||||
return
|
||||
|
@ -242,9 +243,7 @@ fn main() {
|
|||
Ok(notebook) => {
|
||||
println!("Successfully indexed a notebook with {} entries", notebook.entries.len());
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("Failed to index the notebook {:?}", err);
|
||||
}
|
||||
Err(err) => eprintln!("Failed to index the notebook {:?}", err)
|
||||
}
|
||||
},
|
||||
_ => todo!()
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::unwrap_or_return;
|
|||
use crate::cli::CliErr;
|
||||
use crate::database::NotebookContext;
|
||||
use crate::database::read::read_db;
|
||||
use crate::database::labels::get_formatted_name;
|
||||
use crate::database::labels::get_entry_summary;
|
||||
|
||||
pub fn get_list(context: &NotebookContext) -> Result<String, CliErr> {
|
||||
let notebook = unwrap_or_return!(
|
||||
|
@ -19,15 +19,18 @@ pub fn get_list(context: &NotebookContext) -> Result<String, CliErr> {
|
|||
|
||||
// output the number of entries in the db
|
||||
output.push_str(&format!("Got {} entries\n", notebook.entries.len()).to_string());
|
||||
|
||||
// output a summary of what inside the notebook
|
||||
for entry_container in notebook.entries {
|
||||
output.push_str(&(
|
||||
get_formatted_name(
|
||||
output.push_str(
|
||||
&get_entry_summary(
|
||||
&entry_container.entry,
|
||||
"en"
|
||||
).unwrap_or("<??>".to_string()))
|
||||
)
|
||||
);
|
||||
output.push_str("\n");
|
||||
}
|
||||
|
||||
// TODO: Json output
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
|
|
@ -9,3 +9,4 @@ struct CliErr {
|
|||
|
||||
|
||||
pub mod list;
|
||||
pub mod query;
|
||||
|
|
34
src/lib/cli/query.rs
Normal file
34
src/lib/cli/query.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
use crate::unwrap_or_return;
|
||||
use crate::cli::CliErr;
|
||||
use crate::database::NotebookContext;
|
||||
use crate::database::read::read_db;
|
||||
use crate::database::labels::get_entry_summary;
|
||||
|
||||
pub fn get_list(context: &NotebookContext) -> Result<String, CliErr> {
|
||||
let notebook = unwrap_or_return!(
|
||||
read_db(&context),
|
||||
|err| {
|
||||
CliErr {
|
||||
message: format!("Failed to read db: {:?}", err),
|
||||
exit_code: 2
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
let mut output = String::new();
|
||||
|
||||
// output the number of entries in the db
|
||||
output.push_str(&format!("Got {} entries\n", notebook.entries.len()).to_string());
|
||||
|
||||
// output a summary of what inside the notebook
|
||||
for entry_container in notebook.entries {
|
||||
output.push_str(
|
||||
&get_entry_summary(
|
||||
&entry_container.entry,
|
||||
)
|
||||
);
|
||||
output.push_str("\n");
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
Loading…
Reference in a new issue