[BROKEN] Add missing type definitions for config

config-extension
trivernis 2 years ago
parent a8426cb4f9
commit 3f85c87202
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -17,11 +17,7 @@ pub struct BaseConfig {
pub desktop: DesktopConfig,
pub users: UsersConfig,
pub root_user: RootUserConfig,
pub unakite: Option<UnakiteConfig>,
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<String>,
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<Partition>),
}
#[derive(Clone, Debug, Deserialize, RustyValue)]
pub struct Partition {
pub mountpoint: PathBuf,
pub blockdevice: PathBuf,
pub filesystem: Option<FileSystem>,
}
#[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<String>;
#[derive(Clone, Debug, RustyValue, Deserialize)]
pub struct KernelConfig {
pub default: Kernel,
pub additional: Vec<Kernel>,
}
#[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<User>,
}
#[derive(Clone, Debug, Deserialize, RustyValue)]
pub struct User {
pub name: String,
pub password: String,
pub sudoer: bool,
pub shell: String,
}

@ -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;

@ -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,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<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,
})
}
}
#[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<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(())
}
}

@ -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<P: AsRef<Path>>(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() {

Loading…
Cancel
Save