diff --git a/src/distro/os_config/base_config.rs b/src/distro/os_config/base_config.rs index 34e9956..4190a8d 100644 --- a/src/distro/os_config/base_config.rs +++ b/src/distro/os_config/base_config.rs @@ -17,11 +17,7 @@ pub struct BaseConfig { pub desktop: DesktopConfig, pub users: UsersConfig, pub root_user: RootUserConfig, - pub unakite: Option, pub extra_packages: ExtraPackages, - pub enable_timeshift: bool, - pub enable_flatpak: bool, - pub enable_zramd: bool, } impl BaseConfig { @@ -54,11 +50,115 @@ impl BaseConfig { root_user: RootUserConfig { password: String::new(), }, - unakite: None, extra_packages: Vec::new(), - enable_timeshift: false, - enable_flatpak: false, - enable_zramd: false, } } } + +#[derive(Clone, Deserialize, RustyValue, Debug)] +pub struct LocaleConfig { + pub locale: Vec, + pub keymap: String, + pub timezone: String, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct NetworkConfig { + pub hostname: String, + pub ipv6_loopback: bool, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct PartitionsConfig { + pub device: PathBuf, + pub efi_partition: bool, + pub partitions: Partitions, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub enum Partitions { + Auto, + Manual(Vec), +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct Partition { + pub mountpoint: PathBuf, + pub blockdevice: PathBuf, + pub filesystem: Option, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub enum FileSystem { + VFAT, + BFS, + CramFS, + Ext2, + Ext3, + Ext4, + FAT, + MSDOS, + XFS, + BTRFS, + Minix, + F2FS, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct BootloaderConfig { + pub preset: BootloaderPreset, + pub location: PathBuf, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub enum BootloaderPreset { + GrubEfi, + Legacy, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub enum DesktopConfig { + Onyx, + KdePlasma, + Mate, + Gnome, + Cinnamon, + Xfce, + Budgie, + Enlightenment, + Lxqt, + Sway, + I3Gaps, + HerbstluftWM, + AwesomeWM, + BSPWM, +} + +pub type ExtraPackages = Vec; + +#[derive(Clone, Debug, RustyValue, Deserialize)] +pub struct KernelConfig { + pub default: Kernel, + pub additional: Vec, +} + +#[derive(Clone, Debug, RustyValue, Deserialize)] +pub struct Kernel(pub String); + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct RootUserConfig { + pub password: String, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct UsersConfig { + pub users: Vec, +} + +#[derive(Clone, Debug, Deserialize, RustyValue)] +pub struct User { + pub name: String, + pub password: String, + pub sudoer: bool, + pub shell: String, +} diff --git a/src/lib.rs b/src/lib.rs index 4f078b8..ddf7a2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,10 @@ use scripting::{ loader::ScriptLoader, script::{NuScript, Script}, }; -use tasks::*; pub mod error; pub(crate) mod scripting; -pub mod tasks; + pub(crate) mod utils; pub use utils::generate_script_files; pub mod distro; diff --git a/src/task/base_task.rs b/src/task/base_task.rs new file mode 100644 index 0000000..d8d8de8 --- /dev/null +++ b/src/task/base_task.rs @@ -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 { + 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 { + 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 = get_all_base_tasks(); +} + +fn get_all_base_tasks() -> Vec { + 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), + ] +} diff --git a/src/task/base_tasks.rs b/src/task/base_tasks.rs deleted file mode 100644 index 4fe0da3..0000000 --- a/src/task/base_tasks.rs +++ /dev/null @@ -1,20 +0,0 @@ -use super::{BaseTask, Task}; -use lazy_static::lazy_static; - -lazy_static! { - pub static ref ALL_BASE_TASKS: Vec = get_all_base_tasks(); -} - -fn get_all_base_tasks() -> Vec { - 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), - ] -} diff --git a/src/task/custom_task.rs b/src/task/custom_task.rs new file mode 100644 index 0000000..b02b93f --- /dev/null +++ b/src/task/custom_task.rs @@ -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 { + 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 { + 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, + }) + } +} diff --git a/src/task/mod.rs b/src/task/mod.rs index 7fa6461..862d5c1 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -1,11 +1,8 @@ -use std::path::PathBuf; - -use embed_nu::IntoValue; - use crate::{distro::OSConfig, error::AppResult}; -use self::exec_builder::ExecBuilder; -pub mod base_tasks; +use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder}; +pub mod base_task; +pub mod custom_task; pub mod exec_builder; pub mod task_executor; @@ -37,109 +34,3 @@ impl TaskTrait for Task { } } } - -#[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 { - 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 { - 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, - }) - } -} - -#[derive(Clone, Debug)] -pub struct CustomTask { - config_key: String, - up_script: PathBuf, - down_script: PathBuf, -} - -impl TaskTrait for CustomTask { - fn up(&self, config: &OSConfig) -> AppResult { - 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 { - 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, - }) - } -} diff --git a/src/task/task_executor.rs b/src/task/task_executor.rs index f728790..3a39afc 100644 --- a/src/task/task_executor.rs +++ b/src/task/task_executor.rs @@ -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, } 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::>(); + 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(()) + } } diff --git a/src/utils.rs b/src/utils.rs index 610cbb0..de90d28 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -4,7 +4,7 @@ use std::{env, path::PathBuf}; use tokio::fs; use crate::error::AppResult; -use crate::tasks::all_tasks; +use crate::task::base_task::ALL_BASE_TASKS; const DEFAULT_CONFIG_DIR: &str = "/etc"; @@ -15,9 +15,8 @@ lazy_static::lazy_static! { pub async fn generate_script_files>(output: P) -> AppResult<()> { let output = output.as_ref(); - let tasks = all_tasks(); - - for (name, up_script, down_script) in tasks { + for task in &*ALL_BASE_TASKS { + task.up() let script_dir = output.join(&name); if !script_dir.exists() {