mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58:42 +00:00
Refactor: Extract confirm
into a new mod
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
f53680c497
commit
c1809d41fa
2 changed files with 26 additions and 18 deletions
|
@ -1,10 +1,11 @@
|
|||
use std::{
|
||||
borrow::Cow,
|
||||
io::{stderr, stdin, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use cargo_toml::Manifest;
|
||||
use log::{debug, info};
|
||||
use log::debug;
|
||||
use reqwest::Method;
|
||||
use serde::Serialize;
|
||||
use tinytemplate::TinyTemplate;
|
||||
|
@ -18,6 +19,9 @@ pub use async_extracter::extract_archive_stream;
|
|||
mod auto_abort_join_handle;
|
||||
pub use auto_abort_join_handle::AutoAbortJoinHandle;
|
||||
|
||||
mod confirm;
|
||||
pub use confirm::confirm;
|
||||
|
||||
mod extracter;
|
||||
mod readable_rx;
|
||||
|
||||
|
@ -129,23 +133,6 @@ pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> Option<PathB
|
|||
dir
|
||||
}
|
||||
|
||||
pub fn confirm() -> Result<(), BinstallError> {
|
||||
loop {
|
||||
info!("Do you wish to continue? yes/[no]");
|
||||
eprint!("? ");
|
||||
stderr().flush().ok();
|
||||
|
||||
let mut input = String::new();
|
||||
stdin().read_line(&mut input).unwrap();
|
||||
|
||||
match input.as_str().trim() {
|
||||
"yes" | "y" | "YES" | "Y" => break Ok(()),
|
||||
"no" | "n" | "NO" | "N" | "" => break Err(BinstallError::UserAbort),
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Template: Serialize {
|
||||
fn render(&self, template: &str) -> Result<String, BinstallError>
|
||||
where
|
||||
|
|
21
src/helpers/confirm.rs
Normal file
21
src/helpers/confirm.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
use log::info;
|
||||
use std::io::{stderr, stdin, Write};
|
||||
|
||||
use crate::BinstallError;
|
||||
|
||||
pub fn confirm() -> Result<(), BinstallError> {
|
||||
loop {
|
||||
info!("Do you wish to continue? yes/[no]");
|
||||
eprint!("? ");
|
||||
stderr().flush().ok();
|
||||
|
||||
let mut input = String::new();
|
||||
stdin().read_line(&mut input).unwrap();
|
||||
|
||||
match input.as_str().trim() {
|
||||
"yes" | "y" | "YES" | "Y" => break Ok(()),
|
||||
"no" | "n" | "NO" | "N" | "" => break Err(BinstallError::UserAbort),
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue