mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-05-04 19:20:03 +00:00
Split crates and clean up structure of codebase (#294)
Co-authored-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
bf700f9012
commit
4b00f5f143
88 changed files with 2989 additions and 1423 deletions
252
crates/bin/src/args.rs
Normal file
252
crates/bin/src/args.rs
Normal file
|
@ -0,0 +1,252 @@
|
|||
use std::{ffi::OsString, path::PathBuf};
|
||||
|
||||
use binstall::{
|
||||
errors::BinstallError,
|
||||
manifests::cargo_toml_binstall::PkgFmt,
|
||||
ops::resolve::{CrateName, VersionReqExt},
|
||||
};
|
||||
use clap::{builder::PossibleValue, AppSettings, ArgEnum, Parser};
|
||||
use log::LevelFilter;
|
||||
use reqwest::tls::Version;
|
||||
use semver::VersionReq;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[clap(version, about = "Install a Rust binary... from binaries!", setting = AppSettings::ArgRequiredElseHelp)]
|
||||
pub struct Args {
|
||||
/// Packages to install.
|
||||
///
|
||||
/// Syntax: crate[@version]
|
||||
///
|
||||
/// Each value is either a crate name alone, or a crate name followed by @ and the version to
|
||||
/// install. The version syntax is as with the --version option.
|
||||
///
|
||||
/// When multiple names are provided, the --version option and any override options are
|
||||
/// unavailable due to ambiguity.
|
||||
///
|
||||
/// If duplicate names are provided, the last one (and their version requirement)
|
||||
/// is kept.
|
||||
#[clap(
|
||||
help_heading = "Package selection",
|
||||
value_name = "crate[@version]",
|
||||
required_unless_present_any = ["version", "help"],
|
||||
)]
|
||||
pub crate_names: Vec<CrateName>,
|
||||
|
||||
/// Package version to install.
|
||||
///
|
||||
/// Takes either an exact semver version or a semver version requirement expression, which will
|
||||
/// be resolved to the highest matching version available.
|
||||
///
|
||||
/// Cannot be used when multiple packages are installed at once, use the attached version
|
||||
/// syntax in that case.
|
||||
#[clap(help_heading = "Package selection", long = "version", parse(try_from_str = VersionReq::parse_from_cli))]
|
||||
pub version_req: Option<VersionReq>,
|
||||
|
||||
/// Override binary target set.
|
||||
///
|
||||
/// Binstall is able to look for binaries for several targets, installing the first one it finds
|
||||
/// in the order the targets were given. For example, on a 64-bit glibc Linux distribution, the
|
||||
/// default is to look first for a `x86_64-unknown-linux-gnu` binary, then for a
|
||||
/// `x86_64-unknown-linux-musl` binary. However, on a musl system, the gnu version will not be
|
||||
/// considered.
|
||||
///
|
||||
/// This option takes a comma-separated list of target triples, which will be tried in order.
|
||||
/// They override the default list, which is detected automatically from the current platform.
|
||||
///
|
||||
/// If falling back to installing from source, the first target will be used.
|
||||
#[clap(
|
||||
help_heading = "Package selection",
|
||||
alias = "target",
|
||||
long,
|
||||
value_name = "TRIPLE"
|
||||
)]
|
||||
pub targets: Option<String>,
|
||||
|
||||
/// Override Cargo.toml package manifest path.
|
||||
///
|
||||
/// This skips searching crates.io for a manifest and uses the specified path directly, useful
|
||||
/// for debugging and when adding Binstall support. This may be either the path to the folder
|
||||
/// containing a Cargo.toml file, or the Cargo.toml file itself.
|
||||
#[clap(help_heading = "Overrides", long)]
|
||||
pub manifest_path: Option<PathBuf>,
|
||||
|
||||
/// Override Cargo.toml package manifest bin-dir.
|
||||
#[clap(help_heading = "Overrides", long)]
|
||||
pub bin_dir: Option<String>,
|
||||
|
||||
/// Override Cargo.toml package manifest pkg-fmt.
|
||||
#[clap(help_heading = "Overrides", long)]
|
||||
pub pkg_fmt: Option<PkgFmt>,
|
||||
|
||||
/// Override Cargo.toml package manifest pkg-url.
|
||||
#[clap(help_heading = "Overrides", long)]
|
||||
pub pkg_url: Option<String>,
|
||||
|
||||
/// Disable symlinking / versioned updates.
|
||||
///
|
||||
/// By default, Binstall will install a binary named `<name>-<version>` in the install path, and
|
||||
/// either symlink or copy it to (depending on platform) the plain binary name. This makes it
|
||||
/// possible to have multiple versions of the same binary, for example for testing or rollback.
|
||||
///
|
||||
/// Pass this flag to disable this behavior.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub no_symlinks: bool,
|
||||
|
||||
/// Dry run, fetch and show changes without installing binaries.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub dry_run: bool,
|
||||
|
||||
/// Disable interactive mode / confirmation prompts.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub no_confirm: bool,
|
||||
|
||||
/// Do not cleanup temporary files.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub no_cleanup: bool,
|
||||
|
||||
/// Install binaries in a custom location.
|
||||
///
|
||||
/// By default, binaries are installed to the global location `$CARGO_HOME/bin`, and global
|
||||
/// metadata files are updated with the package information. Specifying another path here
|
||||
/// switches over to a "local" install, where binaries are installed at the path given, and the
|
||||
/// global metadata files are not updated.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub install_path: Option<PathBuf>,
|
||||
|
||||
/// Enforce downloads over secure transports only.
|
||||
///
|
||||
/// Insecure HTTP downloads will be removed completely in the future; in the meantime this
|
||||
/// option forces a fail when the remote endpoint uses plaintext HTTP or insecure TLS suites.
|
||||
///
|
||||
/// Without this option, plain HTTP will warn.
|
||||
///
|
||||
/// Implies `--min-tls-version=1.2`.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub secure: bool,
|
||||
|
||||
/// Force a crate to be installed even if it is already installed.
|
||||
#[clap(help_heading = "Options", long)]
|
||||
pub force: bool,
|
||||
|
||||
/// Require a minimum TLS version from remote endpoints.
|
||||
///
|
||||
/// The default is not to require any minimum TLS version, and use the negotiated highest
|
||||
/// version available to both this client and the remote server.
|
||||
#[clap(help_heading = "Options", long, arg_enum, value_name = "VERSION")]
|
||||
pub min_tls_version: Option<TLSVersion>,
|
||||
|
||||
/// Print help information
|
||||
#[clap(help_heading = "Meta", short, long)]
|
||||
pub help: bool,
|
||||
|
||||
/// Print version information
|
||||
#[clap(help_heading = "Meta", short = 'V')]
|
||||
pub version: bool,
|
||||
|
||||
/// Utility log level
|
||||
///
|
||||
/// Set to `trace` to print very low priority, often extremely
|
||||
/// verbose information.
|
||||
///
|
||||
/// Set to `debug` when submitting a bug report.
|
||||
///
|
||||
/// Set to `info` to only print useful information.
|
||||
///
|
||||
/// Set to `warn` to only print on hazardous situations.
|
||||
///
|
||||
/// Set to `error` to only print serious errors.
|
||||
///
|
||||
/// Set to `off` to disable logging completely, this will also
|
||||
/// disable output from `cargo-install`.
|
||||
#[clap(
|
||||
help_heading = "Meta",
|
||||
long,
|
||||
default_value = "info",
|
||||
value_name = "LEVEL",
|
||||
possible_values = [
|
||||
PossibleValue::new("trace").help(
|
||||
"Set to `trace` to print very low priority, often extremely verbose information."
|
||||
),
|
||||
PossibleValue::new("debug").help("Set to debug when submitting a bug report."),
|
||||
PossibleValue::new("info").help("Set to info to only print useful information."),
|
||||
PossibleValue::new("warn").help("Set to warn to only print on hazardous situations."),
|
||||
PossibleValue::new("error").help("Set to error to only print serious errors."),
|
||||
PossibleValue::new("off").help(
|
||||
"Set to off to disable logging completely, this will also disable output from `cargo-install`."
|
||||
),
|
||||
]
|
||||
)]
|
||||
pub log_level: LevelFilter,
|
||||
|
||||
/// Equivalent to setting `log_level` to `off`.
|
||||
///
|
||||
/// This would override the `log_level`.
|
||||
#[clap(help_heading = "Meta", short, long)]
|
||||
pub quiet: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, ArgEnum)]
|
||||
pub enum TLSVersion {
|
||||
#[clap(name = "1.2")]
|
||||
Tls1_2,
|
||||
#[clap(name = "1.3")]
|
||||
Tls1_3,
|
||||
}
|
||||
|
||||
impl From<TLSVersion> for Version {
|
||||
fn from(ver: TLSVersion) -> Self {
|
||||
match ver {
|
||||
TLSVersion::Tls1_2 => Version::TLS_1_2,
|
||||
TLSVersion::Tls1_3 => Version::TLS_1_3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse() -> Result<Args, BinstallError> {
|
||||
// Filter extraneous arg when invoked by cargo
|
||||
// `cargo run -- --help` gives ["target/debug/cargo-binstall", "--help"]
|
||||
// `cargo binstall --help` gives ["/home/ryan/.cargo/bin/cargo-binstall", "binstall", "--help"]
|
||||
let mut args: Vec<OsString> = std::env::args_os().collect();
|
||||
let args = if args.len() > 1 && args[1] == "binstall" {
|
||||
// Equivalent to
|
||||
//
|
||||
// args.remove(1);
|
||||
//
|
||||
// But is O(1)
|
||||
args.swap(0, 1);
|
||||
let mut args = args.into_iter();
|
||||
drop(args.next().unwrap());
|
||||
|
||||
args
|
||||
} else {
|
||||
args.into_iter()
|
||||
};
|
||||
|
||||
// Load options
|
||||
let mut opts = Args::parse_from(args);
|
||||
if opts.quiet {
|
||||
opts.log_level = LevelFilter::Off;
|
||||
}
|
||||
|
||||
if opts.crate_names.len() > 1 {
|
||||
let option = if opts.version_req.is_some() {
|
||||
"version"
|
||||
} else if opts.manifest_path.is_some() {
|
||||
"manifest-path"
|
||||
} else if opts.bin_dir.is_some() {
|
||||
"bin-dir"
|
||||
} else if opts.pkg_fmt.is_some() {
|
||||
"pkg-fmt"
|
||||
} else if opts.pkg_url.is_some() {
|
||||
"pkg-url"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
if !option.is_empty() {
|
||||
return Err(BinstallError::OverrideOptionUsedWithMultiInstall { option }.into());
|
||||
}
|
||||
}
|
||||
|
||||
Ok(opts)
|
||||
}
|
237
crates/bin/src/entry.rs
Normal file
237
crates/bin/src/entry.rs
Normal file
|
@ -0,0 +1,237 @@
|
|||
use std::{fs, path::Path, sync::Arc, time::Duration};
|
||||
|
||||
use binstall::{
|
||||
errors::BinstallError,
|
||||
helpers::{
|
||||
jobserver_client::LazyJobserverClient, remote::create_reqwest_client,
|
||||
tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
manifests::{
|
||||
binstall_crates_v1::Records, cargo_crates_v1::CratesToml, cargo_toml_binstall::PkgOverride,
|
||||
},
|
||||
ops::{
|
||||
self,
|
||||
resolve::{CrateName, Resolution, VersionReqExt},
|
||||
},
|
||||
targets::get_desired_targets,
|
||||
};
|
||||
use log::{debug, error, info, warn, LevelFilter};
|
||||
use miette::{miette, Result, WrapErr};
|
||||
use tokio::task::block_in_place;
|
||||
|
||||
use crate::{args::Args, install_path, ui::UIThread};
|
||||
|
||||
pub async fn install_crates(mut args: Args, jobserver_client: LazyJobserverClient) -> Result<()> {
|
||||
let cli_overrides = PkgOverride {
|
||||
pkg_url: args.pkg_url.take(),
|
||||
pkg_fmt: args.pkg_fmt.take(),
|
||||
bin_dir: args.bin_dir.take(),
|
||||
};
|
||||
|
||||
// Launch target detection
|
||||
let desired_targets = get_desired_targets(&args.targets);
|
||||
|
||||
// Initialize reqwest client
|
||||
let client = create_reqwest_client(args.secure, args.min_tls_version.map(|v| v.into()))?;
|
||||
|
||||
// Build crates.io api client
|
||||
let crates_io_api_client = crates_io_api::AsyncClient::new(
|
||||
"cargo-binstall (https://github.com/ryankurte/cargo-binstall)",
|
||||
Duration::from_millis(100),
|
||||
)
|
||||
.expect("bug: invalid user agent");
|
||||
|
||||
// Initialize UI thread
|
||||
let mut uithread = UIThread::new(!args.no_confirm);
|
||||
|
||||
let (install_path, metadata, temp_dir) = block_in_place(|| -> Result<_> {
|
||||
// Compute install directory
|
||||
let (install_path, custom_install_path) =
|
||||
install_path::get_install_path(args.install_path.as_deref());
|
||||
let install_path = install_path.ok_or_else(|| {
|
||||
error!("No viable install path found of specified, try `--install-path`");
|
||||
miette!("No install path found or specified")
|
||||
})?;
|
||||
fs::create_dir_all(&install_path).map_err(BinstallError::Io)?;
|
||||
debug!("Using install path: {}", install_path.display());
|
||||
|
||||
// Load metadata
|
||||
let metadata = if !custom_install_path {
|
||||
debug!("Reading binstall/crates-v1.json");
|
||||
Some(Records::load()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Create a temporary directory for downloads etc.
|
||||
//
|
||||
// Put all binaries to a temporary directory under `dst` first, catching
|
||||
// some failure modes (e.g., out of space) before touching the existing
|
||||
// binaries. This directory will get cleaned up via RAII.
|
||||
let temp_dir = tempfile::Builder::new()
|
||||
.prefix("cargo-binstall")
|
||||
.tempdir_in(&install_path)
|
||||
.map_err(BinstallError::from)
|
||||
.wrap_err("Creating a temporary directory failed.")?;
|
||||
|
||||
Ok((install_path, metadata, temp_dir))
|
||||
})?;
|
||||
|
||||
// Remove installed crates
|
||||
let crate_names = CrateName::dedup(&args.crate_names)
|
||||
.filter_map(|crate_name| {
|
||||
match (
|
||||
args.force,
|
||||
metadata.as_ref().and_then(|records| records.get(&crate_name.name)),
|
||||
&crate_name.version_req,
|
||||
) {
|
||||
(false, Some(metadata), Some(version_req))
|
||||
if version_req.is_latest_compatible(&metadata.current_version) =>
|
||||
{
|
||||
debug!("Bailing out early because we can assume wanted is already installed from metafile");
|
||||
info!(
|
||||
"{} v{} is already installed, use --force to override",
|
||||
crate_name.name, metadata.current_version
|
||||
);
|
||||
None
|
||||
}
|
||||
|
||||
// we have to assume that the version req could be *,
|
||||
// and therefore a remote upgraded version could exist
|
||||
(false, Some(metadata), _) => {
|
||||
Some((crate_name, Some(metadata.current_version.clone())))
|
||||
}
|
||||
|
||||
_ => Some((crate_name, None)),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
if crate_names.is_empty() {
|
||||
debug!("Nothing to do");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let temp_dir_path: Arc<Path> = Arc::from(temp_dir.path());
|
||||
|
||||
// Create binstall_opts
|
||||
let binstall_opts = Arc::new(ops::Options {
|
||||
no_symlinks: args.no_symlinks,
|
||||
dry_run: args.dry_run,
|
||||
force: args.force,
|
||||
version_req: args.version_req.take(),
|
||||
manifest_path: args.manifest_path.take(),
|
||||
cli_overrides,
|
||||
desired_targets,
|
||||
quiet: args.log_level == LevelFilter::Off,
|
||||
});
|
||||
|
||||
let tasks: Vec<_> = if !args.dry_run && !args.no_confirm {
|
||||
// Resolve crates
|
||||
let tasks: Vec<_> = crate_names
|
||||
.into_iter()
|
||||
.map(|(crate_name, current_version)| {
|
||||
AutoAbortJoinHandle::spawn(ops::resolve::resolve(
|
||||
binstall_opts.clone(),
|
||||
crate_name,
|
||||
current_version,
|
||||
temp_dir_path.clone(),
|
||||
install_path.clone(),
|
||||
client.clone(),
|
||||
crates_io_api_client.clone(),
|
||||
))
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Confirm
|
||||
let mut resolutions = Vec::with_capacity(tasks.len());
|
||||
for task in tasks {
|
||||
match task.await?? {
|
||||
Resolution::AlreadyUpToDate => {}
|
||||
res => resolutions.push(res),
|
||||
}
|
||||
}
|
||||
|
||||
if resolutions.is_empty() {
|
||||
debug!("Nothing to do");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
uithread.confirm().await?;
|
||||
|
||||
// Install
|
||||
resolutions
|
||||
.into_iter()
|
||||
.map(|resolution| {
|
||||
AutoAbortJoinHandle::spawn(ops::install::install(
|
||||
resolution,
|
||||
binstall_opts.clone(),
|
||||
jobserver_client.clone(),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
// Resolve crates and install without confirmation
|
||||
crate_names
|
||||
.into_iter()
|
||||
.map(|(crate_name, current_version)| {
|
||||
let opts = binstall_opts.clone();
|
||||
let temp_dir_path = temp_dir_path.clone();
|
||||
let jobserver_client = jobserver_client.clone();
|
||||
let client = client.clone();
|
||||
let crates_io_api_client = crates_io_api_client.clone();
|
||||
let install_path = install_path.clone();
|
||||
|
||||
AutoAbortJoinHandle::spawn(async move {
|
||||
let resolution = ops::resolve::resolve(
|
||||
opts.clone(),
|
||||
crate_name,
|
||||
current_version,
|
||||
temp_dir_path,
|
||||
install_path,
|
||||
client,
|
||||
crates_io_api_client,
|
||||
)
|
||||
.await?;
|
||||
|
||||
ops::install::install(resolution, opts, jobserver_client).await
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
};
|
||||
|
||||
let mut metadata_vec = Vec::with_capacity(tasks.len());
|
||||
for task in tasks {
|
||||
if let Some(metadata) = task.await?? {
|
||||
metadata_vec.push(metadata);
|
||||
}
|
||||
}
|
||||
|
||||
block_in_place(|| {
|
||||
if let Some(mut records) = metadata {
|
||||
// If using standardised install path,
|
||||
// then create_dir_all(&install_path) would also
|
||||
// create .cargo.
|
||||
|
||||
debug!("Writing .crates.toml");
|
||||
CratesToml::append(metadata_vec.iter())?;
|
||||
|
||||
debug!("Writing binstall/crates-v1.json");
|
||||
for metadata in metadata_vec {
|
||||
records.replace(metadata);
|
||||
}
|
||||
records.overwrite()?;
|
||||
}
|
||||
|
||||
if args.no_cleanup {
|
||||
// Consume temp_dir without removing it from fs.
|
||||
temp_dir.into_path();
|
||||
} else {
|
||||
temp_dir.close().unwrap_or_else(|err| {
|
||||
warn!("Failed to clean up some resources: {err}");
|
||||
});
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
39
crates/bin/src/install_path.rs
Normal file
39
crates/bin/src/install_path.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use binstall::helpers::statics::cargo_home;
|
||||
use log::debug;
|
||||
|
||||
/// Fetch install path from environment
|
||||
/// roughly follows <https://doc.rust-lang.org/cargo/commands/cargo-install.html#description>
|
||||
///
|
||||
/// Return (install_path, is_custom_install_path)
|
||||
pub fn get_install_path<P: AsRef<Path>>(install_path: Option<P>) -> (Option<Arc<Path>>, bool) {
|
||||
// Command line override first first
|
||||
if let Some(p) = install_path {
|
||||
return (Some(Arc::from(p.as_ref())), true);
|
||||
}
|
||||
|
||||
// Environmental variables
|
||||
if let Ok(p) = std::env::var("CARGO_INSTALL_ROOT") {
|
||||
debug!("using CARGO_INSTALL_ROOT ({p})");
|
||||
let b = PathBuf::from(p);
|
||||
return (Some(Arc::from(b.join("bin"))), true);
|
||||
}
|
||||
|
||||
if let Ok(p) = cargo_home() {
|
||||
debug!("using ({}) as cargo home", p.display());
|
||||
return (Some(p.join("bin").into()), false);
|
||||
}
|
||||
|
||||
// Local executable dir if no cargo is found
|
||||
let dir = dirs::executable_dir();
|
||||
|
||||
if let Some(d) = &dir {
|
||||
debug!("Fallback to {}", d.display());
|
||||
}
|
||||
|
||||
(dir.map(Arc::from), true)
|
||||
}
|
78
crates/bin/src/main.rs
Normal file
78
crates/bin/src/main.rs
Normal file
|
@ -0,0 +1,78 @@
|
|||
use std::{
|
||||
process::{ExitCode, Termination},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use binstall::{
|
||||
errors::BinstallError,
|
||||
helpers::{
|
||||
jobserver_client::LazyJobserverClient, signal::cancel_on_user_sig_term,
|
||||
tasks::AutoAbortJoinHandle,
|
||||
},
|
||||
};
|
||||
use log::{debug, error, info};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
mod args;
|
||||
mod entry;
|
||||
mod install_path;
|
||||
mod ui;
|
||||
|
||||
#[cfg(feature = "mimalloc")]
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
fn main() -> MainExit {
|
||||
// This must be the very first thing to happen
|
||||
let jobserver_client = LazyJobserverClient::new();
|
||||
|
||||
let args = match args::parse() {
|
||||
Ok(args) => args,
|
||||
Err(err) => return MainExit::Error(err),
|
||||
};
|
||||
|
||||
ui::logging(&args);
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let result = {
|
||||
let rt = Runtime::new().unwrap();
|
||||
let handle =
|
||||
AutoAbortJoinHandle::new(rt.spawn(entry::install_crates(args, jobserver_client)));
|
||||
rt.block_on(cancel_on_user_sig_term(handle))
|
||||
};
|
||||
|
||||
let done = start.elapsed();
|
||||
debug!("run time: {done:?}");
|
||||
|
||||
result.map_or_else(MainExit::Error, |res| {
|
||||
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
|
||||
err.downcast::<BinstallError>()
|
||||
.map(MainExit::Error)
|
||||
.unwrap_or_else(MainExit::Report)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
enum MainExit {
|
||||
Success(Duration),
|
||||
Error(BinstallError),
|
||||
Report(miette::Report),
|
||||
}
|
||||
|
||||
impl Termination for MainExit {
|
||||
fn report(self) -> ExitCode {
|
||||
match self {
|
||||
Self::Success(spent) => {
|
||||
info!("Done in {spent:?}");
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Self::Error(err) => err.report(),
|
||||
Self::Report(err) => {
|
||||
error!("Fatal error:");
|
||||
eprintln!("{err:?}");
|
||||
ExitCode::from(16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
119
crates/bin/src/ui.rs
Normal file
119
crates/bin/src/ui.rs
Normal file
|
@ -0,0 +1,119 @@
|
|||
use std::{
|
||||
io::{self, BufRead, Write},
|
||||
thread,
|
||||
};
|
||||
|
||||
use log::LevelFilter;
|
||||
use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use binstall::errors::BinstallError;
|
||||
|
||||
use crate::args::Args;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct UIThreadInner {
|
||||
/// Request for confirmation
|
||||
request_tx: mpsc::Sender<()>,
|
||||
|
||||
/// Confirmation
|
||||
confirm_rx: mpsc::Receiver<Result<(), BinstallError>>,
|
||||
}
|
||||
|
||||
impl UIThreadInner {
|
||||
fn new() -> Self {
|
||||
let (request_tx, mut request_rx) = mpsc::channel(1);
|
||||
let (confirm_tx, confirm_rx) = mpsc::channel(10);
|
||||
|
||||
thread::spawn(move || {
|
||||
// This task should be the only one able to
|
||||
// access stdin
|
||||
let mut stdin = io::stdin().lock();
|
||||
let mut input = String::with_capacity(16);
|
||||
|
||||
loop {
|
||||
if request_rx.blocking_recv().is_none() {
|
||||
break;
|
||||
}
|
||||
|
||||
let res = loop {
|
||||
{
|
||||
let mut stdout = io::stdout().lock();
|
||||
|
||||
writeln!(&mut stdout, "Do you wish to continue? yes/[no]").unwrap();
|
||||
write!(&mut stdout, "? ").unwrap();
|
||||
stdout.flush().unwrap();
|
||||
}
|
||||
|
||||
input.clear();
|
||||
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,
|
||||
}
|
||||
};
|
||||
|
||||
confirm_tx
|
||||
.blocking_send(res)
|
||||
.expect("entry exits when confirming request");
|
||||
}
|
||||
});
|
||||
|
||||
Self {
|
||||
request_tx,
|
||||
confirm_rx,
|
||||
}
|
||||
}
|
||||
|
||||
async fn confirm(&mut self) -> Result<(), BinstallError> {
|
||||
self.request_tx
|
||||
.send(())
|
||||
.await
|
||||
.map_err(|_| BinstallError::UserAbort)?;
|
||||
|
||||
self.confirm_rx
|
||||
.recv()
|
||||
.await
|
||||
.unwrap_or(Err(BinstallError::UserAbort))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UIThread(Option<UIThreadInner>);
|
||||
|
||||
impl UIThread {
|
||||
/// * `enable` - `true` to enable confirmation, `false` to disable it.
|
||||
pub fn new(enable: bool) -> Self {
|
||||
Self(if enable {
|
||||
Some(UIThreadInner::new())
|
||||
} else {
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn confirm(&mut self) -> Result<(), BinstallError> {
|
||||
if let Some(inner) = self.0.as_mut() {
|
||||
inner.confirm().await
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn logging(args: &Args) {
|
||||
// Setup logging
|
||||
let mut log_config = ConfigBuilder::new();
|
||||
log_config.add_filter_ignore("hyper".to_string());
|
||||
log_config.add_filter_ignore("reqwest".to_string());
|
||||
log_config.add_filter_ignore("rustls".to_string());
|
||||
log_config.set_location_level(LevelFilter::Off);
|
||||
TermLogger::init(
|
||||
args.log_level,
|
||||
log_config.build(),
|
||||
TerminalMode::Mixed,
|
||||
ColorChoice::Auto,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue