Skip to content

Commit

Permalink
Add atomic-cli search command #778
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Jun 7, 2024
1 parent 121ac0e commit e9df852
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ See [STATUS.md](server/STATUS.md) to learn more about which features will remain
## [v0.38.0] - UNRELEASED

- Remove `process-management` feature #324 #334
- Add `atomic_lib::client::search` for building queries
- Add `atomic_lib::client::search` for building queries #778
- Add `atomic-cli search` command #778
- Migrate atomic_cli to use the derive API #890

## [v0.37.0] - 2024-02-01
Expand Down
14 changes: 12 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod commit;
mod new;
mod path;
mod print;
mod search;

#[derive(Parser)]
#[command(
Expand Down Expand Up @@ -92,6 +93,12 @@ enum Commands {
#[arg(required = true)]
subject: String,
},
/// Full text search
Search {
/// The search query
#[arg(required = true)]
query: String,
},
/// List all bookmarks
List,
/// Validates the store
Expand Down Expand Up @@ -129,8 +136,8 @@ pub struct Context {
}

impl Context {
/// Sets an agent
pub fn get_write_context(&self) -> Config {
/// Returns the config (agent, key) from the user config dir
pub fn read_config(&self) -> Config {
if let Some(write_ctx) = self.write.borrow().as_ref() {
return write_ctx.clone();
};
Expand Down Expand Up @@ -250,6 +257,9 @@ fn exec_command(context: &mut Context) -> AtomicResult<()> {
} => {
commit::set(context, &subject, &property, &value)?;
}
Commands::Search { query } => {
search::search(context, query)?;
}
Commands::Validate => {
validate(context);
}
Expand Down
2 changes: 1 addition & 1 deletion cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn prompt_instance(
// I think URL generation could be better, though. Perhaps use a
let path = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos();

let write_ctx = context.get_write_context();
let write_ctx = context.read_config();

let mut subject = format!("{}/{}", write_ctx.server, path);
if let Some(sn) = &preferred_shortname {
Expand Down
30 changes: 30 additions & 0 deletions cli/src/search.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use atomic_lib::{errors::AtomicResult, urls, Storelike};

pub fn search(context: &crate::Context, query: String) -> AtomicResult<()> {
let opts = atomic_lib::client::search::SearchOpts {
limit: Some(10),
include: Some(true),
..Default::default()
};
let subject = atomic_lib::client::search::build_search_subject(
&context.read_config().server,
&query,
opts,
);
let resource = context.store.get_resource(&subject)?;
let members = resource
.get(urls::ENDPOINT_RESULTS)
.expect("No members?")
.to_subjects(None)
.unwrap();
if members.is_empty() {
println!("No results found.");
println!("URL: {}", subject);
return Ok(());
} else {
for member in members {
println!("{}", member);
}
}
Ok(())
}
4 changes: 1 addition & 3 deletions lib/src/client/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ fn build_filter_string(filters: &HashMap<String, String>) -> String {
pub fn build_search_subject(server_url: &str, query: &str, opts: SearchOpts) -> String {
let mut url = base_url(server_url);

if let Some(q) = query.strip_prefix("q=") {
url.query_pairs_mut().append_pair("q", q);
}
url.query_pairs_mut().append_pair("q", query);
if let Some(include) = opts.include {
url.query_pairs_mut()
.append_pair("include", &include.to_string());
Expand Down

0 comments on commit e9df852

Please sign in to comment.