Use Confirmer instead of confirm in main.rs

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-06-10 13:31:50 +10:00
parent dd2fa2de33
commit 47ed7ce27b
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -191,6 +191,8 @@ async fn entry() -> Result<()> {
) )
.unwrap(); .unwrap();
let mut confirmer = Confirmer::new(!opts.no_confirm);
// Compute install directory // Compute install directory
let install_path = get_install_path(opts.install_path.as_deref()).ok_or_else(|| { let install_path = get_install_path(opts.install_path.as_deref()).ok_or_else(|| {
error!("No viable install path found of specified, try `--install-path`"); error!("No viable install path found of specified, try `--install-path`");
@ -228,8 +230,8 @@ async fn entry() -> Result<()> {
}) })
); );
if !opts.no_confirm && !opts.dry_run { if !opts.dry_run {
confirm()?; confirmer.confirm().await?;
} }
} }
@ -303,6 +305,7 @@ async fn entry() -> Result<()> {
opts, opts,
package, package,
temp_dir, temp_dir,
&mut confirmer,
) )
.await .await
} }
@ -317,7 +320,7 @@ async fn entry() -> Result<()> {
.first() .first()
.ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?; .ok_or_else(|| miette!("No viable targets found, try with `--targets`"))?;
install_from_source(opts, package, target).await install_from_source(opts, package, target, &mut confirmer).await
} }
} }
} }
@ -331,6 +334,7 @@ async fn install_from_package(
opts: Options, opts: Options,
package: Package<Meta>, package: Package<Meta>,
temp_dir: TempDir, temp_dir: TempDir,
confirmer: &mut Confirmer,
) -> Result<()> { ) -> Result<()> {
// Prompt user for third-party source // Prompt user for third-party source
if fetcher.is_third_party() { if fetcher.is_third_party() {
@ -338,8 +342,8 @@ async fn install_from_package(
"The package will be downloaded from third-party source {}", "The package will be downloaded from third-party source {}",
fetcher.source_name() fetcher.source_name()
); );
if !opts.no_confirm && !opts.dry_run { if !opts.dry_run {
confirm()?; confirmer.confirm().await?;
} }
} else { } else {
info!( info!(
@ -429,9 +433,7 @@ async fn install_from_package(
return Ok(()); return Ok(());
} }
if !opts.no_confirm { confirmer.confirm().await?;
confirm()?;
}
info!("Installing binaries..."); info!("Installing binaries...");
for file in &bin_files { for file in &bin_files {
@ -456,11 +458,16 @@ async fn install_from_package(
Ok(()) Ok(())
} }
async fn install_from_source(opts: Options, package: Package<Meta>, target: &str) -> Result<()> { async fn install_from_source(
opts: Options,
package: Package<Meta>,
target: &str,
confirmer: &mut Confirmer,
) -> Result<()> {
// Prompt user for source install // Prompt user for source install
warn!("The package will be installed from source (with cargo)",); warn!("The package will be installed from source (with cargo)",);
if !opts.no_confirm && !opts.dry_run { if !opts.dry_run {
confirm()?; confirmer.confirm().await?;
} }
if opts.dry_run { if opts.dry_run {