-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from perspect3vism/release-0.2.13
- Loading branch information
Showing
27 changed files
with
513 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
|
||
use anyhow::{Result}; | ||
use clap::Subcommand; | ||
use colour::{self, blue_ln, green_ln}; | ||
use std::fs; | ||
use std::sync::mpsc::channel; | ||
|
||
use crate::bootstrap_publish::*; | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum DevFunctions { | ||
/// Generate bootstrap seed from a local prototype JSON file declaring languages to be published | ||
GenerateBootstrap { | ||
agent_path: String, | ||
passphrase: String, | ||
ad4m_host_path: String, | ||
seed_proto: String, | ||
}, | ||
} | ||
|
||
pub async fn run(command: DevFunctions) -> Result<()> { | ||
match command { | ||
DevFunctions::GenerateBootstrap { | ||
agent_path, | ||
passphrase, | ||
ad4m_host_path, | ||
seed_proto, | ||
} => { | ||
green_ln!( | ||
"Attempting to generate a new bootstrap seed using ad4m-host path: {:?} and agent path: {:?}\n", | ||
ad4m_host_path, | ||
agent_path | ||
); | ||
|
||
//Load the seed proto first so we know that works before making new agent path | ||
let seed_proto = fs::read_to_string(seed_proto)?; | ||
let seed_proto: SeedProto = serde_json::from_str(&seed_proto)?; | ||
green_ln!("Loaded seed prototype file!\n"); | ||
|
||
//Create a new ~/.ad4m-publish path with agent.json file supplied | ||
let data_path = dirs::home_dir() | ||
.expect("Could not get home directory") | ||
.join(".ad4m-publish"); | ||
let data_path_files = std::fs::read_dir(&data_path); | ||
if data_path_files.is_ok() { | ||
fs::remove_dir_all(&data_path)?; | ||
} | ||
//Create the ad4m directory | ||
fs::create_dir(&data_path)?; | ||
let ad4m_data_path = data_path.join("ad4m"); | ||
fs::create_dir(&ad4m_data_path)?; | ||
let data_data_path = data_path.join("data"); | ||
fs::create_dir(&data_data_path)?; | ||
|
||
//Read the agent file | ||
let agent_file = fs::read_to_string(agent_path)?; | ||
//Copy the agent file to correct directory | ||
fs::write(ad4m_data_path.join("agent.json"), agent_file)?; | ||
fs::write(data_data_path.join("DIDCache.json"), String::from("{}"))?; | ||
green_ln!("Publishing agent directory setup\n"); | ||
|
||
green_ln!("Creating temporary bootstrap seed for publishing purposes...\n"); | ||
let lang_lang_source = fs::read_to_string(&seed_proto.language_language_ref)?; | ||
let temp_bootstrap_seed = BootstrapSeed { | ||
trusted_agents: vec![], | ||
known_link_languages: vec![], | ||
language_language_bundle: lang_lang_source.clone(), | ||
direct_message_language: String::from(""), | ||
agent_language: String::from(""), | ||
perspective_language: String::from(""), | ||
neighbourhood_language: String::from(""), | ||
}; | ||
let temp_publish_bootstrap_path = data_path.join("publishing_bootstrap.json"); | ||
fs::write( | ||
&temp_publish_bootstrap_path, | ||
serde_json::to_string(&temp_bootstrap_seed)?, | ||
)?; | ||
|
||
//start ad4m-host with publishing bootstrap | ||
let ad4m_host_init = std::process::Command::new(&ad4m_host_path) | ||
.arg("init") | ||
.arg("--networkBootstrapSeed") | ||
.arg(&temp_publish_bootstrap_path) | ||
.arg("--dataPath") | ||
.arg(&data_path) | ||
.arg("--overrideConfig") | ||
.output()?; | ||
|
||
blue_ln!( | ||
"ad4m-host init output: {}\n", | ||
String::from_utf8_lossy(&ad4m_host_init.stdout) | ||
); | ||
|
||
green_ln!( | ||
"Starting publishing with bootstrap path: {}\n", | ||
temp_publish_bootstrap_path.to_str().unwrap() | ||
); | ||
|
||
let (tx, rx) = channel(); | ||
serve_ad4m_host(ad4m_host_path, data_path, tx)?; | ||
|
||
for line in &rx { | ||
println!("{}", line); | ||
if line.contains("GraphQL server started, Unlock the agent to start holohchain") { | ||
green_ln!("AD4M Host ready for publishing\n"); | ||
//Spawn in a new thread so we can continue reading logs in loop below, whilst publishing is happening | ||
tokio::spawn(async move { | ||
start_publishing( | ||
passphrase.clone(), | ||
seed_proto.clone(), | ||
lang_lang_source.clone(), | ||
) | ||
.await; | ||
}); | ||
break; | ||
} | ||
} | ||
|
||
for line in rx { | ||
println!("{}", line); | ||
} | ||
} | ||
}; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use ad4m_client::{Ad4mClient, expressions::expression}; | ||
use anyhow::{Result, bail}; | ||
use clap::Subcommand; | ||
use serde_json::Value; | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum ExpressionFunctions { | ||
Create { | ||
language_address: String, | ||
content: String, | ||
}, | ||
Get { | ||
url: String, | ||
}, | ||
GetRaw { | ||
url: String, | ||
}, | ||
} | ||
|
||
pub async fn run(ad4m_client: Ad4mClient, command: ExpressionFunctions) -> Result<()> { | ||
match command { | ||
ExpressionFunctions::Create { | ||
language_address, | ||
content, | ||
} => { | ||
let content = serde_json::from_str::<Value>(&content).unwrap_or_else(|_| { | ||
serde_json::from_str::<Value>(&format!("\"{}\"", content)).unwrap() | ||
}); | ||
let expression_url = ad4m_client | ||
.expressions | ||
.expression_create(language_address, content) | ||
.await?; | ||
println!("Expression created with url: {}", expression_url); | ||
} | ||
ExpressionFunctions::Get { url } => { | ||
let maybe_content: Option<expression::ExpressionExpression> = ad4m_client.expressions.expression(url.clone()).await?; | ||
match maybe_content { | ||
Some(content) => { | ||
println!("author: {}", content.author); | ||
println!("timestamp: {}", content.timestamp); | ||
if content.proof.valid.unwrap_or(false) { | ||
println!("signature: ✅"); | ||
} else { | ||
println!("signature: ❌"); | ||
} | ||
println!("data: {}", content.data) | ||
} | ||
None => println!("No expression found at url: {}", url), | ||
} | ||
} | ||
|
||
ExpressionFunctions::GetRaw { url } => { | ||
let maybe_content: Option<expression::ExpressionExpression> = ad4m_client.expressions.expression(url.clone()).await?; | ||
match maybe_content { | ||
Some(content) => { | ||
if let Ok(Value::String(content)) = serde_json::from_str::<Value>(&content.data) { | ||
println!("{}", &content); | ||
} else { | ||
println!("{}", content.data); | ||
} | ||
} | ||
None => bail!("No expression found at url: {}", url), | ||
} | ||
} | ||
}; | ||
Ok(()) | ||
} |
Oops, something went wrong.