fix: Give better errors for read_to_string

This commit is contained in:
Gerald Pinder 2024-04-16 17:34:04 -04:00
parent 7c4c6759ca
commit 4ef0bf9169
3 changed files with 15 additions and 12 deletions

View file

@ -1,6 +1,6 @@
use std::{borrow::Cow, collections::HashSet, fs, path::Path};
use anyhow::Result;
use anyhow::{Context, Result};
use blue_build_utils::constants::{CONFIG_PATH, RECIPE_PATH};
use log::{trace, warn};
use serde::{Deserialize, Serialize};
@ -31,7 +31,8 @@ impl ModuleExt<'_> {
legacy_path.join(file_name)
};
let file = fs::read_to_string(file_path)?;
let file = fs::read_to_string(&file_path)
.context(format!("Failed to open {}", file_path.display()))?;
serde_yaml::from_str::<Self>(&file).map_or_else(
|_| -> Result<Self> {

View file

@ -1,6 +1,6 @@
use std::{borrow::Cow, env, fs, path::Path};
use anyhow::Result;
use anyhow::{Context, Result};
use blue_build_utils::constants::{
CI_COMMIT_REF_NAME, CI_COMMIT_SHORT_SHA, CI_DEFAULT_BRANCH, CI_MERGE_REQUEST_IID,
CI_PIPELINE_SOURCE, GITHUB_EVENT_NAME, GITHUB_REF_NAME, GITHUB_SHA, PR_EVENT_NUMBER,
@ -123,17 +123,16 @@ impl<'a> Recipe<'a> {
/// #
/// # Errors
pub fn parse<P: AsRef<Path>>(path: &P) -> Result<Self> {
trace!("Recipe::parse({})", path.as_ref().display());
let file_path = if Path::new(path.as_ref()).is_absolute() {
path.as_ref().to_path_buf()
} else {
std::env::current_dir()?.join(path.as_ref())
};
let recipe_path = fs::canonicalize(file_path)?;
let recipe_path_string = recipe_path.display().to_string();
debug!("Recipe::parse_recipe({recipe_path_string})");
let file = fs::read_to_string(recipe_path)?;
let file = fs::read_to_string(&file_path)
.context(format!("Failed to read {}", file_path.display()))?;
debug!("Recipe contents: {file}");

View file

@ -4,7 +4,7 @@ use std::{
process::Command,
};
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use blue_build_recipe::Recipe;
use blue_build_utils::constants::{
ARCHIVE_SUFFIX, BUILD_ID_LABEL, CI_DEFAULT_BRANCH, CI_PROJECT_NAME, CI_PROJECT_NAMESPACE,
@ -133,14 +133,16 @@ impl BlueBuildCommand for BuildCommand {
let container_file_path = Path::new(CONTAINER_FILE);
if !self.force && container_file_path.exists() {
let gitignore = fs::read_to_string(GITIGNORE_PATH)?;
let gitignore = fs::read_to_string(GITIGNORE_PATH)
.context(format!("Failed to read {GITIGNORE_PATH}"))?;
let is_ignored = gitignore
.lines()
.any(|line: &str| line.contains(CONTAINER_FILE));
if !is_ignored {
let containerfile = fs::read_to_string(container_file_path)?;
let containerfile = fs::read_to_string(container_file_path)
.context(format!("Failed to read {}", container_file_path.display()))?;
let has_label = containerfile.lines().any(|line| {
let label = format!("LABEL {BUILD_ID_LABEL}");
line.to_string().trim().starts_with(&label)
@ -532,7 +534,8 @@ fn check_cosign_files() -> Result<()> {
}
let calculated_pub_key = String::from_utf8(output.stdout)?;
let found_pub_key = fs::read_to_string(COSIGN_PATH)?;
let found_pub_key =
fs::read_to_string(COSIGN_PATH).context(format!("Failed to read {COSIGN_PATH}"))?;
trace!("calculated_pub_key={calculated_pub_key},found_pub_key={found_pub_key}");
if calculated_pub_key.trim() == found_pub_key.trim() {