Skip to content

Commit

Permalink
this should at least be a utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Feb 10, 2025
1 parent 43c4549 commit 5128f08
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
10 changes: 5 additions & 5 deletions src/dfx/src/lib/builders/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::models::canister::CanisterPool;
use crate::util::with_suspend_all_spinners;
use anyhow::{anyhow, bail, Context};
use candid::Principal as CanisterId;
use fn_error_context::context;
Expand Down Expand Up @@ -94,11 +95,10 @@ impl CanisterBuilder for RustBuilder {
"Executing: cargo build --target wasm32-unknown-unknown --release -p {} --locked",
package
);
let mut output = Err(anyhow!("uninitialized"));
env.with_suspend_all_spinners(Box::new(|| {
output = cargo.output().context("Failed to run 'cargo build'. You might need to run `cargo update` (or a similar command like `cargo vendor`) if you have updated `Cargo.toml`, because `dfx build` uses the --locked flag with Cargo.");
}));
let output = output?;

let output = with_suspend_all_spinners(env, || {
cargo.output().context("Failed to run 'cargo build'. You might need to run `cargo update` (or a similar command like `cargo vendor`) if you have updated `Cargo.toml`, because `dfx build` uses the --locked flag with Cargo.")
})?;

if !output.status.success() {
bail!("Failed to compile the rust package: {}", package);
Expand Down
12 changes: 5 additions & 7 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::lib::error::{BuildError, DfxError, DfxResult};
use crate::lib::metadata::dfx::DfxMetadata;
use crate::lib::metadata::names::{CANDID_ARGS, CANDID_SERVICE, DFX};
use crate::lib::wasm::file::{compress_bytes, read_wasm_module};
use crate::util::assets;
use crate::util::{assets, with_suspend_all_spinners};
use anyhow::{anyhow, bail, Context};
use candid::Principal as CanisterId;
use candid_parser::utils::CandidSource;
Expand Down Expand Up @@ -901,16 +901,14 @@ impl CanisterPool {
self.logger,
"Checking for vulnerabilities in rust canisters."
);
let mut out = Err(anyhow!("uninitialized"));
env.with_suspend_all_spinners(Box::new(|| {
out = Command::new("cargo")
let out = with_suspend_all_spinners(env, || {
Command::new("cargo")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("audit")
.output()
.context("Failed to run 'cargo audit'.");
}));
let out = out?;
.context("Failed to run 'cargo audit'.")
})?;
if out.status.success() {
info!(self.logger, "Audit found no vulnerabilities.")
} else {
Expand Down
53 changes: 22 additions & 31 deletions src/dfx/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,39 +240,27 @@ pub fn blob_from_arguments(
map.insert("principal".to_string(), principals);
ctx.set_completion(map);
}
let mut args = Err(anyhow!("uninitialized"));
dfx_env.with_suspend_all_spinners(Box::new(|| {
return with_suspend_all_spinners(dfx_env, || {
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!(
"Sending the following argument:\n{}\n",
args.as_ref().unwrap()
);
let args = input_args(&ctx, &func.args)?;
eprintln!("Sending the following argument:\n{}\n", args);
if is_init_arg {
eprintln!(
"Do you want to initialize the canister with this argument? [y/N]"
);
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;
}
stdin().read_line(&mut input)?;
if !["y", "Y", "yes", "Yes", "YES"].contains(&input.trim()) {
args = Err(error_invalid_data!("User cancelled."));
return Err(error_invalid_data!("User cancelled."));
}
}));
let args = args?;
args.to_bytes_with_types(env, &func.args)
let bytes = args.to_bytes_with_types(env, &func.args)?;
Ok(bytes)
})
} else {
return Err(error_invalid_data!("Expected arguments but found none."));
}
Expand Down Expand Up @@ -441,23 +429,26 @@ async fn attempt_download(client: &Client, url: &Url) -> DfxResult<Option<Bytes>
}

pub fn ask_for_consent(env: &dyn Environment, message: &str) -> Result<(), UserConsent> {
let mut ans = Ok(());
env.with_suspend_all_spinners(Box::new(|| {
with_suspend_all_spinners(env, || {
eprintln!("WARNING!");
eprintln!("{}", message);
eprintln!("Do you want to proceed? yes/No");
let mut input_string = String::new();
let res = stdin().read_line(&mut input_string);
if let Err(e) = res {
ans = Err(UserConsent::ReadError(e));
return;
}
stdin()
.read_line(&mut input_string)
.map_err(UserConsent::ReadError)?;
let input_string = input_string.trim_end().to_lowercase();
if input_string != "yes" && input_string != "y" {
ans = Err(UserConsent::Declined);
return Err(UserConsent::Declined);
}
}));
ans
Ok(())
})
}

pub fn with_suspend_all_spinners<R>(env: &dyn Environment, f: impl FnOnce() -> R) -> R {
let mut r = None;
env.with_suspend_all_spinners(Box::new(|| r = Some(f())));
r.unwrap()
}

#[cfg(test)]
Expand Down

0 comments on commit 5128f08

Please sign in to comment.