Skip to content

Commit 121ac0e

Browse files
committed
Migrate atomic_cli to use the derive API #890
1 parent 1655903 commit 121ac0e

File tree

7 files changed

+164
-223
lines changed

7 files changed

+164
-223
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ By far most changes relate to `atomic-server`, so if not specified, assume the c
55
**Changes to JS assets (including the front-end and JS libraries) are not shown here**, but in [`/browser/CHANGELOG`](/browser/CHANGELOG.md).
66
See [STATUS.md](server/STATUS.md) to learn more about which features will remain stable.
77

8-
## [UNRELEASED]
8+
## [v0.38.0] - UNRELEASED
99

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

1214
## [v0.37.0] - 2024-02-01
1315

cli/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ repository = "https://github.com/atomicdata-dev/atomic-server"
99
version = "0.37.0"
1010

1111
[dependencies]
12-
atomic_lib = {version = "0.37.0", path = "../lib", features = ["config", "rdf"]}
13-
clap = {version = "4", features = ["cargo"]}
12+
atomic_lib = { version = "0.37.0", path = "../lib", features = [
13+
"config",
14+
"rdf",
15+
] }
16+
clap = { version = "4", features = ["cargo", "derive"] }
1417
colored = "2"
1518
dirs = "4"
16-
edit = {version = "0.1", optional = true}
19+
edit = { version = "0.1", optional = true }
1720
promptly = "0.3"
1821
regex = "1"
1922

cli/src/commit.rs

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ use crate::Context;
22
use atomic_lib::{errors::AtomicResult, Storelike};
33

44
/// Apply a Commit using the Set method - create or update a value in a resource
5-
pub fn set(context: &Context) -> AtomicResult<()> {
6-
let subject = argument_to_url(context, "subject")?;
7-
let property = argument_to_string(context, "property")?;
8-
let value = argument_to_string(context, "value")?;
5+
pub fn set(context: &Context, subject: &str, property: &str, value: &str) -> AtomicResult<()> {
96
// If the resource is not found, create it
10-
let mut resource = match context.store.get_resource(&subject) {
7+
let mut resource = match context.store.get_resource(subject) {
118
Ok(r) => r,
12-
Err(_) => atomic_lib::Resource::new(subject),
9+
Err(_) => atomic_lib::Resource::new(subject.into()),
1310
};
1411
resource.set_shortname(&property, &value, &context.store)?;
1512
resource.save(&context.store)?;
@@ -18,13 +15,11 @@ pub fn set(context: &Context) -> AtomicResult<()> {
1815

1916
/// Apply a Commit using the Set method, where the value is edited in the user's text editor.
2017
#[cfg(feature = "native")]
21-
pub fn edit(context: &Context) -> AtomicResult<()> {
22-
let subject = argument_to_url(context, "subject")?;
23-
let prop = argument_to_string(context, "property")?;
18+
pub fn edit(context: &Context, subject: &str, prop: &str) -> AtomicResult<()> {
2419
// If the resource is not found, create it
2520
let mut resource = match context.store.get_resource(&subject) {
2621
Ok(r) => r,
27-
Err(_) => atomic_lib::Resource::new(subject),
22+
Err(_) => atomic_lib::Resource::new(subject.into()),
2823
};
2924
// If the prop is not found, create it
3025
let current_val = match resource.get_shortname(&prop, &context.store) {
@@ -40,45 +35,16 @@ pub fn edit(context: &Context) -> AtomicResult<()> {
4035
}
4136

4237
/// Apply a Commit using the Remove method - removes a property from a resource
43-
pub fn remove(context: &Context) -> AtomicResult<()> {
44-
let subject = argument_to_url(context, "subject")?;
45-
let prop = argument_to_string(context, "property")?;
46-
let mut resource = context.store.get_resource(&subject)?;
38+
pub fn remove(context: &Context, subject: &str, prop: &str) -> AtomicResult<()> {
39+
let mut resource = context.store.get_resource(subject)?;
4740
resource.remove_propval_shortname(&prop, &context.store)?;
4841
resource.save(&context.store)?;
4942
Ok(())
5043
}
5144

5245
/// Apply a Commit using the destroy method - removes a resource
53-
pub fn destroy(context: &Context) -> AtomicResult<()> {
54-
let subject = argument_to_url(context, "subject")?;
55-
let mut resource = context.store.get_resource(&subject)?;
46+
pub fn destroy(context: &Context, subject: &str) -> AtomicResult<()> {
47+
let mut resource = context.store.get_resource(subject)?;
5648
resource.destroy(&context.store)?;
5749
Ok(())
5850
}
59-
60-
/// Parses a single argument as string
61-
fn argument_to_string(context: &Context, argument: &str) -> AtomicResult<String> {
62-
let command_name = context.matches.subcommand_name().unwrap();
63-
let subcommand_matches = context.matches.subcommand_matches(command_name).unwrap();
64-
let user_arg = subcommand_matches
65-
.get_one::<String>(argument)
66-
.ok_or(format!("No argument value for {} found", argument))?;
67-
Ok(user_arg.to_string())
68-
}
69-
70-
/// Parses a single argument (URL or Bookmark), should return a valid URL
71-
fn argument_to_url(context: &Context, argument: &str) -> AtomicResult<String> {
72-
let command_name = context.matches.subcommand_name().unwrap();
73-
let subcommand_matches = context.matches.subcommand_matches(command_name).unwrap();
74-
let user_arg = subcommand_matches
75-
.get_one::<String>(argument)
76-
.ok_or(format!("No argument value for {} found", argument))?;
77-
let id_url: String = context
78-
.mapping
79-
.lock()
80-
.unwrap()
81-
.try_mapping_or_url(&String::from(user_arg))
82-
.ok_or(&*format!("No url found for {}", user_arg))?;
83-
Ok(id_url)
84-
}

0 commit comments

Comments
 (0)