Skip to content

Commit

Permalink
Merge branch 'better-error-messages-when-parsing'
Browse files Browse the repository at this point in the history
  • Loading branch information
dag-andersen committed May 4, 2024
2 parents 82a7769 + 7f8f10f commit c078707
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 25 deletions.
9 changes: 8 additions & 1 deletion src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ static ERROR_MESSAGES: [&str; 6] = [
"Unknown desc = `helm template .",
];

static TIMEOUT_MESSAGES: [&str; 2] = ["Client.Timeout", "failed to get git client for repo"];
static TIMEOUT_MESSAGES: [&str; 3] = [
"Client.Timeout",
"failed to get git client for repo",
"rpc error: code = Unknown desc = Get \"https"
];

pub async fn get_resources(
branch_type: &Branch,
Expand Down Expand Up @@ -106,6 +110,7 @@ pub async fn get_resources(
break;
}

// TIMEOUT
let time_elapsed = start_time.elapsed().as_secs();
if time_elapsed > timeout as u64 {
error!("❌ Timed out after {} seconds", timeout);
Expand All @@ -117,6 +122,7 @@ pub async fn get_resources(
return Err("Timed out".into());
}

// ERRORS
if !set_of_failed_apps.is_empty() {
for (name, msg) in &set_of_failed_apps {
error!(
Expand All @@ -127,6 +133,7 @@ pub async fn get_resources(
return Err("Failed to process applications".into());
}

// TIMED OUT APPS
if !list_of_timed_out_apps.is_empty() {
info!(
"💤 {} Applications timed out.",
Expand Down
2 changes: 1 addition & 1 deletion src/kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::error::Error;
use crate::{run_command, utils::spawn_command};

pub async fn is_installed() -> bool {
match run_command("kind version", None).await {
match run_command("which kind", None).await {
Ok(_) => true,
Err(_) => false,
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
info!("✨ - secrets-folder: {}", secrets_folder);
info!("✨ - output-folder: {}", output_folder);
info!("✨ - git-repo: {}", repo);
info!("✨ - timeout: {}", timeout);
info!("✨ - timeout: {} seconds", timeout);
if let Some(a) = file_regex.clone() {
info!("✨ - file-regex: {}", a.as_str());
}
Expand Down
2 changes: 1 addition & 1 deletion src/minikube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::error::Error;
use crate::{run_command, utils::spawn_command};

pub async fn is_installed() -> bool {
match run_command("minikube status", None).await {
match run_command("which minikube", None).await {
Ok(_) => true,
Err(_) => false,
}
Expand Down
46 changes: 25 additions & 21 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::{error::Error, io::BufRead};

use log::{debug, info};

type K8sResource = serde_yaml::Value;

pub async fn get_applications(
directory: &str,
branch: &str,
Expand All @@ -15,7 +17,10 @@ pub async fn get_applications(
Ok(output)
}

async fn parse_yaml(directory: &str, regex: &Option<Regex>) -> Result<Vec<String>, Box<dyn Error>> {
async fn parse_yaml(
directory: &str,
regex: &Option<Regex>,
) -> Result<Vec<K8sResource>, Box<dyn Error>> {
use walkdir::WalkDir;

info!("🤖 Fetching all files in dir: {}", directory);
Expand All @@ -34,15 +39,14 @@ async fn parse_yaml(directory: &str, regex: &Option<Regex>) -> Result<Vec<String
.map(|e| format!("{}", e.path().display()))
.filter(|f| regex.is_none() || regex.as_ref().unwrap().is_match(&f));

let k8s_resources: Vec<String> = yaml_files
let k8s_resources: Vec<K8sResource> = yaml_files
.flat_map(|f| {
debug!("Found file: {}", f);
let file = std::fs::File::open(f).unwrap();
let file = std::fs::File::open(&f).unwrap();
let reader = std::io::BufReader::new(file);
let lines = reader.lines().map(|l| l.unwrap());

// split list of strings by "---"
let string_lists: Vec<String> = lines.fold(vec!["".to_string()], |mut acc, s| {
let mut raw_yaml_chunks: Vec<String> = lines.fold(vec!["".to_string()], |mut acc, s| {
if s == "---" {
acc.push("".to_string());
} else {
Expand All @@ -52,9 +56,19 @@ async fn parse_yaml(directory: &str, regex: &Option<Regex>) -> Result<Vec<String
}
acc
});
string_lists
let yaml_vec: Vec<serde_yaml::Value> = raw_yaml_chunks.iter_mut().enumerate().map(|(i,r)| {
let yaml = match serde_yaml::from_str(r) {
Ok(r) => r,
Err(e) => {
debug!("⚠️ Failed to parse element number {}, in file '{}', with error: '{}'", i, f, e);
serde_yaml::Value::Null
}
};
yaml
}).collect();
yaml_vec
})
.collect::<Vec<String>>();
.collect();

match regex {
Some(r) => info!(
Expand All @@ -69,7 +83,7 @@ async fn parse_yaml(directory: &str, regex: &Option<Regex>) -> Result<Vec<String
}

async fn patch_argocd_applications(
mut yaml_chunks: Vec<String>,
yaml_chunks: Vec<K8sResource>,
branch: &str,
repo: &str,
) -> Result<String, Box<dyn Error>> {
Expand Down Expand Up @@ -100,18 +114,8 @@ async fn patch_argocd_applications(
}
};

let applications = yaml_chunks
.iter_mut()
.map(|r| {
let resource: serde_yaml::Value = match serde_yaml::from_str(r) {
Ok(r) => r,
Err(e) => {
debug!("⚠️ Failed to parse resource with error: {}", e);
serde_yaml::Value::Null
}
};
resource
})
let applications: Vec<K8sResource> = yaml_chunks
.into_iter()
.map(|mut r| {
r["metadata"]["namespace"] = serde_yaml::Value::String("argocd".to_string());
r
Expand Down Expand Up @@ -151,7 +155,7 @@ async fn patch_argocd_applications(
);
r
})
.collect::<Vec<serde_yaml::Value>>();
.collect();

info!(
"🤖 Patching {} Argo CD Application[Sets] for branch: {}",
Expand Down

0 comments on commit c078707

Please sign in to comment.