Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

CLI additions #2161

Merged
merged 6 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG-UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed

- Adds `--uuid` flag to `hc hash`, allowing a UUID to be specified which will alter the hash [PR#2161](https://github.com/holochain/holochain-rust/pull/2161)
- Adds `--files` flag to `hc sim2h-client`, which when set prints JSON blobs to multiple files named by space hash (the previous default behavior), and when unset prints a single JSON blob to stdout for easy parsing by script [PR#2161](https://github.com/holochain/holochain-rust/pull/2161)

### Deprecated

### Removed
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/cli/hash_dna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::path::PathBuf;
pub fn hash_dna(
dna_file_path: &PathBuf,
properties: Option<Vec<String>>,
uuid: Option<String>,
) -> DefaultResult<Address> {
let mut dna = Conductor::load_dna(dna_file_path)?;
if let Some(properties) = properties {
Expand All @@ -34,6 +35,9 @@ pub fn hash_dna(
}
dna.properties = serde_json::Value::Object(map);
}
if let Some(uuid) = uuid {
dna.uuid = uuid;
}

Ok(dna.address())
}
60 changes: 39 additions & 21 deletions crates/cli/src/cli/sim2h_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@ use std::{
};
use url2::prelude::*;

/// Issue commands to a running sim2h server.
/// NOTE: general output should be on stderr, because at least one flag (-f)
/// needs to print valid JSON to stdout
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CLI)]
pub fn sim2h_client(url_string: String, message_string: String) -> Result<(), String> {
pub fn sim2h_client(
url_string: String,
message_string: String,
to_files: bool,
) -> Result<(), String> {
let url = match Url2::try_parse(url_string.clone()) {
Err(e) => Err(format!(
"unable to parse url:{} got error: {}",
Expand All @@ -27,9 +34,9 @@ pub fn sim2h_client(url_string: String, message_string: String) -> Result<(), St
let ip = if host == "localhost" {
"127.0.0.1".to_string()
} else {
println!("looking up: {}", host);
eprintln!("looking up: {}", host);
let ips: Vec<std::net::IpAddr> = lookup_host(&host).map_err(|e| format!("{}", e))?;
println!("resolved to: {}", ips[0]);
eprintln!("resolved to: {}", ips[0]);
format!("{}", ips[0])
};
let maybe_port = url.port();
Expand All @@ -38,7 +45,7 @@ pub fn sim2h_client(url_string: String, message_string: String) -> Result<(), St
}
let url = Url2::parse(format!("{}://{}:{}", url.scheme(), ip, maybe_port.unwrap()));

println!("connecting to: {}", url);
eprintln!("connecting to: {}", url);
let mut job = Job::new(&url)?;
job.send_wire(match message_string.as_ref() {
"ping" => WireMessage::Ping,
Expand Down Expand Up @@ -66,28 +73,39 @@ pub fn sim2h_client(url_string: String, message_string: String) -> Result<(), St
WireMessage::Pong
| WireMessage::HelloResponse(_)
| WireMessage::StatusResponse(_) => {
println!("Got response => {:?}", msg);
eprintln!("Got response => {:?}", msg);
break;
}
WireMessage::DebugResponse(debug_response_map) => {
println!("Got DebugResponse for {} spaces.", debug_response_map.len());
for (space, json) in debug_response_map {
let filename = format!("{}.json", space);
println!(
"Writing Sim2h state dump for space {} to file: {}",
space, filename
if to_files {
eprintln!(
"Got DebugResponse for {} spaces.",
debug_response_map.len()
);
for (space, json) in debug_response_map {
let filename = format!("{}.json", space);
eprintln!(
"Writing Sim2h state dump for space {} to file: {}",
space, filename
);

File::create(filename.clone())
.unwrap_or_else(|_| {
panic!("Could not create file {}!", filename)
})
.write_all(json.into_bytes().as_slice())
.expect("Could not write to file!");
File::create(filename.clone())
.unwrap_or_else(|_| {
panic!("Could not create file {}!", filename)
})
.write_all(json.into_bytes().as_slice())
.expect("Could not write to file!");
}
} else {
print!("{{");
for (space, json) in debug_response_map {
println!("\"{}\":{}", space, json);
}
println!("}}");
}
break;
}
_ => println!("{:?}", msg),
_ => eprintln!("{:?}", msg),
}
} else {
Err(format!("unexpected {:?}", frame))?;
Expand Down Expand Up @@ -127,10 +145,10 @@ impl Job {
});
let enc = hcid::HcidEncoding::with_kind("hcs0").map_err(|e| format!("{}", e))?;
let agent_id = enc.encode(&*pub_key).unwrap();
println!("Generated agent id: {}", agent_id);
eprintln!("Generated agent id: {}", agent_id);
let connection = await_in_stream_connect(connect_uri)
.map_err(|e| format!("Error awaiting connection: {}", e))?;
println!("Await successfull");
eprintln!("Await successfull");
let out = Self {
agent_id,
pub_key: Arc::new(Mutex::new(pub_key)),
Expand All @@ -143,7 +161,7 @@ impl Job {

/// sign a message and send it to sim2h
pub fn send_wire(&mut self, message: WireMessage) {
println!("Sending wire message to sim2h: {:?}", message);
eprintln!("Sending wire message to sim2h: {:?}", message);
let payload: Opaque = message.into();
let payload_buf: Box<dyn lib3h_crypto_api::Buffer> = Box::new(payload.clone().as_bytes());
let sig = base64::encode(
Expand Down
25 changes: 19 additions & 6 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ enum Cli {
#[structopt(long, short = "x")]
/// Property (in the form 'name=value') that gets set/overwritten before calculating hash
property: Option<Vec<String>>,
#[structopt(long, short)]
/// UUID that gets set/overwritten before calculating hash
uuid: Option<String>,
},
Sim2hClient {
#[structopt(long, short = "u")]
Expand All @@ -163,6 +166,10 @@ enum Cli {
#[structopt(long, short = "m", default_value = "ping")]
/// message to send to the sim2h server ('ping' or 'status')
message: String,
#[structopt(long, short = "f")]
/// if set, write the debug log for each Space to a separate file
/// whose name is the space hash
files: bool,
},
}
arg_enum! {
Expand Down Expand Up @@ -319,19 +326,25 @@ fn run() -> HolochainResult<()> {
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
}
},
Cli::HashDna { path, property } => {
Cli::HashDna {
path,
property,
uuid,
} => {
let dna_path = path
.unwrap_or(util::std_package_path(&project_path).map_err(HolochainError::Default)?);

let dna_hash = cli::hash_dna(&dna_path, property)
let dna_hash = cli::hash_dna(&dna_path, property, uuid)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
println!("DNA Hash: {}", dna_hash);
}

Cli::Sim2hClient { url, message } => {
println!("url: {}", &url);
println!("message: {}", &message);
cli::sim2h_client(url, message)?;
Cli::Sim2hClient {
url,
message,
files,
} => {
cli::sim2h_client(url, message, files)?;
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/sim2h/src/sim2h_im_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ pub struct Space {
#[serde(skip)]
pub crypto: Box<dyn CryptoSystem>,
pub redundancy: u64,
/// Though unusual to have a map where the keys and values are identical,
/// this map provides a way to get the [MonoRef] for a given [AspectHash]
pub all_aspects: im::HashMap<MonoAspectHash, MonoAspectHash>,
pub entry_to_all_aspects: im::HashMap<MonoEntryHash, EntryInfo>,
pub connections: im::HashMap<MonoAgentId, ConnectionState>,
Expand Down