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

Switched to persistent storage #729

Merged
merged 9 commits into from
Dec 13, 2018
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
6 changes: 5 additions & 1 deletion cmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ If we take Zome code in Rust as an example, you will need Rust and Cargo set up
```shell
$ rustup toolchain install nightly
$ rustup target add wasm32-unknown-unknown --toolchain nightly # adds WASM as a compilation target
$ rustup default nightly # switch to the nightly rust toolchain as your default
$ rustup override set nightly # switch to the nightly rust toolchain for the current project directory
```

Once that's done, you should be able to run commands like `cargo build --target=wasm32-unknown-unknown` and have it work.
Expand Down Expand Up @@ -187,6 +187,10 @@ where `test/test.js` is the path of the file.

You have the flexibility to write tests in quite a variety of ways, open to you to explore.

## Running your application
For the purpose of *testing* APIs or prototyping user interfaces, you can run your DNA using `hc run`.
This will start the application and open a WebSocket on port `8888` or any other port specified by the `-p`/`--port` option. The `-b`/`--package` flag will build your DNA before running it. By default, none of the data your application is putting on the source chain gets persisted. To persist data onto the file system, use the `--persist` flag.

## Contribute
Holochain is an open source project. We welcome all sorts of participation and are actively working on increasing surface area to accept it. Please see our [contributing guidelines](https://github.com/holochain/org/blob/master/CONTRIBUTING.md) for our general practices and protocols on participating in the community.

Expand Down
2 changes: 1 addition & 1 deletion cmd/src/cli/git-scaffold/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist/
node_modules/
target/

.hc
45 changes: 32 additions & 13 deletions cmd/src/cli/run.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
use cli::{self, package};
use colored::*;
use error::DefaultResult;
use holochain_container_api::{config::*, container::Container};
use std::fs;

const LOCAL_STORAGE_PATH: &str = ".hc";

const AGENT_CONFIG_ID: &str = "hc-run-agent";
const DNA_CONFIG_ID: &str = "hc-run-dna";
const INSTANCE_CONFIG_ID: &str = "test-instance";
const INTERFACE_CONFIG_ID: &str = "websocket-interface";

/// Starts a small container with the current application running
pub fn run(package: bool, port: u16) -> DefaultResult<()> {
pub fn run(package: bool, port: u16, persist: bool) -> DefaultResult<()> {
sphinxc0re marked this conversation as resolved.
Show resolved Hide resolved
if package {
cli::package(true, Some(package::DEFAULT_BUNDLE_FILE_NAME.into()))?;
}

let agent_config = AgentConfiguration {
id: "hc-run-agent".into(),
id: AGENT_CONFIG_ID.into(),
key_file: "hc_run.key".into(),
};

let dna_config = DNAConfiguration {
id: "hc-run-dna".into(),
id: DNA_CONFIG_ID.into(),
file: package::DEFAULT_BUNDLE_FILE_NAME.into(),
hash: "Qm328wyq38924ybogus".into(),
};

let storage = if persist {
fs::create_dir_all(LOCAL_STORAGE_PATH)?;

StorageConfiguration::File {
path: LOCAL_STORAGE_PATH.into(),
}
} else {
StorageConfiguration::Memory
};

let instance_config = InstanceConfiguration {
id: "test-instance".into(),
dna: "hc-run-dna".into(),
agent: "hc-run-agent".into(),
id: INSTANCE_CONFIG_ID.into(),
dna: DNA_CONFIG_ID.into(),
agent: AGENT_CONFIG_ID.into(),
logger: Default::default(),
storage: StorageConfiguration::Memory,
storage,
network: Some("{\"backend\": \"mock\"}".to_string()),
};

let interface_config = InterfaceConfiguration {
id: "websocket-interface".into(),
driver: InterfaceDriver::Websocket { port: port },
id: INTERFACE_CONFIG_ID.into(),
driver: InterfaceDriver::Websocket { port },
admin: true,
instances: vec![InstanceReferenceConfiguration {
id: "test-instance".into(),
id: INSTANCE_CONFIG_ID.into(),
}],
};

Expand Down Expand Up @@ -65,11 +84,11 @@ pub fn run(package: bool, port: u16) -> DefaultResult<()> {
loop {
let readline = rl.readline("hc> ")?;

match readline.as_str() {
match readline.as_str().trim() {
"exit" => break,
other if !other.is_empty() => eprintln!(
"command {:?} not recognized. Available commands are: exit",
other
"command {} not recognized. Available commands are: exit",
other.red().bold()
),
_ => continue,
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/src/cli/scaffold/assemblyscript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,14 @@ impl Scaffold for AssemblyScriptScaffold {
util::run_cmd(
base_path.as_ref().to_path_buf(),
"npm".into(),
vec!["init".to_owned(), "-y".to_owned()],
&["init", "-y"],
)?;

// add hdk-assemblyscript as a dependency
util::run_cmd(
base_path.as_ref().to_path_buf(),
"npm".into(),
vec![
"install".to_owned(),
"--save".to_owned(),
"holochain/hdk-assemblyscript".to_owned(),
],
&["install", "--save", "holochain/hdk-assemblyscript"],
)?;

// create a index.ts file
Expand Down
7 changes: 1 addition & 6 deletions cmd/src/cli/scaffold/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,7 @@ impl Scaffold for RustScaffold {
util::run_cmd(
base_path.as_ref().to_path_buf(),
"cargo".into(),
vec![
"init".to_owned(),
"--lib".to_owned(),
"--vcs".to_owned(),
"none".to_owned(),
],
&["init", "--lib", "--vcs", "none"],
)?;

// immediately rewrite the generated Cargo file, using some values
Expand Down
4 changes: 2 additions & 2 deletions cmd/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn test(
util::run_cmd(
tests_path.clone(),
"npm".to_string(),
vec!["install".to_string(), "--silent".to_string()],
&["install", "--silent"],
)?;
}

Expand All @@ -55,7 +55,7 @@ pub fn test(
util::run_cmd(
path.to_path_buf(),
"node".to_string(),
vec![testfile.to_string()],
&[testfile.to_string().as_str()],
)?;

Ok(())
Expand Down
16 changes: 7 additions & 9 deletions cmd/src/config_files/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ impl Build {
/// Starts the build using the supplied build steps and returns the contents of the artifact
pub fn run(&self, base_path: &PathBuf) -> DefaultResult<String> {
for (bin, args) in &self.steps {
util::run_cmd(base_path.to_path_buf(), bin.to_string(), args.clone())?;
let slice_vec: Vec<_> = args.iter().map(|e| e.as_str()).collect();
util::run_cmd(base_path.to_path_buf(), bin.to_string(), &slice_vec[..])?;
}

let artifact_path = base_path.join(&self.artifact);
Expand All @@ -59,15 +60,12 @@ impl Build {
}
}

pub fn cmd<S: Into<String> + Clone>(mut self, cmd: S, args: &[S]) -> Build {
let cmd: String = cmd.into();
let args: Vec<_> = args
pub fn cmd(mut self, cmd: &str, args: &[&str]) -> Build {
let cmd: String = cmd.to_owned();
let args = args
.to_vec()
.iter()
.map(|raw_arg| {
let arg: String = (*raw_arg).clone().into();

arg
})
.map(|raw_arg| raw_arg.to_string())
.collect();

self.steps.insert(cmd, args);
Expand Down
40 changes: 20 additions & 20 deletions cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ enum Cli {
help = "Automatically package project before running"
)]
package: bool,
#[structopt(long, help = "Save generated data to file system")]
persist: bool,
},
#[structopt(
name = "test",
Expand All @@ -115,15 +117,17 @@ enum Cli {
#[structopt(
long,
short,
help = "The folder containing the test files, defaults to 'test'"
default_value = "test",
help = "The folder containing the test files"
)]
dir: Option<String>,
dir: String,
#[structopt(
long,
short,
help = "The path of the file to test, defaults to 'test/dist/bundle.js'"
default_value = "test/index.js",
help = "The path of the file to test"
)]
testfile: Option<String>,
testfile: String,
#[structopt(long = "skip-package", short = "s", help = "Skip packaging DNA")]
skip_build: bool,
},
Expand All @@ -141,30 +145,26 @@ fn run() -> HolochainResult<()> {
let args = Cli::from_args();

match args {
Cli::Agent => cli::agent().map_err(|err| HolochainError::Default(err))?,
Cli::Agent => cli::agent().map_err(HolochainError::Default)?,
Cli::Package { strip_meta, output } => {
cli::package(strip_meta, output).map_err(|err| HolochainError::Default(err))?
cli::package(strip_meta, output).map_err(HolochainError::Default)?
}
Cli::Unpack { path, to } => {
cli::unpack(&path, &to).map_err(|err| HolochainError::Default(err))?
}
Cli::Init { path } => cli::init(&path).map_err(|err| HolochainError::Default(err))?,
Cli::Unpack { path, to } => cli::unpack(&path, &to).map_err(HolochainError::Default)?,
Cli::Init { path } => cli::init(&path).map_err(HolochainError::Default)?,
Cli::Generate { zome, language } => {
cli::generate(&zome, &language).map_err(|err| HolochainError::Default(err))?
}
Cli::Run { package, port } => {
cli::run(package, port).map_err(|err| HolochainError::Default(err))?
cli::generate(&zome, &language).map_err(HolochainError::Default)?
}
Cli::Run {
package,
port,
persist,
} => cli::run(package, port, persist).map_err(HolochainError::Default)?,
Cli::Test {
dir,
testfile,
skip_build,
} => {
let tests_folder = dir.unwrap_or(cli::TEST_DIR_NAME.to_string());
let test_file = testfile.unwrap_or("test/index.js".to_string());
cli::test(&PathBuf::from("."), &tests_folder, &test_file, skip_build)
.map_err(|err| HolochainError::Default(err))?
}
} => cli::test(&PathBuf::from("."), &dir, &testfile, skip_build)
.map_err(HolochainError::Default)?,
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion cmd/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::DefaultResult;
use colored::*;
use std::{path::PathBuf, process::Command};

pub fn run_cmd(base_path: PathBuf, bin: String, args: Vec<String>) -> DefaultResult<()> {
pub fn run_cmd(base_path: PathBuf, bin: String, args: &[&str]) -> DefaultResult<()> {
let pretty_command = format!("{} {}", bin.green(), args.join(" ").cyan());

println!("> {}", pretty_command);
Expand Down
2 changes: 1 addition & 1 deletion core/src/nucleus/ribosome/callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl FromStr for Callback {
match s {
"genesis" => Ok(Callback::Genesis),
"receive" => Ok(Callback::Receive),
"" => Ok(Callback::MissingNo),
other if other.is_empty() => Ok(Callback::MissingNo),
_ => Err("Cannot convert string to Callback"),
}
}
Expand Down