summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2019-06-26 08:09:09 -0700
committerAlex Crichton <alex@alexcrichton.com>2019-06-26 08:09:09 -0700
commit5ba3d18162b23b1b0391c8485ef4e2da9b121837 (patch)
treecfc7f2b8320d5660954f1d9c14f391872cb51cc7
parentc38b3418a0d2e5ca5a4c6eee11e99c5739b79d00 (diff)
downloadrust-installer-5ba3d18162b23b1b0391c8485ef4e2da9b121837.tar.gz
Move from `error-chain` to `failure`
-rw-r--r--Cargo.toml2
-rw-r--r--src/combiner.rs30
-rw-r--r--src/generator.rs14
-rw-r--r--src/lib.rs20
-rw-r--r--src/main.rs41
-rw-r--r--src/scripter.rs8
-rw-r--r--src/tarballer.rs30
-rw-r--r--src/util.rs40
8 files changed, 87 insertions, 98 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8626211..33e0091 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,7 +10,7 @@ name = "fabricate"
path = "src/main.rs"
[dependencies]
-error-chain = "0.12.0"
+failure = "0.1"
flate2 = "1.0.1"
rayon = "1.0"
tar = "0.4.13"
diff --git a/src/combiner.rs b/src/combiner.rs
index e48d6ac..35f3560 100644
--- a/src/combiner.rs
+++ b/src/combiner.rs
@@ -1,13 +1,12 @@
-use std::io::{Read, Write};
-use std::path::Path;
-
-use flate2::read::GzDecoder;
-use tar::Archive;
-
use super::Scripter;
use super::Tarballer;
-use crate::errors::*;
use crate::util::*;
+use crate::Result;
+use failure::{bail, ResultExt};
+use flate2::read::GzDecoder;
+use std::io::{Read, Write};
+use std::path::Path;
+use tar::Archive;
actor! {
#[derive(Debug)]
@@ -62,7 +61,7 @@ impl Combiner {
{
// Extract the input tarballs
let tar = GzDecoder::new(open_file(&input_tarball)?);
- Archive::new(tar).unpack(&self.work_dir).chain_err(|| {
+ Archive::new(tar).unpack(&self.work_dir).with_context(|_| {
format!(
"unable to extract '{}' into '{}'",
&input_tarball, self.work_dir
@@ -76,8 +75,8 @@ impl Combiner {
// Verify the version number.
let mut version = String::new();
open_file(pkg_dir.join("rust-installer-version"))
- .and_then(|mut file| file.read_to_string(&mut version).map_err(Error::from))
- .chain_err(|| format!("failed to read version in '{}'", input_tarball))?;
+ .and_then(|mut file| Ok(file.read_to_string(&mut version)?))
+ .with_context(|_| format!("failed to read version in '{}'", input_tarball))?;
if version.trim().parse() != Ok(crate::RUST_INSTALLER_VERSION) {
bail!("incorrect installer version in {}", input_tarball);
}
@@ -85,11 +84,8 @@ impl Combiner {
// Copy components to the new combined installer.
let mut pkg_components = String::new();
open_file(pkg_dir.join("components"))
- .and_then(|mut file| {
- file.read_to_string(&mut pkg_components)
- .map_err(Error::from)
- })
- .chain_err(|| format!("failed to read components in '{}'", input_tarball))?;
+ .and_then(|mut file| Ok(file.read_to_string(&mut pkg_components)?))
+ .with_context(|_| format!("failed to read components in '{}'", input_tarball))?;
for component in pkg_components.split_whitespace() {
// All we need to do is copy the component directory. We could
// move it, but rustbuild wants to reuse the unpacked package
@@ -100,7 +96,7 @@ impl Combiner {
// Merge the component name.
writeln!(&components, "{}", component)
- .chain_err(|| "failed to write new components")?;
+ .with_context(|_| "failed to write new components")?;
}
}
drop(components);
@@ -112,7 +108,7 @@ impl Combiner {
"{}",
crate::RUST_INSTALLER_VERSION
)
- .chain_err(|| "failed to write new installer version")?;
+ .with_context(|_| "failed to write new installer version")?;
// Copy the overlay.
if !self.non_installed_overlay.is_empty() {
diff --git a/src/generator.rs b/src/generator.rs
index 62fe8b6..3b4b0d4 100644
--- a/src/generator.rs
+++ b/src/generator.rs
@@ -1,10 +1,10 @@
-use std::io::Write;
-use std::path::Path;
-
use super::Scripter;
use super::Tarballer;
-use crate::errors::*;
use crate::util::*;
+use crate::Result;
+use failure::{format_err, bail, ResultExt};
+use std::io::Write;
+use std::path::Path;
actor! {
#[derive(Debug)]
@@ -62,7 +62,7 @@ impl Generator {
// Write the component name
let components = package_dir.join("components");
writeln!(create_new_file(components)?, "{}", self.component_name)
- .chain_err(|| "failed to write the component file")?;
+ .with_context(|_| "failed to write the component file")?;
// Write the installer version (only used by combine-installers.sh)
let version = package_dir.join("rust-installer-version");
@@ -71,7 +71,7 @@ impl Generator {
"{}",
crate::RUST_INSTALLER_VERSION
)
- .chain_err(|| "failed to write new installer version")?;
+ .with_context(|_| "failed to write new installer version")?;
// Copy the overlay
if !self.non_installed_overlay.is_empty() {
@@ -128,7 +128,7 @@ fn copy_and_manifest(src: &Path, dst: &Path, bulk_dirs: &str) -> Result<()> {
// Normalize to Unix-style path separators.
let normalized_string;
let mut string = path.to_str().ok_or_else(|| {
- format!(
+ format_err!(
"rust-installer doesn't support non-Unicode paths: {:?}",
path
)
diff --git a/src/lib.rs b/src/lib.rs
index 41c00ef..39a613a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,21 +1,4 @@
-#[macro_use]
-extern crate error_chain;
-
-#[cfg(windows)]
-extern crate winapi;
-#[cfg(windows)]
-#[macro_use]
-extern crate lazy_static;
-
-mod errors {
- error_chain! {
- foreign_links {
- Io(::std::io::Error);
- StripPrefix(::std::path::StripPrefixError);
- WalkDir(::walkdir::Error);
- }
- }
-}
+pub type Result<T> = std::result::Result<T, failure::Error>;
#[macro_use]
mod util;
@@ -29,7 +12,6 @@ mod scripter;
mod tarballer;
pub use crate::combiner::Combiner;
-pub use crate::errors::{Error, ErrorKind, Result};
pub use crate::generator::Generator;
pub use crate::scripter::Scripter;
pub use crate::tarballer::Tarballer;
diff --git a/src/main.rs b/src/main.rs
index 49ea6ba..90f1602 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,24 +1,9 @@
-#[macro_use]
-extern crate clap;
-#[macro_use]
-extern crate error_chain;
-use installer;
-
-use crate::errors::*;
use clap::{App, ArgMatches};
+use failure::ResultExt;
+use installer::Result;
-mod errors {
- error_chain! {
- links {
- Installer(::installer::Error, ::installer::ErrorKind);
- }
- }
-}
-
-quick_main!(run);
-
-fn run() -> Result<()> {
- let yaml = load_yaml!("main.yml");
+fn main() -> Result<()> {
+ let yaml = clap::load_yaml!("main.yml");
let matches = App::from_yaml(yaml).get_matches();
match matches.subcommand() {
@@ -54,7 +39,10 @@ fn combine(matches: &ArgMatches<'_>) -> Result<()> {
"output-dir" => output_dir,
});
- combiner.run().chain_err(|| "failed to combine installers")
+ combiner
+ .run()
+ .with_context(|_| "failed to combine installers")?;
+ Ok(())
}
fn generate(matches: &ArgMatches<'_>) -> Result<()> {
@@ -72,7 +60,10 @@ fn generate(matches: &ArgMatches<'_>) -> Result<()> {
"output-dir" => output_dir,
});
- generator.run().chain_err(|| "failed to generate installer")
+ generator
+ .run()
+ .with_context(|_| "failed to generate installer")?;
+ Ok(())
}
fn script(matches: &ArgMatches<'_>) -> Result<()> {
@@ -86,7 +77,8 @@ fn script(matches: &ArgMatches<'_>) -> Result<()> {
scripter
.run()
- .chain_err(|| "failed to generate installation script")
+ .with_context(|_| "failed to generate installation script")?;
+ Ok(())
}
fn tarball(matches: &ArgMatches<'_>) -> Result<()> {
@@ -96,5 +88,8 @@ fn tarball(matches: &ArgMatches<'_>) -> Result<()> {
"work-dir" => work_dir,
});
- tarballer.run().chain_err(|| "failed to generate tarballs")
+ tarballer
+ .run()
+ .with_context(|_| "failed to generate tarballs")?;
+ Ok(())
}
diff --git a/src/scripter.rs b/src/scripter.rs
index 1f83985..4363230 100644
--- a/src/scripter.rs
+++ b/src/scripter.rs
@@ -1,6 +1,6 @@
use std::io::Write;
-
-use crate::errors::*;
+use failure::ResultExt;
+use crate::Result;
use crate::util::*;
const TEMPLATE: &'static str = include_str!("../install-template.sh");
@@ -51,7 +51,9 @@ impl Scripter {
create_new_executable(&self.output_script)?
.write_all(script.as_ref())
- .chain_err(|| format!("failed to write output script '{}'", self.output_script))
+ .with_context(|_| format!("failed to write output script '{}'", self.output_script))?;
+
+ Ok(())
}
}
diff --git a/src/tarballer.rs b/src/tarballer.rs
index f8b6520..badac9c 100644
--- a/src/tarballer.rs
+++ b/src/tarballer.rs
@@ -1,16 +1,14 @@
+use failure::{bail, ResultExt};
+use flate2::write::GzEncoder;
use std::fs::{read_link, symlink_metadata};
use std::io::{self, empty, BufWriter, Write};
use std::path::Path;
-
-use flate2;
-use flate2::write::GzEncoder;
-use rayon;
use tar::{Builder, Header};
use walkdir::WalkDir;
use xz2::write::XzEncoder;
-use crate::errors::*;
use crate::util::*;
+use crate::Result;
actor! {
#[derive(Debug)]
@@ -43,7 +41,7 @@ impl Tarballer {
// different locations (likely identical) and files with the same
// extension (likely containing similar data).
let (dirs, mut files) = get_recursive_paths(&self.work_dir, &self.input)
- .chain_err(|| "failed to collect file paths")?;
+ .with_context(|_| "failed to collect file paths")?;
files.sort_by(|a, b| a.bytes().rev().cmp(b.bytes().rev()));
// Prepare the `.tar.gz` file.
@@ -67,26 +65,34 @@ impl Tarballer {
let src = Path::new(&self.work_dir).join(&path);
builder
.append_dir(&path, &src)
- .chain_err(|| format!("failed to tar dir '{}'", src.display()))?;
+ .with_context(|_| format!("failed to tar dir '{}'", src.display()))?;
}
for path in files {
let src = Path::new(&self.work_dir).join(&path);
append_path(&mut builder, &src, &path)
- .chain_err(|| format!("failed to tar file '{}'", src.display()))?;
+ .with_context(|_| format!("failed to tar file '{}'", src.display()))?;
}
let RayonTee(xz, gz) = builder
.into_inner()
- .chain_err(|| "failed to finish writing .tar stream")?
+ .with_context(|_| "failed to finish writing .tar stream")?
.into_inner()
.ok()
.unwrap();
// Finish both encoded files.
let (rxz, rgz) = rayon::join(
- || xz.finish().chain_err(|| "failed to finish .tar.xz file"),
- || gz.finish().chain_err(|| "failed to finish .tar.gz file"),
+ || {
+ xz.finish()
+ .with_context(|_| "failed to finish .tar.xz file")
+ },
+ || {
+ gz.finish()
+ .with_context(|_| "failed to finish .tar.gz file")
+ },
);
- rxz.and(rgz).and(Ok(()))
+ rxz?;
+ rgz?;
+ Ok(())
})
}
}
diff --git a/src/util.rs b/src/util.rs
index ec9d43a..8c1a9ca 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,3 +1,5 @@
+use crate::Result;
+use failure::{format_err, ResultExt};
use std::fs;
use std::path::Path;
use walkdir::WalkDir;
@@ -12,13 +14,10 @@ use std::os::unix::fs::symlink as symlink_file;
#[cfg(windows)]
use std::os::windows::fs::symlink_file;
-use crate::errors::*;
-
/// Converts a `&Path` to a UTF-8 `&str`.
pub fn path_to_str(path: &Path) -> Result<&str> {
- path.to_str().ok_or_else(|| {
- ErrorKind::Msg(format!("path is not valid UTF-8 '{}'", path.display())).into()
- })
+ path.to_str()
+ .ok_or_else(|| format_err!("path is not valid UTF-8 '{}'", path.display()))
}
/// Wraps `fs::copy` with a nicer error message.
@@ -28,26 +27,29 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
symlink_file(link, &to)?;
Ok(0)
} else {
- fs::copy(&from, &to).chain_err(|| {
+ let amt = fs::copy(&from, &to).with_context(|_| {
format!(
"failed to copy '{}' to '{}'",
from.as_ref().display(),
to.as_ref().display()
)
- })
+ })?;
+ Ok(amt)
}
}
/// Wraps `fs::create_dir` with a nicer error message.
pub fn create_dir<P: AsRef<Path>>(path: P) -> Result<()> {
fs::create_dir(&path)
- .chain_err(|| format!("failed to create dir '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to create dir '{}'", path.as_ref().display()))?;
+ Ok(())
}
/// Wraps `fs::create_dir_all` with a nicer error message.
pub fn create_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
fs::create_dir_all(&path)
- .chain_err(|| format!("failed to create dir '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to create dir '{}'", path.as_ref().display()))?;
+ Ok(())
}
/// Wraps `fs::OpenOptions::create_new().open()` as executable, with a nicer error message.
@@ -56,35 +58,41 @@ pub fn create_new_executable<P: AsRef<Path>>(path: P) -> Result<fs::File> {
options.write(true).create_new(true);
#[cfg(unix)]
options.mode(0o755);
- options
+ let file = options
.open(&path)
- .chain_err(|| format!("failed to create file '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to create file '{}'", path.as_ref().display()))?;
+ Ok(file)
}
/// Wraps `fs::OpenOptions::create_new().open()`, with a nicer error message.
pub fn create_new_file<P: AsRef<Path>>(path: P) -> Result<fs::File> {
- fs::OpenOptions::new()
+ let file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
- .chain_err(|| format!("failed to create file '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to create file '{}'", path.as_ref().display()))?;
+ Ok(file)
}
/// Wraps `fs::File::open()` with a nicer error message.
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<fs::File> {
- fs::File::open(&path).chain_err(|| format!("failed to open file '{}'", path.as_ref().display()))
+ let file = fs::File::open(&path)
+ .with_context(|_| format!("failed to open file '{}'", path.as_ref().display()))?;
+ Ok(file)
}
/// Wraps `remove_dir_all` with a nicer error message.
pub fn remove_dir_all<P: AsRef<Path>>(path: P) -> Result<()> {
crate::remove_dir_all::remove_dir_all(path.as_ref())
- .chain_err(|| format!("failed to remove dir '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to remove dir '{}'", path.as_ref().display()))?;
+ Ok(())
}
/// Wrap `fs::remove_file` with a nicer error message
pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> {
fs::remove_file(path.as_ref())
- .chain_err(|| format!("failed to remove file '{}'", path.as_ref().display()))
+ .with_context(|_| format!("failed to remove file '{}'", path.as_ref().display()))?;
+ Ok(())
}
/// Copies the `src` directory recursively to `dst`. Both are assumed to exist