[BROKEN] Add missing type definitions for config
parent
a8426cb4f9
commit
3f85c87202
@ -0,0 +1,102 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{distro::OSConfig, error::AppResult};
|
||||
|
||||
use super::{exec_builder::ExecBuilder, Task, TaskTrait};
|
||||
use embed_nu::IntoValue;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum BaseTask {
|
||||
ConfigureLocale,
|
||||
ConfigureNetwork,
|
||||
CreatePartitions,
|
||||
InstallBase,
|
||||
InstallBootloader,
|
||||
InstallDesktop,
|
||||
InstallExtraPackages,
|
||||
SetupRootUser,
|
||||
SetupUsers,
|
||||
}
|
||||
|
||||
impl BaseTask {
|
||||
fn config_key(&self) -> Option<&'static str> {
|
||||
let field = match self {
|
||||
BaseTask::ConfigureLocale => "locale",
|
||||
BaseTask::ConfigureNetwork => "network",
|
||||
BaseTask::CreatePartitions => "partitions",
|
||||
BaseTask::InstallBootloader => "bootloader",
|
||||
BaseTask::InstallDesktop => "desktop",
|
||||
BaseTask::InstallExtraPackages => "extra_packages",
|
||||
BaseTask::SetupRootUser => "root_user",
|
||||
BaseTask::SetupUsers => "users",
|
||||
_ => return None,
|
||||
};
|
||||
Some(field)
|
||||
}
|
||||
|
||||
fn task_name(&self) -> &'static str {
|
||||
match self {
|
||||
BaseTask::ConfigureLocale => "configure-locale",
|
||||
BaseTask::ConfigureNetwork => "configure-network",
|
||||
BaseTask::CreatePartitions => "create-partitions",
|
||||
BaseTask::InstallBase => "install-base",
|
||||
BaseTask::InstallBootloader => "install-bootloader",
|
||||
BaseTask::InstallDesktop => "install-desktop",
|
||||
BaseTask::InstallExtraPackages => "install-extra-packages",
|
||||
BaseTask::SetupRootUser => "setup-root-user",
|
||||
BaseTask::SetupUsers => "setup-users",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TaskTrait for BaseTask {
|
||||
fn up(&self, config: &OSConfig) -> AppResult<ExecBuilder> {
|
||||
let script = PathBuf::from(self.task_name()).join("up.nu");
|
||||
|
||||
let task_config = if let Some(key) = self.config_key() {
|
||||
config.get_nu_value(key)?
|
||||
} else {
|
||||
Option::<()>::None.into_value()
|
||||
};
|
||||
|
||||
Ok(ExecBuilder {
|
||||
script,
|
||||
os_config: config.to_owned(),
|
||||
task_config,
|
||||
})
|
||||
}
|
||||
|
||||
fn down(&self, config: &OSConfig) -> AppResult<ExecBuilder> {
|
||||
let script = PathBuf::from(self.task_name()).join("down.nu");
|
||||
let task_config = if let Some(key) = self.config_key() {
|
||||
config.get_nu_value(key)?
|
||||
} else {
|
||||
Option::<()>::None.into_value()
|
||||
};
|
||||
|
||||
Ok(ExecBuilder {
|
||||
script,
|
||||
os_config: config.to_owned(),
|
||||
task_config,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ALL_BASE_TASKS: Vec<Task> = get_all_base_tasks();
|
||||
}
|
||||
|
||||
fn get_all_base_tasks() -> Vec<Task> {
|
||||
vec![
|
||||
Task::Base(BaseTask::ConfigureLocale),
|
||||
Task::Base(BaseTask::ConfigureNetwork),
|
||||
Task::Base(BaseTask::CreatePartitions),
|
||||
Task::Base(BaseTask::InstallBase),
|
||||
Task::Base(BaseTask::InstallBootloader),
|
||||
Task::Base(BaseTask::InstallDesktop),
|
||||
Task::Base(BaseTask::InstallExtraPackages),
|
||||
Task::Base(BaseTask::SetupRootUser),
|
||||
Task::Base(BaseTask::SetupUsers),
|
||||
]
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
use super::{BaseTask, Task};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ALL_BASE_TASKS: Vec<Task> = get_all_base_tasks();
|
||||
}
|
||||
|
||||
fn get_all_base_tasks() -> Vec<Task> {
|
||||
vec![
|
||||
Task::Base(BaseTask::ConfigureLocale),
|
||||
Task::Base(BaseTask::ConfigureNetwork),
|
||||
Task::Base(BaseTask::CreatePartitions),
|
||||
Task::Base(BaseTask::InstallBase),
|
||||
Task::Base(BaseTask::InstallBootloader),
|
||||
Task::Base(BaseTask::InstallDesktop),
|
||||
Task::Base(BaseTask::InstallExtraPackages),
|
||||
Task::Base(BaseTask::SetupRootUser),
|
||||
Task::Base(BaseTask::SetupUsers),
|
||||
]
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{distro::OSConfig, error::AppResult};
|
||||
|
||||
use super::{exec_builder::ExecBuilder, TaskTrait};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CustomTask {
|
||||
config_key: String,
|
||||
up_script: PathBuf,
|
||||
down_script: PathBuf,
|
||||
}
|
||||
|
||||
impl CustomTask {
|
||||
pub fn from_name_and_key(name: String, config_key: String) -> Self {
|
||||
let base_path = PathBuf::from(name);
|
||||
Self {
|
||||
config_key,
|
||||
up_script: base_path.join("up.nu"),
|
||||
down_script: base_path.join("down.nu"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TaskTrait for CustomTask {
|
||||
fn up(&self, config: &OSConfig) -> AppResult<ExecBuilder> {
|
||||
let task_config = config.get_nu_value(&self.config_key)?;
|
||||
|
||||
Ok(ExecBuilder {
|
||||
script: self.up_script.to_owned(),
|
||||
os_config: config.to_owned(),
|
||||
task_config,
|
||||
})
|
||||
}
|
||||
|
||||
fn down(&self, config: &OSConfig) -> AppResult<ExecBuilder> {
|
||||
let task_config = config.get_nu_value(&self.config_key)?;
|
||||
|
||||
Ok(ExecBuilder {
|
||||
script: self.down_script.to_owned(),
|
||||
os_config: config.to_owned(),
|
||||
task_config,
|
||||
})
|
||||
}
|
||||
}
|
@ -1,24 +1,55 @@
|
||||
use crate::distro::OSConfig;
|
||||
use crate::{
|
||||
distro::{config::Config, OSConfig},
|
||||
error::AppResult,
|
||||
};
|
||||
|
||||
use super::{base_tasks::ALL_BASE_TASKS, Task};
|
||||
use super::{base_task::ALL_BASE_TASKS, custom_task::CustomTask, Task, TaskTrait};
|
||||
|
||||
pub struct TaskExecutor {
|
||||
config: OSConfig,
|
||||
config: Config,
|
||||
os_config: OSConfig,
|
||||
tasks: Vec<Task>,
|
||||
}
|
||||
|
||||
impl TaskExecutor {
|
||||
pub fn new(config: OSConfig) -> Self {
|
||||
pub fn new(os_config: OSConfig, config: Config) -> Self {
|
||||
Self {
|
||||
config,
|
||||
os_config,
|
||||
tasks: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds all base tasks to the executor
|
||||
pub fn with_base_tasks(&mut self) -> &mut Self {
|
||||
let mut base_tasks = (*ALL_BASE_TASKS).clone();
|
||||
self.tasks.append(&mut base_tasks);
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds all custom tasks to the executor
|
||||
pub fn with_custom_tasks(&mut self) -> &mut Self {
|
||||
let mut custom_tasks = self
|
||||
.config
|
||||
.tasks
|
||||
.iter()
|
||||
.map(|(name, task)| {
|
||||
CustomTask::from_name_and_key(name.to_owned(), task.config_field.to_owned())
|
||||
})
|
||||
.map(Task::Custom)
|
||||
.collect::<Vec<_>>();
|
||||
self.tasks.append(&mut custom_tasks);
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Executes all tasks
|
||||
pub async fn execute(&mut self) -> AppResult<()> {
|
||||
for task in &self.tasks {
|
||||
task.up(&self.os_config)?.exec().await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue