From 85d725a09b0537c507097d3e044163fef9f3cc73 Mon Sep 17 00:00:00 2001 From: Adam Spofford Date: Mon, 10 Feb 2025 09:30:40 -0800 Subject: [PATCH] and for interactive args --- src/dfx/src/util/mod.rs | 62 +++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/src/dfx/src/util/mod.rs b/src/dfx/src/util/mod.rs index a5be089ed7..0451ed3f62 100644 --- a/src/dfx/src/util/mod.rs +++ b/src/dfx/src/util/mod.rs @@ -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."));