Skip to content

Commit

Permalink
and for interactive args
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Feb 10, 2025
1 parent 63e296d commit 85d725a
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/dfx/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,33 +231,47 @@ pub fn blob_from_arguments(
} else if is_terminal {
use candid_parser::assist::{input_args, Context};
let mut ctx = Context::new(env.clone());
if let Some(env) = dfx_env {
let principals = gather_principals_from_env(env);
if !principals.is_empty() {
let mut map = BTreeMap::new();
map.insert("principal".to_string(), principals);
ctx.set_completion(map);
}
}
if is_init_arg {
eprintln!("This canister requires an initialization argument.");
} else {
eprintln!("This method requires arguments.");
let dfx_env = dfx_env.expect(
"internal error: requiring interactive args without Environment",
);
let principals = gather_principals_from_env(dfx_env);
if !principals.is_empty() {
let mut map = BTreeMap::new();
map.insert("principal".to_string(), principals);
ctx.set_completion(map);
}
let args = input_args(&ctx, &func.args)?;
eprintln!("Sending the following argument:\n{}\n", args);
if is_init_arg {
let mut args = Err(anyhow!("uninitialized"));
dfx_env.with_suspend_all_spinners(Box::new(|| {
if is_init_arg {
eprintln!("This canister requires an initialization argument.");
} else {
eprintln!("This method requires arguments.");
}
args = input_args(&ctx, &func.args);
if args.is_err() {
return;
}
eprintln!(
"Do you want to initialize the canister with this argument? [y/N]"
"Sending the following argument:\n{}\n",
args.as_ref().unwrap()
);
} else {
eprintln!("Do you want to send this message? [y/N]");
}
let mut input = String::new();
stdin().read_line(&mut input)?;
if !["y", "Y", "yes", "Yes", "YES"].contains(&input.trim()) {
return Err(error_invalid_data!("User cancelled."));
}
if is_init_arg {
eprintln!(
"Do you want to initialize the canister with this argument? [y/N]"
);
} else {
eprintln!("Do you want to send this message? [y/N]");
}
let mut input = String::new();
if let Err(e) = stdin().read_line(&mut input) {
args = Err(e.into());
return;
}
if !["y", "Y", "yes", "Yes", "YES"].contains(&input.trim()) {
args = Err(error_invalid_data!("User cancelled."));
}
}));
let args = args?;
args.to_bytes_with_types(env, &func.args)
} else {
return Err(error_invalid_data!("Expected arguments but found none."));
Expand Down

0 comments on commit 85d725a

Please sign in to comment.