Skip to content

Commit e9df852

Browse files
committed
Add atomic-cli search command #778
1 parent 121ac0e commit e9df852

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ See [STATUS.md](server/STATUS.md) to learn more about which features will remain
88
## [v0.38.0] - UNRELEASED
99

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

1415
## [v0.37.0] - 2024-02-01

cli/src/main.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod commit;
1111
mod new;
1212
mod path;
1313
mod print;
14+
mod search;
1415

1516
#[derive(Parser)]
1617
#[command(
@@ -92,6 +93,12 @@ enum Commands {
9293
#[arg(required = true)]
9394
subject: String,
9495
},
96+
/// Full text search
97+
Search {
98+
/// The search query
99+
#[arg(required = true)]
100+
query: String,
101+
},
95102
/// List all bookmarks
96103
List,
97104
/// Validates the store
@@ -129,8 +136,8 @@ pub struct Context {
129136
}
130137

131138
impl Context {
132-
/// Sets an agent
133-
pub fn get_write_context(&self) -> Config {
139+
/// Returns the config (agent, key) from the user config dir
140+
pub fn read_config(&self) -> Config {
134141
if let Some(write_ctx) = self.write.borrow().as_ref() {
135142
return write_ctx.clone();
136143
};
@@ -250,6 +257,9 @@ fn exec_command(context: &mut Context) -> AtomicResult<()> {
250257
} => {
251258
commit::set(context, &subject, &property, &value)?;
252259
}
260+
Commands::Search { query } => {
261+
search::search(context, query)?;
262+
}
253263
Commands::Validate => {
254264
validate(context);
255265
}

cli/src/new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn prompt_instance(
4545
// I think URL generation could be better, though. Perhaps use a
4646
let path = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos();
4747

48-
let write_ctx = context.get_write_context();
48+
let write_ctx = context.read_config();
4949

5050
let mut subject = format!("{}/{}", write_ctx.server, path);
5151
if let Some(sn) = &preferred_shortname {

cli/src/search.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use atomic_lib::{errors::AtomicResult, urls, Storelike};
2+
3+
pub fn search(context: &crate::Context, query: String) -> AtomicResult<()> {
4+
let opts = atomic_lib::client::search::SearchOpts {
5+
limit: Some(10),
6+
include: Some(true),
7+
..Default::default()
8+
};
9+
let subject = atomic_lib::client::search::build_search_subject(
10+
&context.read_config().server,
11+
&query,
12+
opts,
13+
);
14+
let resource = context.store.get_resource(&subject)?;
15+
let members = resource
16+
.get(urls::ENDPOINT_RESULTS)
17+
.expect("No members?")
18+
.to_subjects(None)
19+
.unwrap();
20+
if members.is_empty() {
21+
println!("No results found.");
22+
println!("URL: {}", subject);
23+
return Ok(());
24+
} else {
25+
for member in members {
26+
println!("{}", member);
27+
}
28+
}
29+
Ok(())
30+
}

lib/src/client/search.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ fn build_filter_string(filters: &HashMap<String, String>) -> String {
6060
pub fn build_search_subject(server_url: &str, query: &str, opts: SearchOpts) -> String {
6161
let mut url = base_url(server_url);
6262

63-
if let Some(q) = query.strip_prefix("q=") {
64-
url.query_pairs_mut().append_pair("q", q);
65-
}
63+
url.query_pairs_mut().append_pair("q", query);
6664
if let Some(include) = opts.include {
6765
url.query_pairs_mut()
6866
.append_pair("include", &include.to_string());

0 commit comments

Comments
 (0)