Skip to content

Commit

Permalink
print byok_config_path to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
olegklimov committed Sep 26, 2024
1 parent 900680b commit 14d4555
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ async fn main() {
}
}

yaml_configs_try_create_all(gcx.clone()).await;
let byok_config_path = yaml_configs_try_create_all(gcx.clone()).await;
if cmdline.only_create_yaml_configs {
println!("{}", byok_config_path);
std::process::exit(0);
}
if cmdline.address_url == "" {
Expand Down
31 changes: 21 additions & 10 deletions src/yaml_configs/create_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,62 @@ use crate::global_context::GlobalContext;

const DEFAULT_CHECKSUM_FILE: &str = "default-checksums.yaml";

pub async fn yaml_configs_try_create_all(gcx: Arc<ARwLock<GlobalContext>>)
pub async fn yaml_configs_try_create_all(gcx: Arc<ARwLock<GlobalContext>>) -> String
{
let mut results = Vec::new();
let files = vec![
("bring-your-own-key.yaml", crate::caps::BRING_YOUR_OWN_KEY_SAMPLE),
("customization.yaml", crate::yaml_configs::customization_compiled_in::COMPILED_IN_INITIAL_USER_YAML),
("privacy.yaml", crate::privacy_compiled_in::COMPILED_IN_INITIAL_PRIVACY_YAML),
("integrations.yaml", crate::integrations::INTEGRATIONS_DEFAULT_YAML),
];
for (file_name, content) in files {
if let Err(e) = yaml_file_exists_or_create(gcx.clone(), file_name, content).await {
tracing::warn!("{}", e);
match _yaml_file_exists_or_create(gcx.clone(), file_name, content).await {
Ok(result) => results.push(result),
Err(e) => {
tracing::warn!("{}", e);
results.push(format!("Error processing {}: {}", file_name, e));
}
}
}
results[0].clone() // path to bring-your-own-key.yaml, relied upon by first run procedure
}


async fn yaml_file_exists_or_create(gcx: Arc<ARwLock<GlobalContext>>, config_name: &str, compiled_in: &str) -> Result<(), String>
async fn _yaml_file_exists_or_create(gcx: Arc<ARwLock<GlobalContext>>, config_name: &str, the_default: &str) -> Result<String, String>
{
let cache_dir = gcx.read().await.cache_dir.clone();
let config_path = cache_dir.join(config_name);
let config_path_str = config_path.to_string_lossy().to_string();

let checksums_dict = read_checksums(&cache_dir).await?;
let new_checksum = calculate_checksum(compiled_in);

if config_path.exists() {
let existing_content = tokio::fs::read_to_string(&config_path).await
.map_err(|e| format!("failed to read {}: {}", config_name, e))?;
if existing_content == compiled_in {
return Ok(());
if existing_content == the_default {
// normal exit, content == default
return Ok(config_path_str);
}
let existing_checksum = calculate_checksum(&existing_content);
if existing_checksum == checksums_dict.get(config_name).map(|s| s.as_str()).unwrap_or("") {
tracing::info!("\n * * * detected that {} is a default config from a previous version of this binary, no changes made by human, overwrite * * *\n", config_path.display());
} else {
return Ok(());
// normal exit, config changed by user
return Ok(config_path_str);
}
}

let mut f = File::create(&config_path).await
.map_err(|e| format!("failed to create {}: {}", config_name, e))?;
f.write_all(compiled_in.as_bytes()).await
f.write_all(the_default.as_bytes()).await
.map_err(|e| format!("failed to write into {}: {}", config_name, e))?;
tracing::info!("created {}", config_path.display());

let new_checksum = calculate_checksum(the_default);
update_checksum(&cache_dir, config_name.to_string(), &new_checksum).await?;
Ok(())

Ok(config_path_str)
}

fn calculate_checksum(content: &str) -> String {
Expand Down

0 comments on commit 14d4555

Please sign in to comment.