From 6a8f46ee7ac419664033e9fd03678ee34dada44f Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Tue, 21 Jan 2025 22:53:05 -0800 Subject: [PATCH] escape incoming sync backslashes (BREAKING) --- bin/core/src/helpers/update.rs | 2 +- bin/core/src/sync/file.rs | 4 +--- bin/core/src/sync/mod.rs | 34 +++++++++++++++++++++++++++++----- bin/core/src/sync/remote.rs | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/bin/core/src/helpers/update.rs b/bin/core/src/helpers/update.rs index c15993c31..fe4f8d6ad 100644 --- a/bin/core/src/helpers/update.rs +++ b/bin/core/src/helpers/update.rs @@ -517,6 +517,6 @@ pub async fn init_execution_update( // Don't actually send it here, let the handlers send it after they can set action state. update.id = add_update_without_send(&update).await?; } - + Ok(update) } diff --git a/bin/core/src/sync/file.rs b/bin/core/src/sync/file.rs index 4f3691705..672410b84 100644 --- a/bin/core/src/sync/file.rs +++ b/bin/core/src/sync/file.rs @@ -143,9 +143,7 @@ fn read_resource_file( path: file_path.display().to_string(), contents: contents.clone(), }); - let more = toml::from_str::(&contents) - // the error without this comes through with multiple lines (\n) and looks bad - .map_err(|e| anyhow!("{e:#}")) + let more = super::deserialize_resources_toml(&contents) .context("failed to parse resource file contents")?; log.push('\n'); let path_for_view = diff --git a/bin/core/src/sync/mod.rs b/bin/core/src/sync/mod.rs index 0bf855555..9e4d29c42 100644 --- a/bin/core/src/sync/mod.rs +++ b/bin/core/src/sync/mod.rs @@ -1,11 +1,21 @@ use std::{collections::HashMap, str::FromStr}; +use anyhow::anyhow; use komodo_client::entities::{ - action::Action, alerter::Alerter, build::Build, builder::Builder, - deployment::Deployment, procedure::Procedure, repo::Repo, - server::Server, server_template::ServerTemplate, stack::Stack, - sync::ResourceSync, tag::Tag, toml::ResourceToml, ResourceTarget, - ResourceTargetVariant, + action::Action, + alerter::Alerter, + build::Build, + builder::Builder, + deployment::Deployment, + procedure::Procedure, + repo::Repo, + server::Server, + server_template::ServerTemplate, + stack::Stack, + sync::ResourceSync, + tag::Tag, + toml::{ResourceToml, ResourcesToml}, + ResourceTarget, ResourceTargetVariant, }; use mungos::mongodb::bson::oid::ObjectId; use toml::ToToml; @@ -209,3 +219,17 @@ impl AllResourcesById { }) } } + +fn deserialize_resources_toml( + toml_str: &str, +) -> anyhow::Result { + ::toml::from_str::( + &toml_str + // Escape the \ for the user on incoming. + // After deserialized by `toml` crate, the contents + // will match what would be passed on eg command line exactly. + .replace(r#"\"#, r#"\\"#), + ) + // the error without this comes through with multiple lines (\n) and looks bad + .map_err(|e| anyhow!("{e:#}")) +} diff --git a/bin/core/src/sync/remote.rs b/bin/core/src/sync/remote.rs index 7eeef06e4..6365e1ce6 100644 --- a/bin/core/src/sync/remote.rs +++ b/bin/core/src/sync/remote.rs @@ -56,7 +56,7 @@ pub async fn get_remote_resources( // ========== let mut resources = ResourcesToml::default(); let resources = if !sync.config.file_contents.is_empty() { - toml::from_str::(&sync.config.file_contents) + super::deserialize_resources_toml(&sync.config.file_contents) .context("failed to parse resource file contents") .map(|more| { extend_resources(