init: Introduce a json format for services

While I would have like to use toml, something inside the toml crate
seems to be using randomness, which is not yet available when init
starts.
This commit is contained in:
bjorn3
2026-02-25 20:18:42 +01:00
parent 4e772728ad
commit f364bf1f60
9 changed files with 38 additions and 9 deletions
+9 -2
View File
@@ -1,12 +1,13 @@
use std::collections::BTreeMap;
use std::env;
use std::ffi::OsString;
use std::ffi::{OsStr, OsString};
use std::io::Result;
use std::path::Path;
use std::{env, fs};
use libredox::flag::{O_RDONLY, O_WRONLY};
use crate::script::Command;
use crate::service::Service;
mod script;
mod service;
@@ -74,6 +75,12 @@ fn switch_root(prefix: &Path, etcdir: &Path, config: &mut InitConfig) {
}
fn run(file: &Path, config: &mut InitConfig) -> Result<()> {
if file.extension() == Some(OsStr::new("service")) {
let service: Service = serde_json::from_str(&fs::read_to_string(file).unwrap()).unwrap();
service.spawn(&config.envs);
return Ok(());
}
let (script, errors) = script::Script::from_file(file)?;
for error in errors {
+8 -2
View File
@@ -2,15 +2,21 @@ use std::collections::BTreeMap;
use std::ffi::OsString;
use std::process::Command;
#[derive(Debug)]
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Service {
pub cmd: String,
#[serde(default)]
pub args: Vec<String>,
#[serde(default)]
pub envs: BTreeMap<String, String>,
#[serde(rename = "type")]
pub type_: ServiceType,
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ServiceType {
#[default]
Notify,