Add layout part of create_partitiosn and custom commands

main^2
trivernis 2 years ago
parent 4ad2700f06
commit eecaa70b59
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

754
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -16,25 +16,25 @@ path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = { version = "4.1.6", features = ["derive"] }
clap = { version = "4.1.8", features = ["derive"] }
color-eyre = "0.6.2"
dotenv = "0.15.0"
embed-nu = "0.5.1"
embed-nu = "0.5.4"
lazy_static = "1.4.0"
libc = "0.2.139"
libc = "0.2.140"
miette = { version = "5.5.0", features = ["fancy"] }
paste = "1.0.11"
paste = "1.0.12"
rusty-value = { version = "0.6.0", features = ["derive", "json"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
serde = { version = "1.0.155", features = ["derive"] }
serde_json = "1.0.94"
sys-mount = "2.0.2"
thiserror = "1.0.38"
tokio = { version = "1.25.0", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
thiserror = "1.0.39"
tokio = { version = "1.26.0", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
toml = "0.7.2"
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
valico = "3.6.1"
[build-dependencies]
cargo_toml = "0.15.2"
serde = { version = "1.0.152", features = ["derive"] }
serde = { version = "1.0.155", features = ["derive"] }

@ -1,4 +1,49 @@
module auto_partition {
export def efi [device: string] {
info "Creating efi partitions"
efi_layout $device
}
def efi_layout [device: string] {
run parted -s $device mklabel gpt
debug "Partition table created"
run parted -s $device mkpart fat32 0 300
debug "EFI partition created"
run parted -s $device mkpart primary btrfs 512MIB 100%
debug "Root partition created"
}
export def bios [device: string] {
debug "Creating bios partitions"
bios_layout $device
}
def bios_layout [device: string] {
parted -s $device mklabel msdos
parted -s $device mkpart primary ext4 1MIB 512MIB
parted -s $device mkpart primary btrfs 512MIB 100%
}
}
# Applies all system changes of `create-partitions`
def main [cfg] {
echo "Executing up task `create-partitions` with config" $cfg
debug $"Creating partitions with config ($cfg)"
if $cfg.partitions == "Auto" {
info "Creating partitions automatically..."
use auto_partition
if $cfg.efi_partition {
auto_partition efi $cfg.device
} else {
auto_partition bios $cfg.device
}
} else {
info "Creating partitions manually"
}
info "Partitions created"
}

@ -15,6 +15,9 @@ const VERSION: &str = concat!(
pub struct Args {
#[command(subcommand)]
pub command: Command,
#[arg(long)]
pub verbose: bool,
}
#[derive(Debug, Clone, Subcommand)]

@ -4,6 +4,8 @@ use miette::{Context, IntoDiagnostic, Result};
use rusty_value::into_json::{EnumRepr, IntoJson, IntoJsonOptions};
use tokio::fs;
use tourmaline::{distro::OSConfig, generate_script_files};
use tracing::metadata::LevelFilter;
use tracing_subscriber::fmt::format::FmtSpan;
mod args;
@ -14,6 +16,12 @@ async fn main() -> miette::Result<()> {
let _ = dotenv::dotenv();
let args = Args::parse();
if args.verbose {
init_tracing(LevelFilter::DEBUG);
} else {
init_tracing(LevelFilter::INFO);
}
match args.command {
Command::InstallFromConfig(args) => install_from_config(args).await,
Command::GenerateScripts(args) => generate_scripts(args).await,
@ -51,3 +59,12 @@ async fn generate_empty_config(args: CreateEmptyConfigArgs) -> Result<()> {
Ok(())
}
fn init_tracing(max_level: LevelFilter) {
tracing_subscriber::fmt::SubscriberBuilder::default()
.with_max_level(max_level)
.with_writer(std::io::stderr)
.with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
.compact()
.init();
}

@ -0,0 +1,36 @@
use embed_nu::{
nu_protocol::{engine::Command, Signature, SyntaxShape},
CallExt, PipelineData,
};
#[derive(Clone)]
pub struct DebugCommand;
impl Command for DebugCommand {
fn name(&self) -> &str {
"debug"
}
fn signature(&self) -> embed_nu::nu_protocol::Signature {
Signature::new("debug")
.rest("rest", SyntaxShape::Any, "the message to print")
.category(embed_nu::nu_protocol::Category::Custom("Tourmaline".into()))
}
fn usage(&self) -> &str {
"Prints the given message and values with debug severity (only when --verbose is passed to tourmaline)"
}
fn run(
&self,
engine_state: &embed_nu::nu_protocol::engine::EngineState,
stack: &mut embed_nu::nu_protocol::engine::Stack,
call: &embed_nu::nu_protocol::ast::Call,
_input: embed_nu::PipelineData,
) -> Result<embed_nu::PipelineData, embed_nu::nu_protocol::ShellError> {
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
tracing::debug!("{}", args.join(" "));
Ok(PipelineData::empty())
}
}

@ -0,0 +1,36 @@
use embed_nu::{
nu_protocol::{engine::Command, Signature, SyntaxShape},
CallExt, PipelineData,
};
#[derive(Clone)]
pub struct InfoCommand;
impl Command for InfoCommand {
fn name(&self) -> &str {
"info"
}
fn signature(&self) -> embed_nu::nu_protocol::Signature {
Signature::new("info")
.rest("rest", SyntaxShape::Any, "the message to print")
.category(embed_nu::nu_protocol::Category::Custom("Tourmaline".into()))
}
fn usage(&self) -> &str {
"Prints the given message and values with info severity"
}
fn run(
&self,
engine_state: &embed_nu::nu_protocol::engine::EngineState,
stack: &mut embed_nu::nu_protocol::engine::Stack,
call: &embed_nu::nu_protocol::ast::Call,
_input: embed_nu::PipelineData,
) -> Result<embed_nu::PipelineData, embed_nu::nu_protocol::ShellError> {
let args: Vec<String> = call.rest(engine_state, stack, 0)?;
tracing::info!("{}", args.join(" "));
Ok(PipelineData::empty())
}
}

@ -0,0 +1,6 @@
mod debug;
mod info;
mod run;
pub use debug::*;
pub use info::*;
pub use run::*;

@ -0,0 +1,66 @@
use std::{
io::Write,
process::{Command, Stdio},
};
use embed_nu::{
nu_protocol::{ShellError, Signature, SyntaxShape},
CallExt, PipelineData,
};
#[derive(Clone)]
pub struct RunCommand;
impl embed_nu::nu_protocol::engine::Command for RunCommand {
fn name(&self) -> &str {
"run"
}
fn signature(&self) -> embed_nu::nu_protocol::Signature {
Signature::new("run")
.required("executable", SyntaxShape::String, "The executable to run")
.rest(
"rest",
SyntaxShape::String,
"the args for the given command",
)
.allows_unknown_args()
.category(embed_nu::nu_protocol::Category::Custom("Tourmalin".into()))
}
fn usage(&self) -> &str {
"run <executable> [...<args>]"
}
fn run(
&self,
engine_state: &embed_nu::nu_protocol::engine::EngineState,
stack: &mut embed_nu::nu_protocol::engine::Stack,
call: &embed_nu::nu_protocol::ast::Call,
input: embed_nu::PipelineData,
) -> Result<embed_nu::PipelineData, embed_nu::nu_protocol::ShellError> {
let executable: String = call.req(engine_state, stack, 0)?;
let args: Vec<String> = call.rest(engine_state, stack, 1)?;
tracing::debug!("Running {executable} {}", args.join(" "));
let mut cmd = Command::new(&executable)
.args(args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::piped())
.spawn()?;
let mut stdin = cmd.stdin.take().unwrap();
stdin.write_all(input.collect_string_strict(call.span())?.0.as_bytes())?;
if cmd.wait().is_err() {
Err(ShellError::ExternalCommand(
executable,
String::from("Is it written correctly?"),
call.span(),
))
} else {
Ok(PipelineData::empty())
}
}
}

@ -6,6 +6,8 @@ use std::fs;
use crate::{distro::OSConfig, error::ScriptError, utils::CFG_PATH};
use miette::{Context, IntoDiagnostic, Result};
use super::commands::{DebugCommand, InfoCommand, RunCommand};
#[derive(Clone)]
pub struct ExecBuilder {
task_config: embed_nu::Value,
@ -21,15 +23,19 @@ impl ExecBuilder {
let script_contents = Self::get_script_contents(&script)?;
let mut ctx = embed_nu::Context::builder()
.with_command_groups(CommandGroupConfig::default().all_groups(true))
.into_diagnostic()?
.with_command_groups(CommandGroupConfig::default().all_groups(true))?
.add_command(RunCommand)?
.add_command(InfoCommand)?
.add_command(DebugCommand)?
.add_env_var("PWD", std::env::var("PWD").unwrap_or(String::from("/")))
.add_env_var(
"PATH",
std::env::var("PATH").unwrap_or(String::from("/usr/bin")),
)
.add_parent_env_vars()
.add_var("TRM_CONFIG", os_config)
.into_diagnostic()?
.add_script(script_contents)
.into_diagnostic()?
.build()
.into_diagnostic()?;
.add_var("TRM_CONFIG", os_config)?
.add_script(script_contents)?
.build()?;
if !ctx.has_fn("main") {
Err(ScriptError::MissingMain(script).into())
} else {
@ -39,14 +45,11 @@ impl ExecBuilder {
#[tracing::instrument(level = "trace", skip_all)]
pub fn exec(mut self) -> Result<()> {
let pipeline = self
.ctx
.call_fn(
let pipeline = self.ctx.call_fn(
"main",
vec![Argument::Positional(self.task_config.into_expression())],
)
.into_diagnostic()?;
self.ctx.print_pipeline_stderr(pipeline).into_diagnostic()?;
)?;
self.ctx.print_pipeline_stderr(pipeline)?;
Ok(())
}

@ -5,6 +5,7 @@ use crate::distro::OSConfig;
use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder};
pub mod base_task;
mod chrooting;
mod commands;
pub mod custom_task;
pub mod exec_builder;
pub mod task_executor;

Loading…
Cancel
Save