Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/joepio/atomic"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
atomic_lib = { version = "0.15.0", path = "../lib", features = ["db", "rdf"] }
atomic_lib = { version = "0.16.0", path = "../lib", features = ["config", "db", "rdf"] }
promptly = "0.3.0"
clap = "2.33.1"
colored = "1.9.3"
Expand Down
7 changes: 5 additions & 2 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ atomic new class
- [x] Basic JSON Serialization
- [x] RDF (Turtle / N-Triples / RDF/XML) Serialization
- [x] Fetch data from the interwebs with `get` commands
- [ ] Works with [`atomic-server`](../server) (fetches from there, stores there, uses domain etc.) [#6](https://github.com/joepio/atomic/issues/6)
- [x] A `delta` command for manipulating existing resources
- [ ] Works with [`atomic-server`](../server) [#6](https://github.com/joepio/atomic/issues/6)
- [x] fetches data
- [ ] `set`, `remove` and `destroy` commands for commits
- [ ] `new` creates commits
- [x] A `delta` command for manipulating existing local resources
- [ ] Tests for the cli
- [ ] A `map` command for creating a bookmark and storing a copy

Expand Down
39 changes: 39 additions & 0 deletions cli/src/commit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::{Context, delta::argument_to_url};
use atomic_lib::{errors::AtomicResult};

/// Apply a Commit using the Set method - create or update a value in a resource
pub fn set(context: &Context) -> AtomicResult<()> {
let subcommand = "set";
let subcommand_matches = context.matches.subcommand_matches(subcommand).clone().unwrap();
let subject = argument_to_url(context, subcommand, "subject")?;
let prop = argument_to_url(context, subcommand, "property")?;
let val = subcommand_matches.value_of("value").unwrap();
let mut commit_builder = builder(context, subject);
commit_builder.set(prop, val.into());
post(context, commit_builder)?;
Ok(())
}

/// Apply a Commit using the Remove method - removes a property from a resource
pub fn remove(context: &Context) -> AtomicResult<()> {
let subcommand = "remove";
let subject = argument_to_url(context, subcommand, "subject")?;
let prop = argument_to_url(context, subcommand, "property")?;
let mut commit_builder = builder(context, subject);
commit_builder.remove(prop);
post(context, commit_builder)?;
Ok(())
}

fn builder(context: &Context, subject: String) -> atomic_lib::commit::CommitBuilder {
let write_ctx = context.get_write_context();
atomic_lib::commit::CommitBuilder::new(subject, write_ctx.author_subject)
}

/// Posts the Commit and applies it to the server
fn post(context: &Context , commit_builder: atomic_lib::commit::CommitBuilder) -> AtomicResult<()> {
let write_ctx = context.get_write_context();
let commit = commit_builder.sign(&write_ctx.author_private_key)?;
atomic_lib::client::post_commit(&format!("{}commit", &write_ctx.base_url), &commit)?;
Ok(())
}
12 changes: 6 additions & 6 deletions cli/src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use atomic_lib::{delta::DeltaDeprecated, errors::AtomicResult, DeltaLine, Storel
/// Processes a singe delta
pub fn delta(context: &mut Context) -> AtomicResult<()> {
let subcommand_matches = context.matches.subcommand_matches("delta").unwrap();
let method = subcommand_to_url(context, "method")?;
let subject = subcommand_to_url(context, "subject")?;
let property = match subcommand_to_url(context, "property") {
let method = argument_to_url(context, "delta", "method")?;
let subject = argument_to_url(context, "delta", "subject")?;
let property = match argument_to_url(context, "delta", "property") {
// If it's a valid URL, use that.
Ok(prop) => Ok(prop),
// If it's a shortname available from the Class of the resource, use that;
Expand Down Expand Up @@ -34,9 +34,9 @@ pub fn delta(context: &mut Context) -> AtomicResult<()> {
}

/// Parses a single argument (URL or Bookmark), should return a valid URL
pub fn subcommand_to_url(context: &Context, subcommand: &str) -> AtomicResult<String> {
let subcommand_matches = context.matches.subcommand_matches("delta").unwrap();
let user_arg = subcommand_matches.value_of(subcommand).unwrap();
pub fn argument_to_url(context: &Context, subcommand: &str, argument: &str) -> AtomicResult<String> {
let subcommand_matches = context.matches.subcommand_matches(subcommand).unwrap();
let user_arg = subcommand_matches.value_of(argument).ok_or(format!("No argument value for {} found", argument))?;
let id_url: String = context
.mapping.lock().unwrap()
.try_mapping_or_url(&String::from(user_arg))
Expand Down
84 changes: 74 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,47 @@
use path::SERIALIZE_OPTIONS;
use atomic_lib::{errors::AtomicResult, Storelike};
use atomic_lib::mapping::Mapping;
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand, crate_version};
use atomic_lib::{errors::AtomicResult, Storelike};
use clap::{crate_version, App, AppSettings, Arg, ArgMatches, SubCommand};
use colored::*;
use dirs::home_dir;
use path::SERIALIZE_OPTIONS;
use std::{path::PathBuf, sync::Mutex};

mod commit;
mod delta;
mod new;
mod path;

#[allow(dead_code)]
pub struct Context<'a> {
base_url: String,
store: atomic_lib::Store,
mapping: Mutex<Mapping>,
matches: ArgMatches<'a>,
config_folder: PathBuf,
user_mapping_path: PathBuf,
write: Option<WriteContext>,
}

impl Context<'_> {
pub fn get_write_context(&self) -> WriteContext {
match self.write {
Some(_) => {
self.write.clone().unwrap()
}
None => {
panic!("No write context set");
}
}
}
}

#[derive(Clone)]
pub struct WriteContext {
/// URL of the Atomic Server to write to
base_url: String,
/// URL of the Author of Commits
author_subject: String,
/// Private key of the Author of Commits
author_private_key: String,
}

fn main() -> AtomicResult<()> {
Expand Down Expand Up @@ -82,9 +106,37 @@ fn main() -> AtomicResult<()> {
.required(true)
)
)
.subcommand(
SubCommand::with_name("set")
.about("Update an Atom's value. Writes a commit to the store using the current Agent.")
.arg(Arg::with_name("subject")
.help("Subject URL or bookmark of the resourece")
.required(true)
)
.arg(Arg::with_name("property")
.help("Property URL or shortname of the property")
.required(true)
)
.arg(Arg::with_name("value")
.help("String representation of the Value to be changed")
.required(true)
)
)
.subcommand(
SubCommand::with_name("remove")
.about("Remove a single Atom from a Resource. Writes a commit to the store using the current Agent.")
.arg(Arg::with_name("subject")
.help("Subject URL or bookmark of the resource")
.required(true)
)
.arg(Arg::with_name("property")
.help("Property URL or shortname of the property to be deleted")
.required(true)
)
)
.subcommand(
SubCommand::with_name("delta")
.about("Update the store using an single Delta",
.about("Update the store using an single Delta. Deprecated in favor of `set`, `remove` and `",
)
.arg(Arg::with_name("method")
.help("Method URL or bookmark, describes how the resource will be changed. Only suppports Insert at the time")
Expand Down Expand Up @@ -132,22 +184,29 @@ fn main() -> AtomicResult<()> {
}
let store = atomic_lib::Store::init();

let agent_config_path = atomic_lib::config::default_path()?;
let agent_config = atomic_lib::config::read_config(&agent_config_path)?;
let write_context = WriteContext {
base_url: agent_config.server,
author_private_key: agent_config.private_key,
author_subject: agent_config.agent,
};

let mut context = Context {
// TODO: This should be configurable
base_url: "http://localhost/".into(),
mapping: Mutex::new(mapping),
store,
matches,
config_folder,
user_mapping_path,
write: Some(write_context),
};

exec_command(&mut context)?;
Ok(())
}

fn exec_command(context: &mut Context) -> AtomicResult<()>{
fn exec_command(context: &mut Context) -> AtomicResult<()> {
match context.matches.subcommand_name() {
Some("new") => {
new::new(context)?;
Expand All @@ -164,6 +223,12 @@ fn exec_command(context: &mut Context) -> AtomicResult<()>{
Some("delta") => {
delta::delta(context)?;
}
Some("set") => {
commit::set(context)?;
}
Some("remove") => {
commit::remove(context)?;
}
Some("populate") => {
populate(context)?;
}
Expand Down Expand Up @@ -192,8 +257,7 @@ fn list(context: &mut Context) {
/// Prints a resource to the terminal with readble formatting and colors
fn pretty_print_resource(url: &str, store: &mut dyn Storelike) -> AtomicResult<()> {
let mut output = String::new();
let resource = store
.get_resource_string(url)?;
let resource = store.get_resource_string(url)?;
for (prop_url, val) in resource {
let prop_shortname = store.property_url_to_shortname(&prop_url)?;
output.push_str(&*format!(
Expand All @@ -208,7 +272,7 @@ fn pretty_print_resource(url: &str, store: &mut dyn Storelike) -> AtomicResult<(
}

/// Triple Pattern Fragment Query
fn tpf(context: &mut Context) -> AtomicResult<()>{
fn tpf(context: &mut Context) -> AtomicResult<()> {
let subcommand_matches = context.matches.subcommand_matches("tpf").unwrap();
let subject = tpf_value(subcommand_matches.value_of("subject").unwrap());
let property = tpf_value(subcommand_matches.value_of("property").unwrap());
Expand Down
6 changes: 4 additions & 2 deletions cli/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ fn prompt_instance<'a>(
// I think URL generation could be better, though. Perhaps use a
let path = SystemTime::now().duration_since(UNIX_EPOCH)?.subsec_nanos();

let mut subject = format!("{}/{}", context.base_url, path);
let write_ctx = context.get_write_context();

let mut subject = format!("{}/{}", write_ctx.base_url, path);
if preffered_shortname.is_some() {
subject = format!("{}/{}-{}", context.base_url, path, preffered_shortname.clone().unwrap());
subject = format!("{}/{}-{}", write_ctx.base_url, path, preffered_shortname.clone().unwrap());
}

let mut new_resource: Resource = Resource::new(subject.clone(), &context.store);
Expand Down
23 changes: 13 additions & 10 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
[package]
name = "atomic_lib"
version = "0.15.0"
authors = ["Joep Meindertsma <joep@argu.co>"]
description = "Library for creating, storing, querying, validating and converting Atomic Data."
edition = "2018"
license = "MIT"
description = "Library for creating, storing, querying, validating and converting Atomic Data."
name = "atomic_lib"
readme = "README.md"
repository = "https://github.com/joepio/atomic"
version = "0.16.0"

[dependencies]
base64 = "0.13.0"
bincode = {version = "1.3.1", optional = true}
dirs = {version = "3.0.1", optional = true}
rand = "0.7.3"
regex = "1.3.9"
ring = "0.16.15"
rio_api = {version = "0.5.0", optional = true}
rio_turtle = {version = "0.5.0", optional = true}
serde = {version = "1.0.114", features = ["derive"]}
serde_json = "1.0.57"
serde = { version = "1.0.114", features = ["derive"] }
sled = {version = "0.34.3", optional = true}
bincode = {version = "1.3.1", optional = true}
toml = {version = "0.5.7", optional = true}
ureq = "1.4.0"
rio_turtle = { version = "0.5.0", optional = true}
rio_api = { version = "0.5.0", optional = true}
rand = "0.7.3"
ring = "0.16.15"
base64 = "0.13.0"
url = "2.1.1"

[features]
config = ["dirs", "toml"]
db = ["sled", "bincode"]
rdf = ["rio_api", "rio_turtle"]
14 changes: 9 additions & 5 deletions lib/defaults/default_store.ad3
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
["https://atomicdata.dev/classes/Agent","https://atomicdata.dev/properties/description","An Agent is a user that can create or modify data. It has two keys: a private and a public one. The private key should be kept secret. The publik key is for proving that the "]
["https://atomicdata.dev/classes/Agent","https://atomicdata.dev/properties/shortname","agent"]
["https://atomicdata.dev/classes/Collection","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Class\"]"]
["https://atomicdata.dev/classes/Collection","https://atomicdata.dev/properties/recommends","[\"https://atomicdata.dev/properties/collection/property\"]"]
["https://atomicdata.dev/classes/Collection","https://atomicdata.dev/properties/recommends","[\"https://atomicdata.dev/properties/collection/property\",\"https://atomicdata.dev/properties/collection/value\",\"https://atomicdata.dev/properties/collection/pageSize\",\"https://atomicdata.dev/properties/collection/members\",\"https://atomicdata.dev/properties/collection/totalPages\",\"https://atomicdata.dev/properties/collection/currentPage\"]"]
["https://atomicdata.dev/classes/Collection","https://atomicdata.dev/properties/description","A paginated set of resources that can be sorted. Accepts query parameters for setting the current page number, page size, sort attribute, sort direction"]
["https://atomicdata.dev/classes/Collection","https://atomicdata.dev/properties/shortname","collection"]
# Datatypes
Expand Down Expand Up @@ -142,10 +142,10 @@
["https://atomicdata.dev/properties/collection/members","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/resourceArray"]
["https://atomicdata.dev/properties/collection/members","https://atomicdata.dev/properties/shortname","members"]
["https://atomicdata.dev/properties/collection/members","https://atomicdata.dev/properties/description","The members are the list of resources in a collection."]
["https://atomicdata.dev/properties/collection/itemCount","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Property\"]"]
["https://atomicdata.dev/properties/collection/itemCount","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/integer"]
["https://atomicdata.dev/properties/collection/itemCount","https://atomicdata.dev/properties/shortname","item-count"]
["https://atomicdata.dev/properties/collection/itemCount","https://atomicdata.dev/properties/description","The total number of items in the collection."]
["https://atomicdata.dev/properties/collection/totalMembers","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Property\"]"]
["https://atomicdata.dev/properties/collection/totalMembers","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/integer"]
["https://atomicdata.dev/properties/collection/totalMembers","https://atomicdata.dev/properties/shortname","total-members"]
["https://atomicdata.dev/properties/collection/totalMembers","https://atomicdata.dev/properties/description","The count of items (members) in the collection."]
["https://atomicdata.dev/properties/collection/totalPages","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Property\"]"]
["https://atomicdata.dev/properties/collection/totalPages","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/integer"]
["https://atomicdata.dev/properties/collection/totalPages","https://atomicdata.dev/properties/shortname","total-pages"]
Expand All @@ -154,6 +154,10 @@
["https://atomicdata.dev/properties/collection/currentPage","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/integer"]
["https://atomicdata.dev/properties/collection/currentPage","https://atomicdata.dev/properties/shortname","current-page"]
["https://atomicdata.dev/properties/collection/currentPage","https://atomicdata.dev/properties/description","The curent page number of the collection. Defaults to 0."]
["https://atomicdata.dev/properties/collection/pageSize","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Property\"]"]
["https://atomicdata.dev/properties/collection/pageSize","https://atomicdata.dev/properties/datatype","https://atomicdata.dev/datatypes/integer"]
["https://atomicdata.dev/properties/collection/pageSize","https://atomicdata.dev/properties/shortname","page-size"]
["https://atomicdata.dev/properties/collection/pageSize","https://atomicdata.dev/properties/description","The amount of members per page."]
# Collections
["https://atomicdata.dev/classes","https://atomicdata.dev/properties/isA","[\"https://atomicdata.dev/classes/Collection\"]"]
["https://atomicdata.dev/classes","https://atomicdata.dev/properties/collection/property","https://atomicdata.dev/properties/isA"]
Expand Down
22 changes: 22 additions & 0 deletions lib/src/agents.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Logic for Agents - which are like Users

/// PKCS#8 keypair, serialized using base64
pub struct Pair {
pub private: String,
pub public: String,
}

/// Returns a new random PKCS#8 keypair.
pub fn generate_keypair() -> Pair {
use ring::signature::KeyPair;
let rng = ring::rand::SystemRandom::new();
let pkcs8_bytes = ring::signature::Ed25519KeyPair::generate_pkcs8(&rng)
.map_err(|_| "Error generating seed").unwrap();
let key_pair = ring::signature::Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref())
.map_err(|_| "Error generating keypair").unwrap();
let pair = Pair {
private: base64::encode(pkcs8_bytes.as_ref()),
public: base64::encode(key_pair.public_key().as_ref()),
};
pair
}
Loading