use std::env;
use std::fs;
use std::io;
use std::path::Path;
use anyhow::{anyhow, bail, Result};
pub fn install() -> Result<()> {
do_install()?;
if cfg!(windows) {
println!("Press enter to close this window...");
let mut line = String::new();
drop(io::stdin().read_line(&mut line));
}
Ok(())
}
fn do_install() -> Result<()> {
let rustup = match which::which("rustup") {
Ok(path) => path,
Err(_) => {
bail!(
"failed to find an installation of `rustup` in `PATH`, \
is rustup already installed?"
);
}
};
let installation_dir = match rustup.parent() {
Some(parent) => parent,
None => bail!("can't install when `rustup` is at the root of the filesystem"),
};
let destination = installation_dir
.join("wrangler")
.with_extension(env::consts::EXE_EXTENSION);
if destination.exists() {
confirm_can_overwrite(&destination)?;
}
let me = env::current_exe()?;
fs::copy(&me, &destination)
.map_err(|_| anyhow!("failed to copy executable to `{}`", destination.display()))?;
println!(
"info: successfully installed wrangler to `{}`",
destination.display()
);
Ok(())
}
fn confirm_can_overwrite(dst: &Path) -> Result<()> {
if env::args().any(|arg| arg == "-f") {
return Ok(());
}
if !atty::is(atty::Stream::Stdin) {
bail!(
"existing wrangler installation found at `{}`, pass `-f` to \
force installation over this file, otherwise aborting \
installation now",
dst.display()
);
}
eprintln!(
"info: existing wrangler installation found at `{}`",
dst.display()
);
eprint!("info: would you like to overwrite this file? [y/N]: ");
let mut line = String::new();
io::stdin()
.read_line(&mut line)
.map_err(|_| anyhow!("failed to read stdin"))?;
if line.starts_with('y') || line.starts_with('Y') {
return Ok(());
}
bail!("aborting installation");
}