Skip to content

Commit

Permalink
Migrate from deprecated failure to anyhow
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-petruk committed Jan 8, 2023
1 parent 8f359eb commit 50c6868
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 163 deletions.
108 changes: 4 additions & 104 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ scriptisto = { path = "/usr/bin/scriptisto" }
[dependencies]
dirs = '4.0'
exec = '0.3'
failure = '0.1'
anyhow = '1'
include_dir = '0.7'
log = '0.4'
serde='1.0'
Expand All @@ -53,7 +53,7 @@ clap = { version = "4", features = ["derive"] }
[build-dependencies]
clap_mangen="0.2"
clap = { version = "4", features = ["derive", "string"] }
failure = '0.1'
anyhow = '1'

[dependencies.env_logger]
default-features = false
Expand Down
18 changes: 9 additions & 9 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use failure::{format_err, Error, ResultExt};
use anyhow::{anyhow, Context, Result};
use log::debug;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
Expand All @@ -23,22 +23,22 @@ use crate::opt;

const SCRIPTISTO_SOURCE_VAR: &str = "SCRIPTISTO_SOURCE";

fn docker_prefix(script_cache_path: &Path) -> Result<String, Error> {
fn docker_prefix(script_cache_path: &Path) -> Result<String> {
Ok(format!(
"scriptisto-{}-{:x}",
script_cache_path
.file_name()
.ok_or_else(|| format_err!("BUG: invalid script_cache_path={:?}", script_cache_path))?
.ok_or_else(|| anyhow!("BUG: invalid script_cache_path={:?}", script_cache_path))?
.to_string_lossy(),
md5::compute(script_cache_path.to_string_lossy().as_bytes())
))
}

pub fn docker_image_name(script_cache_path: &Path) -> Result<String, Error> {
pub fn docker_image_name(script_cache_path: &Path) -> Result<String> {
docker_prefix(script_cache_path)
}

pub fn docker_volume_name(script_cache_path: &Path) -> Result<String, Error> {
pub fn docker_volume_name(script_cache_path: &Path) -> Result<String> {
let docker_prefix = docker_prefix(script_cache_path)?;
Ok(format!("{}-src", docker_prefix))
}
Expand All @@ -47,7 +47,7 @@ fn docker_create_volume(
volume_name: &str,
script_cache_path: &Path,
stderr_mode: Stdio,
) -> Result<(), Error> {
) -> Result<()> {
let mut build_vol_cmd = Command::new("docker");
build_vol_cmd.arg("volume").arg("create").arg(volume_name);
common::run_command(script_cache_path, build_vol_cmd, stderr_mode)?;
Expand All @@ -60,7 +60,7 @@ fn docker_volume_cmd(
run_as_current_user: bool,
cmd: &str,
stderr_mode: Stdio,
) -> Result<(), Error> {
) -> Result<()> {
let mut vol_cmd = Command::new("docker");
vol_cmd.args(["run", "-t", "--rm"]);
if run_as_current_user {
Expand All @@ -87,7 +87,7 @@ fn run_build_command<F>(
first_run: bool,
build_mode: opt::BuildMode,
stderr_mode: F,
) -> Result<(), Error>
) -> Result<()>
where
F: Fn() -> Stdio,
{
Expand Down Expand Up @@ -217,7 +217,7 @@ pub fn perform(
build_mode: opt::BuildMode,
script_path: &str,
show_logs: bool,
) -> Result<(cfg::BuildSpec, PathBuf), Error> {
) -> Result<(cfg::BuildSpec, PathBuf)> {
let script_path = Path::new(script_path);

let script_body = std::fs::read(script_path).context("Cannot read script file")?;
Expand Down
14 changes: 7 additions & 7 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

use crate::opt::CacheCommand;
use failure::{Error, ResultExt};
use anyhow::{anyhow, Context, Result};
use number_prefix::NumberPrefix;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
Expand All @@ -40,7 +40,7 @@ fn get_dir_size_lossy(path: &Path) -> String {
}
}

fn collect_info(script_path: &Path) -> Result<BTreeMap<String, String>, Error> {
fn collect_info(script_path: &Path) -> Result<BTreeMap<String, String>> {
let script_body = std::fs::read(script_path).context("Cannot read script file")?;
let script_cache_path = common::build_cache_path(script_path).context(format!(
"Cannot build cache path for script: {:?}",
Expand Down Expand Up @@ -70,22 +70,22 @@ fn collect_info(script_path: &Path) -> Result<BTreeMap<String, String>, Error> {
Ok(items)
}

pub fn command_get(name: &str, script_path: &Path) -> Result<(), Error> {
pub fn command_get(name: &str, script_path: &Path) -> Result<()> {
let items = collect_info(script_path)?;

if let Some(value) = items.get(name) {
println!("{}", value);
Ok(())
} else {
Err(format_err!(
Err(anyhow!(
"'{}' is not found. Available items: {:?}.",
name,
items.keys()
))
}
}

pub fn command_info(script_path: &Path) -> Result<(), Error> {
pub fn command_info(script_path: &Path) -> Result<()> {
let items = collect_info(script_path)?;

for (k, v) in items.iter() {
Expand All @@ -95,7 +95,7 @@ pub fn command_info(script_path: &Path) -> Result<(), Error> {
Ok(())
}

pub fn command_clean(script_path: &Path) -> Result<(), Error> {
pub fn command_clean(script_path: &Path) -> Result<()> {
let items = collect_info(script_path)?;
let cache_path = items.get("cache_path").expect("cache_path to exist");

Expand All @@ -116,7 +116,7 @@ pub fn command_clean(script_path: &Path) -> Result<(), Error> {
Ok(())
}

pub fn command_cache(cmd: CacheCommand) -> Result<(), Error> {
pub fn command_cache(cmd: CacheCommand) -> Result<()> {
match cmd {
CacheCommand::Clean { file } => command_clean(&file),
CacheCommand::Get { name, file } => command_get(&name, &file),
Expand Down
4 changes: 2 additions & 2 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use failure::{Error, ResultExt};
use anyhow::{Context, Result};
use log::debug;
use serde_derive::Deserialize;
use std::cmp::min;
Expand Down Expand Up @@ -59,7 +59,7 @@ enum ParserState {
}

impl BuildSpec {
pub fn new(script_body: &[u8]) -> Result<Self, Error> {
pub fn new(script_body: &[u8]) -> Result<Self> {
let mut script_src = Vec::new();
let reader = BufReader::new(script_body);

Expand Down
Loading

0 comments on commit 50c6868

Please sign in to comment.