Add indicator for scripts if they should be run in a different root

main
trivernis 2 years ago committed by Michal Stopyra
parent 9dff21dc53
commit a5468c8217

736
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,17 +16,17 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.0.17", features = ["derive"] }
clap = { version = "4.0.24", features = ["derive"] }
color-eyre = "0.6.2"
dotenv = "0.15.0"
embed-nu = "0.3.3"
embed-nu = "0.3.5"
lazy_static = "1.4.0"
libc = "0.2.135"
libc = "0.2.137"
paste = "1.0.9"
rusty-value = { version = "0.6.0", features = ["derive", "json"] }
serde = { version = "1.0.145", features = ["derive"] }
serde_json = "1.0.86"
sys-mount = "1.5.1"
serde = { version = "1.0.147", features = ["derive"] }
serde_json = "1.0.87"
sys-mount = "2.0.1"
thiserror = "1.0.37"
tokio = { version = "1.21.2", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
toml = "0.5.9"
@ -36,4 +36,4 @@ valico = "3.6.1"
[build-dependencies]
cargo_toml = "0.13.0"
serde = { version = "1.0.145", features = ["derive"] }
serde = { version = "1.0.147", features = ["derive"] }

@ -7,10 +7,8 @@ use crate::{distro::OSConfig, error::ScriptError, utils::CFG_PATH};
#[derive(Clone)]
pub struct ExecBuilder {
script: PathBuf,
script_contents: String,
os_config: OSConfig,
task_config: embed_nu::Value,
ctx: Context,
}
impl ExecBuilder {
@ -21,36 +19,38 @@ impl ExecBuilder {
) -> Result<Self, ScriptError> {
let script_contents = Self::get_script_contents(&script)?;
Ok(Self {
script,
script_contents,
os_config,
task_config,
})
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn exec(self) -> Result<(), ScriptError> {
let mut ctx = Context::builder()
.with_command_groups(CommandGroupConfig::default().all_groups(true))?
.add_parent_env_vars()
.add_var("TRM_CONFIG", self.os_config)?
.add_script(self.script_contents)?
.add_var("TRM_CONFIG", os_config)?
.add_script(script_contents)?
.build()?;
if ctx.has_fn("main") {
let pipeline = ctx.call_fn(
"main",
vec![Argument::Positional(self.task_config.into_expression())],
)?;
ctx.print_pipeline_stderr(pipeline)?;
Ok(())
if !ctx.has_fn("main") {
Err(ScriptError::MissingMain(script))
} else {
Err(ScriptError::MissingMain(self.script))
Ok(Self { ctx, task_config })
}
}
#[tracing::instrument(level = "trace", skip_all)]
pub fn exec(mut self) -> Result<(), ScriptError> {
let pipeline = self.ctx.call_fn(
"main",
vec![Argument::Positional(self.task_config.into_expression())],
)?;
self.ctx.print_pipeline_stderr(pipeline)?;
Ok(())
}
/// Returns if the script needs to be run inside the new root
pub fn requires_chroot(&self) -> bool {
self.ctx
.get_var("run_in_chroot")
.and_then(|v| v.as_bool().ok())
.unwrap_or(false)
}
#[tracing::instrument(level = "trace", skip_all)]
fn get_script_contents(path: &Path) -> Result<String, ScriptError> {
let path = CFG_PATH.join(path);

@ -1,5 +1,3 @@
use std::process::Command;
use crate::{
distro::{distro_config::DistroConfig, OSConfig},
error::AppResult,
@ -66,22 +64,14 @@ impl TaskExecutor {
pub async fn execute(&mut self) -> AppResult<()> {
self.tasks.sort_by(Task::compare);
let chroot = Chroot::create(&*ROOT_MNT).await?;
chroot
.run(move || {
Command::new("pacman")
.args(["-S", "--noconfirm", "neofetch"])
.spawn()
.unwrap()
.wait()
.unwrap();
Command::new("neofetch").spawn().unwrap().wait().unwrap();
})
.await
.unwrap()?;
for task in &self.tasks {
if let Some(up_task) = task.up(&self.os_config)? {
up_task.exec()?;
if up_task.requires_chroot() {
chroot.run(|| up_task.exec()).await.unwrap()??;
} else {
up_task.exec()?;
}
}
}

Loading…
Cancel
Save