init: Move some legacy script complexity into Script::from_str

This commit is contained in:
bjorn3
2026-04-11 15:51:54 +02:00
parent dd1c590131
commit ee8109fe1d
3 changed files with 23 additions and 31 deletions
+1 -3
View File
@@ -84,7 +84,7 @@ impl Scheduler {
fn run(unit: &mut Unit, config: &mut InitConfig) {
match &unit.kind {
UnitKind::LegacyScript { script } => {
for cmd in script.0.clone() {
for cmd in script.clone() {
if config.log_debug {
eprintln!("init: running: {cmd:?}");
}
@@ -118,8 +118,6 @@ fn run(unit: &mut Unit, config: &mut InitConfig) {
fn run_command(cmd: Command, config: &mut InitConfig) {
match cmd {
Command::RequiresWeak(_) => {} // handled by unit parsing code
Command::Nothing => {}
Command::Service(service) => {
if config.skip_cmd.contains(&service.cmd) {
eprintln!(
+19 -18
View File
@@ -12,11 +12,12 @@ pub fn subst_env<'a>(arg: &str) -> String {
}
}
pub struct Script(pub Vec<Command>);
pub struct Script(pub Vec<Command>, pub Vec<UnitId>);
impl Script {
pub fn from_str(config: &str, errors: &mut Vec<String>) -> io::Result<Script> {
let mut cmds = vec![];
let mut requires_weak = vec![];
for line_raw in config.lines() {
let line = line_raw.trim();
@@ -26,57 +27,57 @@ impl Script {
let args = line.split(' ').map(subst_env);
match Command::parse(args) {
Ok(cmd) => cmds.push(cmd),
match Command::parse(args, &mut requires_weak) {
Ok(None) => {}
Ok(Some(cmd)) => cmds.push(cmd),
Err(err) => errors.push(err),
}
}
Ok(Script(cmds))
Ok(Script(cmds, requires_weak))
}
}
#[derive(Clone, Debug)]
pub enum Command {
// Dependencies
RequiresWeak(Vec<UnitId>),
// Service
Service(Service),
// Misc
Nothing,
}
impl Command {
fn parse(mut args: impl Iterator<Item = String>) -> Result<Command, String> {
fn parse(
mut args: impl Iterator<Item = String>,
requires_weak: &mut Vec<UnitId>,
) -> Result<Option<Command>, String> {
let Some(cmd) = args.next() else {
return Ok(Command::Nothing);
return Ok(None);
};
match cmd.as_str() {
"requires_weak" => Ok(Command::RequiresWeak(args.map(UnitId).collect::<Vec<_>>())),
"requires_weak" => {
requires_weak.extend(args.map(UnitId));
Ok(None)
}
"nowait" => {
let process = Process::parse(args)?;
Ok(Command::Service(Service {
Ok(Some(Command::Service(Service {
cmd: process.cmd,
args: process.args,
envs: process.envs,
inherit_envs: BTreeSet::new(),
type_: ServiceType::OneshotAsync,
}))
})))
}
_ => {
let process = Process::parse(iter::once(cmd).chain(args))?;
Ok(Command::Service(Service {
Ok(Some(Command::Service(Service {
cmd: process.cmd,
args: process.args,
envs: process.envs,
inherit_envs: BTreeSet::new(),
type_: ServiceType::Oneshot,
}))
})))
}
}
}
+3 -10
View File
@@ -132,7 +132,7 @@ fn true_bool() -> bool {
}
pub enum UnitKind {
LegacyScript { script: Script },
LegacyScript { script: Vec<Command> },
Service { service: Service },
Target {},
}
@@ -182,23 +182,16 @@ impl Unit {
let Some(ext) = config_path.extension().map(|ext| ext.to_str().unwrap()) else {
let script = Script::from_str(&config, errors)?;
let mut requires_weak = vec![];
for command in &script.0 {
match command {
Command::RequiresWeak(deps) => requires_weak.extend(deps.into_iter().cloned()),
_ => {}
}
}
return Ok(Unit {
id,
info: UnitInfo {
description: None,
default_dependencies: true,
requires_weak,
requires_weak: script.1,
condition_architecture: None,
condition_board: None,
},
kind: UnitKind::LegacyScript { script },
kind: UnitKind::LegacyScript { script: script.0 },
});
};