From 4222fbf750ebdbf2de216b38d177aa30a27310cb Mon Sep 17 00:00:00 2001 From: rorka Date: Thu, 25 Jun 2026 13:08:01 +0300 Subject: install: add install_cmd field to ServiceEntry run it after service file write if init matches --- src/install.rs | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'src/install.rs') diff --git a/src/install.rs b/src/install.rs index 9f7d80d..48c9c05 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,4 +1,3 @@ -// install.rs use serde::{Deserialize, Serialize}; use std::fs; use std::io::{BufReader, Read, Write}; @@ -130,6 +129,8 @@ pub struct ServiceEntry { pub filepath: String, #[serde(default)] pub file: String, + #[serde(default)] + pub install_cmd: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -603,6 +604,37 @@ pub fn remove_services(pkg: &Package) { } } +fn run_service_install_cmd(cmd_template: &str, init: &str, pkg_name: &str) { + let cmd = cmd_template.replace("{init}", init); + let parts: Vec<&str> = cmd.split_whitespace().collect(); + if parts.is_empty() { + return; + } + match std::process::Command::new(parts[0]) + .args(&parts[1..]) + .status() + { + Ok(status) if status.success() => { + println!( + "{} Service install command OK for '{}': {}", + "[ror]".green().bold(), pkg_name, cmd + ); + } + Ok(status) => { + eprintln!( + "{} Service install command exited with {} for '{}': {}", + "[ror]".yellow().bold(), status, pkg_name, cmd + ); + } + Err(e) => { + eprintln!( + "{} Failed to run service install command for '{}': {} — {}", + "[ror]".red().bold(), pkg_name, cmd, e + ); + } + } +} + pub fn install_services(pkg: &Package) { let current_init = get_current_init_in_root(Path::new("/")); for service in &pkg.services { @@ -619,7 +651,7 @@ pub fn install_services(pkg: &Package) { "[ror]".yellow().bold(), pkg.name, service.init, init ); } - Some(_) => { + Some(init) => { let path = std::path::Path::new(&service.filepath); if let Some(parent) = path.parent() { if let Err(e) = std::fs::create_dir_all(parent) { @@ -631,10 +663,15 @@ pub fn install_services(pkg: &Package) { } } match std::fs::write(path, &service.file) { - Ok(_) => println!( - "{} Created service for '{}': {}", - "[ror]".green().bold(), pkg.name, service.filepath - ), + Ok(_) => { + println!( + "{} Created service for '{}': {}", + "[ror]".green().bold(), pkg.name, service.filepath + ); + if let Some(cmd) = &service.install_cmd { + run_service_install_cmd(cmd, init, &pkg.name); + } + } Err(e) => eprintln!( "{} Failed to write service file '{}': {}", "[ror]".red().bold(), service.filepath, e -- cgit v1.2.3