Skip to content

Commit b33f455

Browse files
ravinderjangraS-Coyle
authored andcommitted
feat(tool): add option to join the existing network
1 parent 67df6d0 commit b33f455

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/lib.rs

+88
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,98 @@ struct CmdArgs {
6464
is_local: bool,
6565
}
6666

67+
/// Run a SAFE vault to join a network
68+
#[derive(StructOpt, Debug)]
69+
#[structopt(name = "safe-nlt-join")]
70+
struct JoinCmdArgs {
71+
/// Verbosity level for this tool
72+
#[structopt(short = "v", long, parse(from_occurrences))]
73+
verbosity: u8,
74+
75+
/// Path where to locate safe_vault/safe_vault.exe binary. The SAFE_VAULT_PATH env var can be also used to set the path
76+
#[structopt(short = "p", long, env = "SAFE_VAULT_PATH")]
77+
vault_path: Option<PathBuf>,
78+
79+
/// Path where to store the data for the running safe_vault
80+
#[structopt(short = "f", long, env = "SAFE_VAULT_DATA_PATH")]
81+
data_dir: Option<PathBuf>,
82+
83+
/// Path where the output directories for all the vaults are written
84+
#[structopt(short = "d", long, default_value = "./vaults")]
85+
vaults_dir: PathBuf,
86+
87+
/// Verbosity level for vaults logs (default: INFO)
88+
#[structopt(short = "y", long, parse(from_occurrences))]
89+
vaults_verbosity: u8,
90+
91+
/// IP used to launch the vaults with.
92+
#[structopt(long = "ip")]
93+
ip: Option<String>,
94+
}
95+
6796
pub fn run() -> Result<(), String> {
6897
run_with(None)
6998
}
7099

100+
pub fn join() -> Result<(), String> {
101+
join_with(None)
102+
}
103+
104+
pub fn join_with(cmd_args: Option<&[&str]>) -> Result<(), String> {
105+
// Let's first get all the arguments passed in, either as function's args, or CLI args
106+
let args = match cmd_args {
107+
None => JoinCmdArgs::from_args(),
108+
Some(cmd_args) => JoinCmdArgs::from_iter_safe(cmd_args).map_err(|err| err.to_string())?,
109+
};
110+
111+
let vault_bin_path = get_vault_bin_path(args.vault_path)?;
112+
let msg = format!(
113+
"Launching with vault executable from: {}",
114+
vault_bin_path.display()
115+
);
116+
if args.verbosity > 0 {
117+
println!("{}", msg);
118+
}
119+
debug!("{}", msg);
120+
121+
let mut common_args: Vec<&str> = vec![];
122+
123+
// We need a minimum of INFO level for vaults verbosity,
124+
// since the genesis vault logs the contact info at INFO level
125+
let verbosity = format!("-{}", "v".repeat(2 + args.vaults_verbosity as usize));
126+
common_args.push(&verbosity);
127+
128+
if let Some(ref data_dir) = args.data_dir {
129+
common_args.push("--data-dir");
130+
common_args.push(data_dir.to_str().unwrap());
131+
};
132+
133+
if let Some(ref ip) = args.ip {
134+
let msg = format!("Network hardcoded contact: {}", ip);
135+
if args.verbosity > 0 {
136+
println!("{}", msg);
137+
}
138+
debug!("{}", msg);
139+
140+
// Construct current vault's command arguments
141+
let vault_dir = &args
142+
.vaults_dir
143+
.join("safe_vault_logs")
144+
.display()
145+
.to_string();
146+
147+
let current_vault_args = build_vault_args(common_args.clone(), &vault_dir, Some(&ip));
148+
149+
let msg = "Launching vault...";
150+
if args.verbosity > 0 {
151+
println!("{}", msg);
152+
}
153+
debug!("{}", msg);
154+
run_vault_cmd(&vault_bin_path, &current_vault_args, args.verbosity)?;
155+
};
156+
Ok(())
157+
}
158+
71159
pub fn run_with(cmd_args: Option<&[&str]>) -> Result<(), String> {
72160
// Let's first get all the arguments passed in, either as function's args, or CLI args
73161
let args = match cmd_args {

0 commit comments

Comments
 (0)