Skip to content

Commit

Permalink
Switch cli to use Ad4mClient struct
Browse files Browse the repository at this point in the history
  • Loading branch information
lucksus committed Nov 24, 2022
1 parent 17a4975 commit c01f63a
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 83 deletions.
16 changes: 8 additions & 8 deletions cli/src/agent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{formatting::*, util::readline_masked};
use ad4m_client::agent;
use ad4m_client::Ad4mClient;
use anyhow::{bail, Result};
use clap::Subcommand;

Expand Down Expand Up @@ -27,14 +27,14 @@ pub enum AgentFunctions {
},
}

pub async fn run(cap_token: String, command: AgentFunctions) -> Result<()> {
pub async fn run(ad4m_client: Ad4mClient, command: AgentFunctions) -> Result<()> {
match command {
AgentFunctions::Me => {
let agent = agent::me(cap_token).await?;
let agent = ad4m_client.agent.me().await?;
print_agent(agent.into());
}
AgentFunctions::Status => {
let status = agent::status(cap_token).await?;
let status = ad4m_client.agent.status().await?;
println!(
"\x1b[36mDID: \x1b[97m{}",
status.did.unwrap_or_else(|| "<undefined>".to_string())
Expand All @@ -49,7 +49,7 @@ pub async fn run(cap_token: String, command: AgentFunctions) -> Result<()> {
);
}
AgentFunctions::Lock => {
let result = agent::lock(cap_token, readline_masked("Passphrase: ")?).await?;
let result = ad4m_client.agent.lock(readline_masked("Passphrase: ")?).await?;
if let Some(error) = result.error {
bail!(error);
} else {
Expand All @@ -63,15 +63,15 @@ pub async fn run(cap_token: String, command: AgentFunctions) -> Result<()> {
readline_masked("Passphrase: ")?
};

let result = agent::unlock(cap_token, pp).await?;
let result = ad4m_client.agent.unlock(pp).await?;
if let Some(error) = result.error {
bail!(error);
} else {
println!("Agent unlocked");
}
}
AgentFunctions::ByDID { did } => {
if let Some(agent) = agent::by_did(cap_token, did).await? {
if let Some(agent) = ad4m_client.agent.by_did(did).await? {
print_agent(agent.into());
} else {
println!("Agent not found");
Expand All @@ -89,7 +89,7 @@ pub async fn run(cap_token: String, command: AgentFunctions) -> Result<()> {
passphrase1
};

let result = agent::generate(cap_token, pp).await?;
let result = ad4m_client.agent.generate(pp).await?;
if let Some(error) = result.error {
bail!(error);
} else {
Expand Down
25 changes: 12 additions & 13 deletions cli/src/languages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ad4m_client::languages;
use ad4m_client::Ad4mClient;
use anyhow::{Context, Result};
use clap::Subcommand;
use rustyline::Editor;
Expand Down Expand Up @@ -30,9 +30,9 @@ pub enum LanguageFunctions {
Remove { address: String },
}

pub async fn run(cap_token: String, command: Option<LanguageFunctions>) -> Result<()> {
pub async fn run(ad4m_client: Ad4mClient, command: Option<LanguageFunctions>) -> Result<()> {
if command.is_none() {
let all_languages = languages::by_filter(cap_token, "".to_string()).await?;
let all_languages = ad4m_client.languages.by_filter(None).await?;
for language in all_languages {
println!("\x1b[36mName: \x1b[97m{}", language.name);
println!("\x1b[36mAddress: \x1b[97m{}", language.address);
Expand All @@ -53,37 +53,37 @@ pub async fn run(cap_token: String, command: Option<LanguageFunctions>) -> Resul

match command.unwrap() {
LanguageFunctions::All => {
let all_perspectives = languages::by_filter(cap_token, "".to_string()).await?;
let all_perspectives = ad4m_client.languages.by_filter(None).await?;
println!("{:#?}", all_perspectives);
}
LanguageFunctions::ByFilter { filter } => {
let languages = languages::by_filter(cap_token, filter).await?;
let languages = ad4m_client.languages.by_filter(Some(filter)).await?;
println!("{:#?}", languages);
}
LanguageFunctions::ByAddress { address } => {
let maybe_language = languages::by_address(cap_token, address).await?;
let maybe_language = ad4m_client.languages.by_address(address).await?;
if let Some(language) = maybe_language {
println!("{:#?}", language);
} else {
println!("Language not found");
}
}
LanguageFunctions::WriteSettings { address, settings } => {
languages::write_settings(cap_token, address, settings).await?;
ad4m_client.languages.write_settings(address, settings).await?;
println!("Language settings written");
}
LanguageFunctions::ApplyTemplateAndPublish {
source,
template_data,
} => {
let new_language =
languages::apply_template_and_publish(cap_token, source, template_data).await?;
ad4m_client.languages.apply_template_and_publish(source, template_data).await?;
println!("Language template applied and published!");
println!("Name: {}", new_language.name);
println!("Address: {}", new_language.address);
}
LanguageFunctions::Meta { address } => {
let meta = languages::meta(cap_token, address).await?;
let meta = ad4m_client.languages.meta(address).await?;
println!("{:#?}", meta);
}
LanguageFunctions::Publish { path } => {
Expand Down Expand Up @@ -119,8 +119,7 @@ pub async fn run(cap_token: String, command: Option<LanguageFunctions>) -> Resul
Some(source_code_link)
};

let publish_result = languages::publish(
cap_token,
let publish_result = ad4m_client.languages.publish(
path,
name,
description,
Expand All @@ -134,11 +133,11 @@ pub async fn run(cap_token: String, command: Option<LanguageFunctions>) -> Resul
);
}
LanguageFunctions::Source { address } => {
let source = languages::source(cap_token, address).await?;
let source = ad4m_client.languages.source(address).await?;
println!("{}", source);
}
LanguageFunctions::Remove { address } => {
languages::remove(cap_token, address).await?;
ad4m_client.languages.remove(address).await?;
println!("Language removed");
}
};
Expand Down
22 changes: 12 additions & 10 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ enum Domain {
async fn main() -> Result<()> {
let args = ClapApp::parse();

if let Some(custom_url) = args.executor_url {
set_executor_url(custom_url);
let executor_url = if let Some(custom_url) = args.executor_url {
custom_url
} else {
set_executor_url(crate::startup::get_executor_url()?);
crate::startup::get_executor_url()?
};

let cap_token = if args.no_capability {
Expand All @@ -114,18 +114,20 @@ async fn main() -> Result<()> {
Domain::Log => "".to_string(),
Domain::Agent { command } => match command {
AgentFunctions::Lock | AgentFunctions::Unlock{ .. } | AgentFunctions::Generate { .. } => "".to_string(),
_ => startup::get_cap_token().await?,
_ => startup::get_cap_token(executor_url.clone()).await?,
},
_ => startup::get_cap_token().await?,
_ => startup::get_cap_token(executor_url.clone()).await?,
}
};

let ad4m_client = Ad4mClient::new(executor_url, cap_token);

match args.domain {
Domain::Agent { command } => agent::run(cap_token, command).await?,
Domain::Languages { command } => languages::run(cap_token, command).await?,
Domain::Perspectives { command } => perspectives::run(cap_token, command).await?,
Domain::Neighbourhoods { command } => neighbourhoods::run(cap_token, command).await?,
Domain::Runtime { command } => runtime::run(cap_token, command).await?,
Domain::Agent { command } => agent::run(ad4m_client, command).await?,
Domain::Languages { command } => languages::run(ad4m_client, command).await?,
Domain::Perspectives { command } => perspectives::run(ad4m_client, command).await?,
Domain::Neighbourhoods { command } => neighbourhoods::run(ad4m_client, command).await?,
Domain::Runtime { command } => runtime::run(ad4m_client, command).await?,
Domain::Log => {
let file = executor_data_path().join("ad4min.log");
let log = std::fs::read_to_string(file.clone()).with_context(|| {
Expand Down
8 changes: 4 additions & 4 deletions cli/src/neighbourhoods.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ad4m_client::neighbourhoods;
use ad4m_client::Ad4mClient;
use anyhow::Result;
use clap::Subcommand;

Expand All @@ -13,18 +13,18 @@ pub enum NeighbourhoodFunctions {
},
}

pub async fn run(cap_token: String, command: NeighbourhoodFunctions) -> Result<()> {
pub async fn run(ad4m_client: Ad4mClient, command: NeighbourhoodFunctions) -> Result<()> {
match command {
NeighbourhoodFunctions::Create {
perspective_id,
link_language,
} => {
let neighbourhood =
neighbourhoods::publish(cap_token, link_language, None, perspective_id).await?;
ad4m_client.neighbourhoods.publish(link_language, None, perspective_id).await?;
println!("Neighbourhood shared as: {}", neighbourhood);
}
NeighbourhoodFunctions::Join { url } => {
let neighbourhood = neighbourhoods::join(cap_token, url).await?;
let neighbourhood = ad4m_client.neighbourhoods.join(url).await?;
println!("Neighbourhod joined!\n{:#?}", neighbourhood);
}
};
Expand Down
27 changes: 12 additions & 15 deletions cli/src/perspectives.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{formatting::*, util::maybe_parse_datetime};
use ad4m_client::perspectives;
use ad4m_client::Ad4mClient;
use anyhow::Result;
use clap::{Args, Subcommand};
use regex::Regex;
Expand Down Expand Up @@ -64,9 +64,9 @@ pub enum PerspectiveFunctions {
Repl { id: String },
}

pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Result<()> {
pub async fn run(ad4m_client: Ad4mClient, command: Option<PerspectiveFunctions>) -> Result<()> {
if command.is_none() {
let all_perspectives = perspectives::all(cap_token).await?;
let all_perspectives = ad4m_client.perspectives.all().await?;
for perspective in all_perspectives {
println!("\x1b[36mName: \x1b[97m{}", perspective.name);
println!("\x1b[36mID: \x1b[97m{}", perspective.uuid);
Expand Down Expand Up @@ -101,25 +101,24 @@ pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Re

match command.unwrap() {
PerspectiveFunctions::Add { name } => {
let new_perspective_id = perspectives::add(cap_token, name).await?;
let new_perspective_id = ad4m_client.perspectives.add(name).await?;
println!("{}", new_perspective_id);
}
PerspectiveFunctions::Remove { id } => {
perspectives::remove(cap_token, id).await?;
ad4m_client.perspectives.remove(id).await?;
}
PerspectiveFunctions::AddLink {
id,
source,
target,
predicate,
} => {
perspectives::add_link(cap_token, id, source, target, predicate).await?;
ad4m_client.perspectives.add_link(id, source, target, predicate).await?;
}
PerspectiveFunctions::QueryLinks(args) => {
let from_date = maybe_parse_datetime(args.from_date)?;
let until_date = maybe_parse_datetime(args.until_date)?;
let result = perspectives::query_links(
cap_token,
let result = ad4m_client.perspectives.query_links(
args.id,
args.source,
args.target,
Expand All @@ -134,12 +133,11 @@ pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Re
}
}
PerspectiveFunctions::Infer { id, query } => {
let results = perspectives::infer(cap_token, id, query).await?;
let results = ad4m_client.perspectives.infer(id, query).await?;
print_prolog_results(results)?;
}
PerspectiveFunctions::Watch { id } => {
perspectives::watch(
cap_token,
ad4m_client.perspectives.watch(
id,
Box::new(|link| {
print_link(link);
Expand All @@ -148,7 +146,7 @@ pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Re
.await?;
}
PerspectiveFunctions::Snapshot { id } => {
let result = perspectives::snapshot(cap_token, id).await?;
let result = ad4m_client.perspectives.snapshot(id).await?;
println!("{:#?}", result);
}
PerspectiveFunctions::Repl { id } => {
Expand Down Expand Up @@ -177,8 +175,7 @@ pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Re
Some(predicate)
};

perspectives::add_link(
cap_token.clone(),
ad4m_client.perspectives.add_link(
id.clone(),
source,
target,
Expand All @@ -188,7 +185,7 @@ pub async fn run(cap_token: String, command: Option<PerspectiveFunctions>) -> Re
continue;
}

match perspectives::infer(cap_token.clone(), id.clone(), line).await {
match ad4m_client.perspectives.infer(id.clone(), line).await {
Ok(results) => {
print_prolog_results(results)?;
}
Expand Down
Loading

0 comments on commit c01f63a

Please sign in to comment.