MCPcopy Create free account
hub / github.com/ermak-dev/cloudpub / run_script

Function run_script

client/src/upgrade/mod.rs:375–528  ·  view source on GitHub ↗
(
    script_name: &str,
    script_template: &str,
    replacements: Vec<(&str, String)>,
    is_service: bool,
)

Source from the content-addressed store, hash-verified

373/// Generic function to run a script with given template and replacements
374#[allow(unused_variables)]
375fn run_script(
376 script_name: &str,
377 script_template: &str,
378 replacements: Vec<(&str, String)>,
379 is_service: bool,
380) -> Result<()> {
381 use anyhow::Context;
382 use tracing::{debug, info};
383
384 // Use cache directory for updates instead of temp
385 let cache_dir = get_cache_dir("updates")?;
386 std::fs::create_dir_all(&cache_dir).context("Failed to create updates cache directory")?;
387
388 let timestamp = format!(
389 "{}_{}",
390 std::process::id(),
391 std::time::SystemTime::now()
392 .duration_since(std::time::UNIX_EPOCH)
393 .unwrap_or_default()
394 .as_secs()
395 );
396
397 #[cfg(windows)]
398 let script_path = cache_dir.join(format!("{}_{}.bat", script_name, timestamp));
399 #[cfg(unix)]
400 let script_path = cache_dir.join(format!("{}_{}.sh", script_name, timestamp));
401
402 debug!("Creating script: {}", script_path.display());
403
404 // Apply all replacements to the template
405 let mut script_content = script_template.to_string();
406 for (placeholder, value) in replacements {
407 script_content = script_content.replace(placeholder, &value);
408 }
409
410 std::fs::write(&script_path, script_content).context("Failed to create script")?;
411
412 info!("Script created at: {}", script_path.display());
413
414 // Always make script executable on Unix
415 #[cfg(unix)]
416 {
417 use std::os::unix::fs::PermissionsExt;
418 let mut perms = std::fs::metadata(&script_path)?.permissions();
419 perms.set_mode(0o755);
420 std::fs::set_permissions(&script_path, perms)?;
421 }
422
423 // Execute the script
424 #[cfg(windows)]
425 {
426 use std::os::windows::process::CommandExt;
427 std::process::Command::new("cmd")
428 .args(&["/C", &script_path.to_string_lossy()])
429 .creation_flags(0x08000000) // CREATE_NO_WINDOW
430 .spawn()
431 .context("Failed to execute script")?;
432 }

Callers 5

run_upgrade_scriptFunction · 0.85
install_msi_packageFunction · 0.85
install_dmg_packageFunction · 0.85
install_deb_packageFunction · 0.85
install_rpm_packageFunction · 0.85

Calls 2

get_cache_dirFunction · 0.85
spawnMethod · 0.80

Tested by

no test coverage detected