fix: output better serde::yaml errors (#47)
Co-authored-by: Gerald Pinder <gmpinder@gmail.com>
This commit is contained in:
parent
59c3cf55bd
commit
8f44bf4ea0
7 changed files with 107 additions and 64 deletions
|
|
@ -5,6 +5,7 @@ use anyhow::Result;
|
|||
use askama::Template;
|
||||
use clap::Args;
|
||||
use clap_complete::Shell;
|
||||
use format_serde_error::SerdeError;
|
||||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
||||
use log::{debug, error, trace};
|
||||
use requestty::question::{completions, Completions};
|
||||
|
|
@ -390,10 +391,16 @@ fn get_module_from_file(file_name: &str) -> Result<ModuleExt> {
|
|||
|
||||
let file = fs::read_to_string(file_path.clone())?;
|
||||
|
||||
serde_yaml::from_str::<ModuleExt>(file.as_str()).map_or_else(
|
||||
|_| -> Result<ModuleExt> {
|
||||
let module = serde_yaml::from_str::<Module>(file.as_str())?;
|
||||
serde_yaml::from_str::<ModuleExt>(&file).map_or_else(
|
||||
|err| -> Result<ModuleExt> {
|
||||
error!(
|
||||
"Failed to parse module from {}: {}",
|
||||
file_path.display(),
|
||||
SerdeError::new(file.to_owned(), err).to_string()
|
||||
);
|
||||
|
||||
let module =
|
||||
serde_yaml::from_str::<Module>(&file).map_err(|err| SerdeError::new(file, err))?;
|
||||
Ok(ModuleExt::builder().modules(vec![module]).build())
|
||||
},
|
||||
Ok,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ use std::{
|
|||
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use clap::Args;
|
||||
use format_serde_error::SerdeError;
|
||||
use log::{debug, info, trace, warn};
|
||||
use typed_builder::TypedBuilder;
|
||||
|
||||
|
|
@ -21,9 +22,6 @@ use podman_api::{
|
|||
#[cfg(feature = "podman-api")]
|
||||
use build_strategy::BuildStrategy;
|
||||
|
||||
#[cfg(feature = "futures-util")]
|
||||
use futures_util::StreamExt;
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
|
|
@ -188,6 +186,8 @@ impl BlueBuildCommand for BuildCommand {
|
|||
impl BuildCommand {
|
||||
#[cfg(feature = "podman-api")]
|
||||
async fn build_image_podman_api(&self, client: Podman, recipe_path: &Path) -> Result<()> {
|
||||
use futures_util::StreamExt;
|
||||
|
||||
trace!("BuildCommand::build_image({client:#?})");
|
||||
|
||||
let credentials = self.get_login_creds();
|
||||
|
|
@ -196,7 +196,9 @@ impl BuildCommand {
|
|||
bail!("Failed to get credentials");
|
||||
}
|
||||
|
||||
let recipe: Recipe = serde_yaml::from_str(fs::read_to_string(recipe_path)?.as_str())?;
|
||||
let recipe_str = fs::read_to_string(recipe_path)?;
|
||||
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
|
||||
.map_err(|err| SerdeError::new(recipe_str.to_owned(), err))?;
|
||||
trace!("recipe: {recipe:#?}");
|
||||
|
||||
// Get values for image
|
||||
|
|
@ -280,7 +282,9 @@ impl BuildCommand {
|
|||
fn build_image(&self, recipe_path: &Path) -> Result<()> {
|
||||
trace!("BuildCommand::build_image()");
|
||||
|
||||
let recipe: Recipe = serde_yaml::from_str(fs::read_to_string(recipe_path)?.as_str())?;
|
||||
let recipe_str = fs::read_to_string(recipe_path)?;
|
||||
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
|
||||
.map_err(|err| SerdeError::new(recipe_str.to_owned(), err))?;
|
||||
|
||||
let tags = recipe.generate_tags();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use std::{
|
|||
|
||||
use anyhow::{bail, Result};
|
||||
use clap::Args;
|
||||
use format_serde_error::SerdeError;
|
||||
use log::{debug, info, trace};
|
||||
use typed_builder::TypedBuilder;
|
||||
use users::{Users, UsersCache};
|
||||
|
|
@ -43,8 +44,10 @@ impl BlueBuildCommand for UpgradeCommand {
|
|||
|
||||
check_can_run()?;
|
||||
|
||||
let recipe: Recipe =
|
||||
serde_yaml::from_str(fs::read_to_string(&self.common.recipe)?.as_str())?;
|
||||
let recipe_str = fs::read_to_string(&self.common.recipe)?;
|
||||
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
|
||||
.map_err(|err| SerdeError::new(recipe_str.to_owned(), err))?;
|
||||
|
||||
let mut build = BuildCommand::builder()
|
||||
.recipe(self.common.recipe.clone())
|
||||
.archive(LOCAL_BUILD)
|
||||
|
|
@ -90,8 +93,10 @@ impl BlueBuildCommand for RebaseCommand {
|
|||
|
||||
check_can_run()?;
|
||||
|
||||
let recipe: Recipe =
|
||||
serde_yaml::from_str(fs::read_to_string(&self.common.recipe)?.as_str())?;
|
||||
let recipe_str = fs::read_to_string(&self.common.recipe)?;
|
||||
let recipe: Recipe = serde_yaml::from_str(&recipe_str)
|
||||
.map_err(|err| SerdeError::new(recipe_str.to_owned(), err))?;
|
||||
|
||||
let mut build = BuildCommand::builder()
|
||||
.recipe(self.common.recipe.clone())
|
||||
.archive(LOCAL_BUILD)
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use std::{
|
|||
use anyhow::Result;
|
||||
use askama::Template;
|
||||
use clap::Args;
|
||||
use format_serde_error::SerdeError;
|
||||
use log::{debug, error, info, trace};
|
||||
use typed_builder::TypedBuilder;
|
||||
|
||||
|
|
@ -184,8 +185,11 @@ fn template_module_from_file(file_name: &str) -> String {
|
|||
|
||||
serde_yaml::from_str::<ModuleExt>(file.as_str()).map_or_else(
|
||||
|_| {
|
||||
let module = serde_yaml::from_str::<Module>(file.as_str()).unwrap_or_else(|e| {
|
||||
error!("Failed to deserialize module {file_name}: {e}");
|
||||
let module = serde_yaml::from_str::<Module>(file.as_str()).unwrap_or_else(|err| {
|
||||
error!(
|
||||
"Failed to deserialize module {file_name}: {}",
|
||||
SerdeError::new(file_name.to_owned(), err)
|
||||
);
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,10 @@
|
|||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
env, fs,
|
||||
path::Path,
|
||||
process::{self, Command},
|
||||
};
|
||||
use std::{borrow::Cow, collections::HashMap, env, fs, path::Path, process::Command};
|
||||
|
||||
use anyhow::Result;
|
||||
use chrono::Local;
|
||||
use format_serde_error::SerdeError;
|
||||
use indexmap::IndexMap;
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use log::{debug, info, trace, warn};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use serde_yaml::Value;
|
||||
|
|
@ -137,17 +132,11 @@ impl<'a> Recipe<'a> {
|
|||
let recipe_path_string = recipe_path.display().to_string();
|
||||
debug!("Recipe::parse_recipe({recipe_path_string})");
|
||||
|
||||
let file = fs::read_to_string(recipe_path).unwrap_or_else(|e| {
|
||||
error!("Failed to read file {recipe_path_string}: {e}");
|
||||
process::exit(1);
|
||||
});
|
||||
let file = fs::read_to_string(recipe_path)?;
|
||||
|
||||
debug!("Recipe contents: {file}");
|
||||
|
||||
serde_yaml::from_str::<Recipe>(file.as_str()).map_err(|e| {
|
||||
error!("Failed to parse recipe {recipe_path_string}: {e}");
|
||||
process::exit(1);
|
||||
})
|
||||
Ok(serde_yaml::from_str::<Recipe>(&file).map_err(|err| SerdeError::new(file, err))?)
|
||||
}
|
||||
|
||||
fn get_os_version(&self) -> String {
|
||||
|
|
@ -185,8 +174,11 @@ impl<'a> Recipe<'a> {
|
|||
let inspection: ImageInspection = match serde_json::from_str(
|
||||
String::from_utf8_lossy(&output.stdout).as_ref(),
|
||||
) {
|
||||
Err(_) => {
|
||||
warn!("Issue deserializing 'skopeo' output, falling back to version defined in recipe");
|
||||
Err(err) => {
|
||||
let err_msg =
|
||||
SerdeError::new(String::from_utf8_lossy(&output.stdout).to_string(), err)
|
||||
.to_string();
|
||||
warn!("Issue deserializing 'skopeo' output, falling back to version defined in recipe. {err_msg}",);
|
||||
return self.image_version.to_string();
|
||||
}
|
||||
Ok(inspection) => inspection,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue