This repository was archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathmain.rs
More file actions
executable file
·347 lines (326 loc) · 11.8 KB
/
main.rs
File metadata and controls
executable file
·347 lines (326 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#![warn(unused_extern_crates)]
#[macro_use]
extern crate holochain_common;
extern crate holochain_conductor_lib;
extern crate holochain_core;
extern crate holochain_core_types;
extern crate holochain_json_api;
extern crate holochain_locksmith;
extern crate holochain_net;
extern crate holochain_persistence_api;
extern crate holochain_persistence_file;
extern crate json_patch;
extern crate lib3h_crypto_api;
extern crate lib3h_sodium;
extern crate sim2h;
extern crate sim2h_client;
extern crate structopt;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate serde_derive;
extern crate base64;
extern crate colored;
extern crate semver;
#[macro_use]
extern crate serde_json;
extern crate dns_lookup;
extern crate flate2;
extern crate glob;
extern crate ignore;
extern crate in_stream;
extern crate rpassword;
extern crate tar;
extern crate tempfile;
extern crate tera;
extern crate url2;
mod cli;
mod config_files;
mod error;
mod util;
use crate::error::{HolochainError, HolochainResult};
use holochain_conductor_lib::happ_bundle::HappBundle;
use std::{fs::File, io::Read, path::PathBuf, str::FromStr};
use structopt::{clap::arg_enum, StructOpt};
new_relic_setup!("NEW_RELIC_LICENSE_KEY");
#[derive(StructOpt)]
/// A command line for Holochain
enum Cli {
#[structopt(alias = "p")]
/// Builds DNA source files into a single .dna.json DNA file
Package {
#[structopt(long, short, parse(from_os_str))]
output: Option<PathBuf>,
#[structopt(long, short)]
properties: Option<String>,
},
#[structopt(alias = "i")]
/// Initializes a new Holochain app at the given directory
Init {
#[structopt(parse(from_os_str))]
path: PathBuf,
},
#[structopt(alias = "g")]
/// Generates a new zome from a template
Generate {
#[structopt(parse(from_os_str))]
/// The path to the zome that should be generated (usually in ./zomes/)
zome: PathBuf,
#[structopt(default_value = "rust")]
/// Either the name of a built-in template (rust, rust-proc) or the url to a git repo containing a Zome template.
template: String,
},
#[structopt(alias = "r")]
/// Starts a development conductor with a websocket or http interface
Run {
#[structopt(long, short, default_value = "8888")]
/// The port to run the websocket server at
port: u16,
#[structopt(long, short = "b")]
/// Automatically package project before running
package: bool,
#[structopt(long = "dna", short = "d", parse(from_os_str))]
/// Absolute path to the .dna.json file to run. [default: ./dist/<dna-name>.dna.json]
dna_path: Option<PathBuf>,
#[structopt(long)]
/// Produce logging output
logging: bool,
#[structopt(long)]
/// Save generated data to file system
persist: bool,
#[structopt(long, possible_values = &NetworkingType::variants(), case_insensitive = true)]
/// Use real networking use: n3h/sim2h
networked: Option<NetworkingType>,
#[structopt(long, default_value = "ws://localhost:9000")]
/// Set the sim2h server url if you are using real networking.
sim2h_server: String,
#[structopt(long, short, default_value = "websocket")]
/// Specify interface type to use: websocket/http
interface: String,
#[structopt(long, short, default_value = cli::run::AGENT_NAME_DEFAULT)]
/// Specify agent name which will be used to generate the %agent_id.
agent_name: String,
},
#[structopt(alias = "t")]
/// Runs tests written in the test folder
Test {
#[structopt(long, short, default_value = "test")]
/// The folder containing the test files
dir: String,
#[structopt(long, short, default_value = "test/index.js")]
/// The path of the file to test
testfile: String,
#[structopt(long = "skip-package", short = "s")]
/// Skip packaging DNA
skip_build: bool,
#[structopt(long = "show-npm-output", short = "n")]
/// Show NPM output when installing test dependencies
show_npm_output: bool,
},
#[structopt(name = "keygen", alias = "k")]
/// Creates a new agent key pair, asks for a passphrase and writes an encrypted key bundle to disk in the XDG compliant config directory of Holochain, which is dependent on the OS platform (/home/alice/.config/holochain/keys or C:\\Users\\Alice\\AppData\\Roaming\\holochain\\holochain\\keys or /Users/Alice/Library/Preferences/com.holochain.holochain/keys)
KeyGen {
#[structopt(long, short, parse(from_os_str))]
/// Specify path of file
path: Option<PathBuf>,
#[structopt(long, short)]
/// Only print machine-readable output; intended for use by programs and scripts
quiet: bool,
#[structopt(long, short)]
/// Don't ask for passphrase
nullpass: bool,
},
#[structopt(name = "chain")]
/// View the contents of a source chain
ChainLog {
#[structopt(name = "INSTANCE")]
/// Instance ID to view
instance_id: Option<String>,
#[structopt(long, short, parse(from_os_str))]
/// Location of chain storage
path: Option<PathBuf>,
#[structopt(long, short)]
/// List available instances
list: bool,
},
#[structopt(name = "hash")]
/// Parse and hash a DNA file to determine its unique network hash
HashDna {
#[structopt(long, short, parse(from_os_str))]
/// Path to .dna.json file [default: dist/<dna-name>.dna.json]
path: Option<PathBuf>,
#[structopt(long, short = "x")]
/// Property (in the form 'name=value') that gets set/overwritten before calculating hash
property: Option<Vec<String>>,
},
Sim2hClient {
#[structopt(long, short = "u")]
/// url of the sim2h server
url: String,
#[structopt(long, short = "m", default_value = "ping")]
/// message to send to the sim2h server ('ping' or 'status')
message: String,
},
Walkman {
#[structopt()]
/// Path to walkman cassette file for playback
cassette: PathBuf,
},
}
arg_enum! {
#[derive(Debug)]
pub enum NetworkingType {
N3h,
Sim2h,
}
}
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CLI)]
fn main() {
lib3h_sodium::check_init();
run().unwrap_or_else(|err| {
eprintln!("{}", err);
::std::process::exit(1);
});
}
#[holochain_tracing_macros::newrelic_autotrace(HOLOCHAIN_CLI)]
fn run() -> HolochainResult<()> {
let args = Cli::from_args();
let project_path =
std::env::current_dir().map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
match args {
// If using default path, we'll create if necessary; otherwise, target dir must exist
Cli::Package {
output,
properties: properties_string,
} => {
let output = if let Some(output_inner) = output {
output_inner
} else {
util::std_package_path(&project_path).map_err(HolochainError::Default)?
};
let properties = properties_string
.map(|s| serde_json::Value::from_str(&s))
.unwrap_or_else(|| Ok(json!({})));
match properties {
Ok(properties) => {
cli::package(output, properties).map_err(HolochainError::Default)?
}
Err(e) => {
return Err(HolochainError::Default(format_err!(
"Failed to parse properties argument as JSON: {:?}",
e
)))
}
}
}
Cli::Init { path } => cli::init(&path).map_err(HolochainError::Default)?,
Cli::Generate { zome, template } => {
cli::generate(&zome, &template).map_err(HolochainError::Default)?
}
Cli::Run {
package,
port,
dna_path,
persist,
networked,
sim2h_server,
interface,
logging,
agent_name,
} => {
let dna_path = dna_path
.unwrap_or(util::std_package_path(&project_path).map_err(HolochainError::Default)?);
let interface_type = cli::get_interface_type_string(interface);
let bundle_path = project_path.join("bundle.toml");
let networked = networked.map(|n| cli::run::Networking::new(n, sim2h_server));
let conductor_config = if bundle_path.exists() {
let mut f = File::open(bundle_path)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
let mut contents = String::new();
f.read_to_string(&mut contents)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
let happ_bundle =
toml::from_str::<HappBundle>(&contents).expect("Error loading bundle.");
cli::hc_run_bundle_configuration(
&happ_bundle,
port,
persist,
networked,
logging,
agent_name,
)
.map_err(HolochainError::Default)?
} else {
cli::hc_run_configuration(
&dna_path,
port,
persist,
networked,
&interface_type,
logging,
agent_name,
)
.map_err(HolochainError::Default)?
};
println!(
"Booting conductor with following configuration: {:?}",
conductor_config
);
cli::run(dna_path, package, port, interface_type, conductor_config)
.map_err(HolochainError::Default)?
}
Cli::Test {
dir,
testfile,
skip_build,
show_npm_output,
} => {
let current_path = std::env::current_dir()
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
cli::test(¤t_path, &dir, &testfile, skip_build, show_npm_output)
}
.map_err(HolochainError::Default)?,
Cli::KeyGen {
path,
quiet,
nullpass,
} => {
let passphrase = if nullpass {
Some(String::from(holochain_common::DEFAULT_PASSPHRASE))
} else {
None
};
cli::keygen(path, passphrase, quiet)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?
}
Cli::ChainLog {
instance_id,
list,
path,
} => match (list, instance_id) {
(true, _) => cli::chain_list(path),
(false, None) => {
Cli::clap().print_help().expect("Couldn't print help!");
println!("\n\nTry `hc help chain` for more info");
}
(false, Some(instance_id)) => {
cli::chain_log(path, instance_id)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
}
},
Cli::HashDna { path, property } => {
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)
.map_err(|e| HolochainError::Default(format_err!("{}", e)))?;
println!("DNA Hash: {}", dna_hash);
}
Cli::Sim2hClient { url, message } => {
cli::sim2h_client(url, message)?;
}
Cli::Walkman { cassette } => {
cli::walkman(cassette);
}
}
Ok(())
}