2019-05-24 01:25:55 -07:00
|
|
|
use crate::common::*;
|
|
|
|
|
2020-01-07 18:05:48 -08:00
|
|
|
pub(crate) struct Env {
|
2019-05-24 01:25:55 -07:00
|
|
|
args: Vec<String>,
|
|
|
|
dir: Box<dyn AsRef<Path>>,
|
2020-01-04 18:58:42 -08:00
|
|
|
pub(crate) err: Box<dyn Write>,
|
2020-01-15 23:37:12 -08:00
|
|
|
pub(crate) out: Box<dyn Write>,
|
|
|
|
err_style: Style,
|
2019-05-24 01:25:55 -07:00
|
|
|
}
|
|
|
|
|
2020-01-07 18:05:48 -08:00
|
|
|
impl Env {
|
2020-01-04 18:58:42 -08:00
|
|
|
pub(crate) fn main() -> Self {
|
2019-05-24 01:25:55 -07:00
|
|
|
let dir = match env::current_dir() {
|
|
|
|
Ok(dir) => dir,
|
|
|
|
Err(error) => panic!("Failed to get current directory: {}", error),
|
|
|
|
};
|
|
|
|
|
2020-01-15 23:37:12 -08:00
|
|
|
let err_style = if env::var_os("NO_COLOR").is_some()
|
|
|
|
|| env::var_os("TERM").as_deref() == Some(OsStr::new("dumb"))
|
|
|
|
|| !atty::is(atty::Stream::Stderr)
|
|
|
|
{
|
|
|
|
Style::inactive()
|
|
|
|
} else {
|
|
|
|
Style::active()
|
|
|
|
};
|
|
|
|
|
|
|
|
Self::new(dir, io::stdout(), io::stderr(), err_style, env::args())
|
2019-05-24 01:25:55 -07:00
|
|
|
}
|
|
|
|
|
2020-01-04 18:58:42 -08:00
|
|
|
pub(crate) fn run(&mut self) -> Result<(), Error> {
|
2020-01-15 23:37:12 -08:00
|
|
|
#[cfg(windows)]
|
|
|
|
ansi_term::enable_ansi_support().ok();
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
env_logger::Builder::from_env(
|
|
|
|
env_logger::Env::new()
|
|
|
|
.filter("JUST_LOG")
|
|
|
|
.write_style("JUST_LOG_STYLE"),
|
|
|
|
)
|
|
|
|
.init();
|
|
|
|
|
|
|
|
let opt = Opt::from_iter_safe(&self.args)?;
|
|
|
|
|
|
|
|
match opt.use_color {
|
|
|
|
UseColor::Always => self.err_style = Style::active(),
|
|
|
|
UseColor::Auto => {}
|
|
|
|
UseColor::Never => self.err_style = Style::inactive(),
|
|
|
|
}
|
|
|
|
|
|
|
|
opt.run(self)
|
2019-05-24 01:25:55 -07:00
|
|
|
}
|
|
|
|
|
2020-01-15 23:37:12 -08:00
|
|
|
pub(crate) fn new<D, O, E, S, I>(dir: D, out: O, err: E, err_style: Style, args: I) -> Self
|
2019-05-24 01:25:55 -07:00
|
|
|
where
|
|
|
|
D: AsRef<Path> + 'static,
|
2020-01-14 00:52:27 -08:00
|
|
|
O: Write + 'static,
|
2019-05-24 01:25:55 -07:00
|
|
|
E: Write + 'static,
|
|
|
|
S: Into<String>,
|
|
|
|
I: IntoIterator<Item = S>,
|
|
|
|
{
|
2020-01-04 18:58:42 -08:00
|
|
|
Self {
|
|
|
|
args: args.into_iter().map(Into::into).collect(),
|
2019-05-24 01:25:55 -07:00
|
|
|
dir: Box::new(dir),
|
|
|
|
err: Box::new(err),
|
2020-01-15 23:37:12 -08:00
|
|
|
out: Box::new(out),
|
|
|
|
err_style,
|
2019-05-24 01:25:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn status(&mut self) -> Result<(), i32> {
|
2020-01-14 00:52:27 -08:00
|
|
|
use structopt::clap::ErrorKind;
|
2020-01-15 23:37:12 -08:00
|
|
|
|
2019-05-24 01:25:55 -07:00
|
|
|
if let Err(error) = self.run() {
|
2020-01-14 00:52:27 -08:00
|
|
|
if let Error::Clap { source } = error {
|
2020-01-15 23:37:12 -08:00
|
|
|
if source.use_stderr() {
|
|
|
|
write!(&mut self.err, "{}", source).ok();
|
|
|
|
} else {
|
|
|
|
write!(&mut self.out, "{}", source).ok();
|
|
|
|
}
|
2020-01-14 00:52:27 -08:00
|
|
|
match source.kind {
|
|
|
|
ErrorKind::VersionDisplayed | ErrorKind::HelpDisplayed => Ok(()),
|
|
|
|
_ => Err(EXIT_FAILURE),
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-15 23:37:12 -08:00
|
|
|
writeln!(
|
|
|
|
&mut self.err,
|
|
|
|
"{}{}: {}{}",
|
|
|
|
self.err_style.error().paint("error"),
|
|
|
|
self.err_style.message().prefix(),
|
|
|
|
error,
|
|
|
|
self.err_style.message().suffix(),
|
|
|
|
)
|
|
|
|
.ok();
|
2020-01-14 00:52:27 -08:00
|
|
|
Err(EXIT_FAILURE)
|
|
|
|
}
|
2019-05-24 01:25:55 -07:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn resolve(&self, path: impl AsRef<Path>) -> PathBuf {
|
|
|
|
self.dir.as_ref().as_ref().join(path).clean()
|
|
|
|
}
|
|
|
|
}
|
2020-01-14 00:52:27 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_message_on_stdout() {
|
|
|
|
let mut env = testing::env(
|
|
|
|
["torrent", "create", "--input", "foo", "--announce", "bar"]
|
|
|
|
.iter()
|
|
|
|
.cloned(),
|
|
|
|
);
|
|
|
|
fs::write(env.resolve("foo"), "").unwrap();
|
|
|
|
env.status().ok();
|
|
|
|
let err = env.err();
|
|
|
|
if !err.starts_with("error: Failed to parse announce URL:") {
|
|
|
|
panic!("Unexpected standard error output: {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(env.out(), "");
|
|
|
|
}
|
|
|
|
}
|