summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2020-04-06 15:26:25 -0400
committerGitHub <noreply@github.com>2020-04-06 15:26:25 -0400
commitd66f476b4d5e7fdf1ec215c9ac16c923dc292324 (patch)
tree75363152b1230dc2ee293569609945ae809c4dd3
parent0a22f4fb63de9f52be81c18e6fefec1e50277625 (diff)
parent9a112b3352ed13b56d6fe801dcf9a995c191a851 (diff)
downloadrust-installer-d66f476b4d5e7fdf1ec215c9ac16c923dc292324.tar.gz
Merge pull request #101 from cuviper/anyhow
Migrate from failure to anyhow
-rw-r--r--Cargo.toml2
-rw-r--r--src/combiner.rs14
-rw-r--r--src/generator.rs7
-rw-r--r--src/lib.rs2
-rw-r--r--src/main.rs17
-rw-r--r--src/scripter.rs7
-rw-r--r--src/tarballer.rs21
-rw-r--r--src/util.rs19
8 files changed, 34 insertions, 55 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 37dcbb5..7979528 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,7 +10,7 @@ name = "fabricate"
path = "src/main.rs"
[dependencies]
-failure = "0.1"
+anyhow = "1.0.19"
flate2 = "1.0.1"
rayon = "1.0"
tar = "0.4.13"
diff --git a/src/combiner.rs b/src/combiner.rs
index 35f3560..9cdd2df 100644
--- a/src/combiner.rs
+++ b/src/combiner.rs
@@ -1,8 +1,7 @@
use super::Scripter;
use super::Tarballer;
use crate::util::*;
-use crate::Result;
-use failure::{bail, ResultExt};
+use anyhow::{bail, Context, Result};
use flate2::read::GzDecoder;
use std::io::{Read, Write};
use std::path::Path;
@@ -61,7 +60,7 @@ impl Combiner {
{
// Extract the input tarballs
let tar = GzDecoder::new(open_file(&input_tarball)?);
- Archive::new(tar).unpack(&self.work_dir).with_context(|_| {
+ Archive::new(tar).unpack(&self.work_dir).with_context(|| {
format!(
"unable to extract '{}' into '{}'",
&input_tarball, self.work_dir
@@ -76,7 +75,7 @@ impl Combiner {
let mut version = String::new();
open_file(pkg_dir.join("rust-installer-version"))
.and_then(|mut file| Ok(file.read_to_string(&mut version)?))
- .with_context(|_| format!("failed to read version in '{}'", input_tarball))?;
+ .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,7 +84,7 @@ impl Combiner {
let mut pkg_components = String::new();
open_file(pkg_dir.join("components"))
.and_then(|mut file| Ok(file.read_to_string(&mut pkg_components)?))
- .with_context(|_| format!("failed to read components in '{}'", input_tarball))?;
+ .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
@@ -95,8 +94,7 @@ impl Combiner {
copy_recursive(&pkg_dir.join(&component), &component_dir)?;
// Merge the component name.
- writeln!(&components, "{}", component)
- .with_context(|_| "failed to write new components")?;
+ writeln!(&components, "{}", component).context("failed to write new components")?;
}
}
drop(components);
@@ -108,7 +106,7 @@ impl Combiner {
"{}",
crate::RUST_INSTALLER_VERSION
)
- .with_context(|_| "failed to write new installer version")?;
+ .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 3b4b0d4..ecf73ca 100644
--- a/src/generator.rs
+++ b/src/generator.rs
@@ -1,8 +1,7 @@
use super::Scripter;
use super::Tarballer;
use crate::util::*;
-use crate::Result;
-use failure::{format_err, bail, ResultExt};
+use anyhow::{bail, format_err, Context, Result};
use std::io::Write;
use std::path::Path;
@@ -62,7 +61,7 @@ impl Generator {
// Write the component name
let components = package_dir.join("components");
writeln!(create_new_file(components)?, "{}", self.component_name)
- .with_context(|_| "failed to write the component file")?;
+ .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 +70,7 @@ impl Generator {
"{}",
crate::RUST_INSTALLER_VERSION
)
- .with_context(|_| "failed to write new installer version")?;
+ .context("failed to write new installer version")?;
// Copy the overlay
if !self.non_installed_overlay.is_empty() {
diff --git a/src/lib.rs b/src/lib.rs
index 0310aba..8e88311 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,3 @@
-pub type Result<T> = std::result::Result<T, failure::Error>;
-
#[macro_use]
mod util;
diff --git a/src/main.rs b/src/main.rs
index 90f1602..8b5f1e1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,5 @@
+use anyhow::{Context, Result};
use clap::{App, ArgMatches};
-use failure::ResultExt;
-use installer::Result;
fn main() -> Result<()> {
let yaml = clap::load_yaml!("main.yml");
@@ -39,9 +38,7 @@ fn combine(matches: &ArgMatches<'_>) -> Result<()> {
"output-dir" => output_dir,
});
- combiner
- .run()
- .with_context(|_| "failed to combine installers")?;
+ combiner.run().context("failed to combine installers")?;
Ok(())
}
@@ -60,9 +57,7 @@ fn generate(matches: &ArgMatches<'_>) -> Result<()> {
"output-dir" => output_dir,
});
- generator
- .run()
- .with_context(|_| "failed to generate installer")?;
+ generator.run().context("failed to generate installer")?;
Ok(())
}
@@ -77,7 +72,7 @@ fn script(matches: &ArgMatches<'_>) -> Result<()> {
scripter
.run()
- .with_context(|_| "failed to generate installation script")?;
+ .context("failed to generate installation script")?;
Ok(())
}
@@ -88,8 +83,6 @@ fn tarball(matches: &ArgMatches<'_>) -> Result<()> {
"work-dir" => work_dir,
});
- tarballer
- .run()
- .with_context(|_| "failed to generate tarballs")?;
+ tarballer.run().context("failed to generate tarballs")?;
Ok(())
}
diff --git a/src/scripter.rs b/src/scripter.rs
index 4363230..9e82ae7 100644
--- a/src/scripter.rs
+++ b/src/scripter.rs
@@ -1,7 +1,6 @@
-use std::io::Write;
-use failure::ResultExt;
-use crate::Result;
use crate::util::*;
+use anyhow::{Context, Result};
+use std::io::Write;
const TEMPLATE: &'static str = include_str!("../install-template.sh");
@@ -51,7 +50,7 @@ impl Scripter {
create_new_executable(&self.output_script)?
.write_all(script.as_ref())
- .with_context(|_| 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 fa6ab2c..42a4ffa 100644
--- a/src/tarballer.rs
+++ b/src/tarballer.rs
@@ -1,4 +1,4 @@
-use failure::{bail, ResultExt};
+use anyhow::{bail, Context, Result};
use flate2::write::GzEncoder;
use std::fs::{read_link, symlink_metadata};
use std::io::{self, empty, BufWriter, Write};
@@ -8,7 +8,6 @@ use walkdir::WalkDir;
use xz2::write::XzEncoder;
use crate::util::*;
-use crate::Result;
actor! {
#[derive(Debug)]
@@ -41,7 +40,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)
- .with_context(|_| "failed to collect file paths")?;
+ .context("failed to collect file paths")?;
files.sort_by(|a, b| a.bytes().rev().cmp(b.bytes().rev()));
// Prepare the `.tar.gz` file.
@@ -71,30 +70,24 @@ impl Tarballer {
let src = Path::new(&self.work_dir).join(&path);
builder
.append_dir(&path, &src)
- .with_context(|_| 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)
- .with_context(|_| format!("failed to tar file '{}'", src.display()))?;
+ .with_context(|| format!("failed to tar file '{}'", src.display()))?;
}
let RayonTee(xz, gz) = builder
.into_inner()
- .with_context(|_| "failed to finish writing .tar stream")?
+ .context("failed to finish writing .tar stream")?
.into_inner()
.ok()
.unwrap();
// Finish both encoded files.
let (rxz, rgz) = rayon::join(
- || {
- xz.finish()
- .with_context(|_| "failed to finish .tar.xz file")
- },
- || {
- gz.finish()
- .with_context(|_| "failed to finish .tar.gz file")
- },
+ || xz.finish().context("failed to finish .tar.xz file"),
+ || gz.finish().context("failed to finish .tar.gz file"),
);
rxz?;
rgz?;
diff --git a/src/util.rs b/src/util.rs
index 868a636..3ddcfc6 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,5 +1,4 @@
-use crate::Result;
-use failure::{format_err, ResultExt};
+use anyhow::{format_err, Context, Result};
use std::fs;
use std::path::Path;
use walkdir::WalkDir;
@@ -27,7 +26,7 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
symlink_file(link, &to)?;
Ok(0)
} else {
- let amt = fs::copy(&from, &to).with_context(|_| {
+ let amt = fs::copy(&from, &to).with_context(|| {
format!(
"failed to copy '{}' to '{}'",
from.as_ref().display(),
@@ -41,14 +40,14 @@ pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
/// Wraps `fs::create_dir` with a nicer error message.
pub fn create_dir<P: AsRef<Path>>(path: P) -> Result<()> {
fs::create_dir(&path)
- .with_context(|_| 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)
- .with_context(|_| format!("failed to create dir '{}'", path.as_ref().display()))?;
+ .with_context(|| format!("failed to create dir '{}'", path.as_ref().display()))?;
Ok(())
}
@@ -60,7 +59,7 @@ pub fn create_new_executable<P: AsRef<Path>>(path: P) -> Result<fs::File> {
options.mode(0o755);
let file = options
.open(&path)
- .with_context(|_| format!("failed to create file '{}'", path.as_ref().display()))?;
+ .with_context(|| format!("failed to create file '{}'", path.as_ref().display()))?;
Ok(file)
}
@@ -70,28 +69,28 @@ pub fn create_new_file<P: AsRef<Path>>(path: P) -> Result<fs::File> {
.write(true)
.create_new(true)
.open(&path)
- .with_context(|_| 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> {
let file = fs::File::open(&path)
- .with_context(|_| format!("failed to open file '{}'", path.as_ref().display()))?;
+ .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<()> {
remove_dir_all::remove_dir_all(path.as_ref())
- .with_context(|_| 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())
- .with_context(|_| format!("failed to remove file '{}'", path.as_ref().display()))?;
+ .with_context(|| format!("failed to remove file '{}'", path.as_ref().display()))?;
Ok(())
}