Move os config to different directory

config-extension
trivernis 2 years ago
parent 8f063cdef1
commit 23728d05ff
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

3
.gitignore vendored

@ -11,3 +11,6 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
.env
# Misc
test-config.json

255
Cargo.lock generated

@ -12,6 +12,17 @@ dependencies = [
"regex",
]
[[package]]
name = "addr"
version = "0.11.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936697e9caf938eb2905036100edf8e1269da8291f8a02f5fe7b37073784eec0"
dependencies = [
"no-std-net",
"psl",
"psl-types",
]
[[package]]
name = "addr2line"
version = "0.17.0"
@ -33,7 +44,7 @@ version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"getrandom 0.2.7",
"once_cell",
"version_check",
]
@ -265,9 +276,9 @@ dependencies = [
[[package]]
name = "cargo_toml"
version = "0.12.4"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a621d5d6d6c8d086dbaf1fe659981da41a1b63c6bdbba30b4dbb592c6d3bd49"
checksum = "aa0e3586af56b3bfa51fca452bd56e8dbbbd5d8d81cbf0b7e4e35b695b537eb8"
dependencies = [
"serde",
"toml",
@ -334,7 +345,7 @@ checksum = "29c39203181991a7dd4343b8005bd804e7a9a37afb8ac070e43771e8c820bbde"
dependencies = [
"chrono",
"chrono-tz-build",
"phf",
"phf 0.11.1",
]
[[package]]
@ -344,8 +355,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f509c3a87b33437b05e2458750a0700e5bdd6956176773e6c7d6dd15a283a0c"
dependencies = [
"parse-zoneinfo",
"phf",
"phf_codegen",
"phf 0.11.1",
"phf_codegen 0.11.1",
]
[[package]]
@ -361,9 +372,9 @@ dependencies = [
[[package]]
name = "clap"
version = "4.0.14"
version = "4.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ea54a38e4bce14ff6931c72e5b3c43da7051df056913d4e7e1fcdb1c03df69d"
checksum = "06badb543e734a2d6568e19a40af66ed5364360b9226184926f89d229b4b4267"
dependencies = [
"atty",
"bitflags",
@ -756,9 +767,9 @@ checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]]
name = "embed-nu"
version = "0.3.0"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adf2e40b13c4f1e3cd0d5494d8a225c3d5064f1c83ff373b04e783b0df7b1c23"
checksum = "4291b7a0c6d69db167faca6bede4c16a07dff35fead4152b913d0f9bc9bcfb23"
dependencies = [
"nu-command",
"nu-engine",
@ -1045,6 +1056,17 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.7"
@ -1420,6 +1442,25 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "json-pointer"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fe841b94e719a482213cee19dd04927cf412f26d8dc84c5a446c081e49c2997"
dependencies = [
"serde_json",
]
[[package]]
name = "jsonway"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "effcb749443c905fbaef49d214f8b1049c240e0adb7af9baa0e201e625e4f9de"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -1735,6 +1776,15 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "no-std-net"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bcece43b12349917e096cddfa66107277f123e6c96a5aea78711dc601a47152"
dependencies = [
"serde",
]
[[package]]
name = "nom"
version = "1.2.4"
@ -1880,7 +1930,7 @@ dependencies = [
"pathdiff",
"powierza-coefficient",
"quick-xml 0.23.1",
"rand",
"rand 0.8.5",
"rayon",
"reedline",
"reqwest",
@ -1903,7 +1953,7 @@ dependencies = [
"unicode-segmentation",
"url",
"users 0.11.0",
"uuid",
"uuid 1.2.1",
"wax",
"windows",
]
@ -1977,7 +2027,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa9667e6ddf13e01a5f192caa8570b19f062dfe1fa70af6f28b2ab56c369d57"
dependencies = [
"nu-ansi-term",
"rand",
"rand 0.8.5",
]
[[package]]
@ -2337,13 +2387,32 @@ version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e"
[[package]]
name = "phf"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"
dependencies = [
"phf_shared 0.8.0",
]
[[package]]
name = "phf"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "928c6535de93548188ef63bb7c4036bd415cd8f36ad25af44b9789b2ee72a48c"
dependencies = [
"phf_shared",
"phf_shared 0.11.1",
]
[[package]]
name = "phf_codegen"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbffee61585b0411840d3ece935cce9cb6321f01c45477d30066498cd5e1a815"
dependencies = [
"phf_generator 0.8.0",
"phf_shared 0.8.0",
]
[[package]]
@ -2352,8 +2421,18 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a56ac890c5e3ca598bbdeaa99964edb5b0258a583a9eb6ef4e89fc85d9224770"
dependencies = [
"phf_generator",
"phf_shared",
"phf_generator 0.11.1",
"phf_shared 0.11.1",
]
[[package]]
name = "phf_generator"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17367f0cc86f2d25802b2c26ee58a7b23faeccf78a396094c13dced0d0182526"
dependencies = [
"phf_shared 0.8.0",
"rand 0.7.3",
]
[[package]]
@ -2362,8 +2441,17 @@ version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1181c94580fa345f50f19d738aaa39c0ed30a600d95cb2d3e23f94266f14fbf"
dependencies = [
"phf_shared",
"rand",
"phf_shared 0.11.1",
"rand 0.8.5",
]
[[package]]
name = "phf_shared"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7"
dependencies = [
"siphasher",
]
[[package]]
@ -2463,6 +2551,21 @@ dependencies = [
"rustix",
]
[[package]]
name = "psl"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07242622d9f4b9c1a6fe9c2691cf18ee7d34400a5eed2e1668c756bfaea93fb3"
dependencies = [
"psl-types",
]
[[package]]
name = "psl-types"
version = "2.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33cb294fe86a74cbcf50d4445b37da762029549ebeea341421c7c70370f86cac"
[[package]]
name = "pure-rust-locales"
version = "0.5.6"
@ -2507,6 +2610,20 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc",
"rand_pcg",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -2514,8 +2631,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_chacha 0.3.1",
"rand_core 0.6.4",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
]
[[package]]
@ -2525,7 +2652,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.4",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
]
[[package]]
@ -2534,7 +2670,25 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.7",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_pcg"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
@ -2576,7 +2730,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"getrandom 0.2.7",
"redox_syscall",
"thiserror",
]
@ -2780,11 +2934,12 @@ checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8"
[[package]]
name = "rusty-value"
version = "0.5.1"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48dca1efc1cbe6434e3ed1afb6ce7bb3152af7a0f568bb5f5ea316388e333c3e"
checksum = "eb7ccab1b447c1ba376a608ef1fd25e3b7872e9162a1147274c263e7675a7c98"
dependencies = [
"rusty-value-derive",
"serde_json",
]
[[package]]
@ -3363,12 +3518,14 @@ dependencies = [
"embed-nu",
"lazy_static",
"paste",
"rusty-value",
"serde",
"serde_json",
"thiserror",
"tokio",
"tracing",
"tracing-subscriber",
"valico",
]
[[package]]
@ -3551,6 +3708,15 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]]
name = "uritemplate-next"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcde98d1fc3f528255b1ecb22fb688ee0d23deb672a8c57127df10b98b4bd18c"
dependencies = [
"regex",
]
[[package]]
name = "url"
version = "2.3.1"
@ -3594,13 +3760,44 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom 0.2.7",
]
[[package]]
name = "uuid"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83"
dependencies = [
"getrandom",
"getrandom 0.2.7",
]
[[package]]
name = "valico"
version = "3.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "647856408e327686b6640397d19f60fac3e64c3bfaa6afc409da63ef7da45edb"
dependencies = [
"addr",
"base64",
"chrono",
"json-pointer",
"jsonway",
"percent-encoding",
"phf 0.8.0",
"phf_codegen 0.8.0",
"regex",
"serde",
"serde_json",
"uritemplate-next",
"url",
"uuid 0.8.2",
]
[[package]]
@ -3675,6 +3872,12 @@ dependencies = [
"try-lock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"

@ -16,19 +16,21 @@ 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.14", features = ["derive"] }
clap = { version = "4.0.17", features = ["derive"] }
color-eyre = "0.6.2"
dotenv = "0.15.0"
embed-nu = "0.3.0"
embed-nu = "0.3.1"
lazy_static = "1.4.0"
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"
thiserror = "1.0.37"
tokio = { version = "1.21.2", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
tracing = "0.1.37"
tracing-subscriber = "0.3.16"
valico = "3.6.1"
[build-dependencies]
cargo_toml = "0.12.4"
cargo_toml = "0.13.0"
serde = { version = "1.0.145", features = ["derive"] }

@ -26,6 +26,11 @@ pub enum Command {
/// Generates empty script files for the installation
#[command()]
GenerateScripts(GenerateScriptsArgs),
/// *For testing purposes only*
/// Generates the JSON for an empty config file
#[command()]
CreateEmptyConfig(GenerateEmptyConfigArgs),
}
#[derive(Debug, Clone, Parser)]
@ -41,3 +46,10 @@ pub struct GenerateScriptsArgs {
#[arg()]
pub path: PathBuf,
}
#[derive(Debug, Clone, Parser)]
pub struct GenerateEmptyConfigArgs {
/// The path to the empty configuration file
#[arg(default_value = "config.json")]
pub path: PathBuf,
}

@ -9,8 +9,12 @@ use crate::tasks::{
UsersConfig,
};
/// The base configuration of the operating system
/// This config alone should provide all required configuraiton
/// values to create a base distro installation while still being
/// distro agnostic.
#[derive(Clone, Debug, Deserialize, RustyValue)]
pub struct Config {
pub struct BaseConfig {
pub locale: LocaleConfig,
pub network: NetworkConfig,
pub partitions: PartitionsConfig,
@ -26,7 +30,7 @@ pub struct Config {
pub enable_zramd: bool,
}
impl Config {
impl BaseConfig {
pub(crate) fn empty() -> Self {
Self {
locale: LocaleConfig {

@ -0,0 +1,87 @@
mod base_config;
use std::collections::HashMap;
pub use base_config::BaseConfig;
use embed_nu::rusty_value::{
Fields, Float, HashablePrimitive, HashableValue, Integer, Primitive, RustyValue, Struct, Value,
};
use serde::Deserialize;
/// Represents the full configuration of the OS including extensions defined
/// by the distro
#[derive(Clone, Debug, Deserialize)]
pub struct OSConfig {
#[serde(flatten)]
pub base: BaseConfig,
#[serde(flatten)]
pub extended: HashMap<String, serde_json::Value>,
}
impl RustyValue for OSConfig {
fn into_rusty_value(self) -> embed_nu::rusty_value::Value {
let base = self.base.into_rusty_value();
let mut fields = if let Value::Struct(Struct { fields, .. }) = base {
if let Fields::Named(named) = fields {
named
} else {
panic!("Base fields don't have a name?!");
}
} else {
panic!("Base is not a struct?!");
};
let ext = self.extended.into_iter().map(ext_field_to_rusty_value);
fields.extend(ext);
Value::Struct(Struct {
name: String::from("Config"),
fields: Fields::Named(fields),
})
}
}
impl OSConfig {
pub fn empty() -> Self {
Self {
base: BaseConfig::empty(),
extended: HashMap::new(),
}
}
}
#[inline]
fn ext_field_to_rusty_value(
entry: (String, serde_json::Value),
) -> (String, embed_nu::rusty_value::Value) {
(entry.0, json_to_rusty_value(entry.1))
}
fn json_to_rusty_value(val: serde_json::Value) -> embed_nu::rusty_value::Value {
match val {
serde_json::Value::Null => Value::None,
serde_json::Value::Bool(b) => Value::Primitive(Primitive::Bool(b)),
serde_json::Value::Number(num) => Value::Primitive(if num.is_i64() {
Primitive::Integer(Integer::I64(num.as_i64().unwrap()))
} else if num.is_u64() {
Primitive::Integer(Integer::U64(num.as_u64().unwrap()))
} else {
Primitive::Float(Float::F64(num.as_f64().unwrap()))
}),
serde_json::Value::String(s) => Value::Primitive(Primitive::String(s)),
serde_json::Value::Array(a) => {
Value::List(a.into_iter().map(json_to_rusty_value).collect())
}
serde_json::Value::Object(o) => {
let vals = o
.into_iter()
.map(|(k, v)| {
(
HashableValue::Primitive(HashablePrimitive::String(k)),
json_to_rusty_value(v),
)
})
.collect::<HashMap<_, _>>();
Value::Map(vals)
}
}
}

@ -0,0 +1,3 @@
mod config;
pub use config::*;

@ -1,4 +1,4 @@
use config::Config;
use distro::OSConfig;
use error::{AppError, AppResult};
use scripting::{
loader::ScriptLoader,
@ -6,12 +6,12 @@ use scripting::{
};
use tasks::*;
pub mod config;
pub mod error;
pub(crate) mod scripting;
pub mod tasks;
pub(crate) mod utils;
pub use utils::generate_script_files;
pub mod distro;
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub enum TaskOperation {
@ -20,7 +20,7 @@ pub enum TaskOperation {
}
pub struct TaskExecutor {
config: Option<Config>,
config: Option<OSConfig>,
loader: ScriptLoader,
}
@ -40,7 +40,7 @@ macro_rules! task_executors {
impl TaskExecutor {
/// Creates a new task executor with a given config
pub fn with_config(config: Config) -> Self {
pub fn with_config(config: OSConfig) -> Self {
Self {
config: Some(config),
loader: ScriptLoader::new(),
@ -68,36 +68,39 @@ impl TaskExecutor {
#[tracing::instrument(level = "trace", skip(self))]
pub async fn install_from_config(&self) -> AppResult<()> {
let config = self.config.clone().ok_or(AppError::MissingConfig)?;
self.create_partitions(TaskOperation::Up, config.partitions)
let base_config = config.base;
self.create_partitions(TaskOperation::Up, base_config.partitions)
.await?;
self.install_base(TaskOperation::Up, ()).await?;
self.install_kernels(TaskOperation::Up, config.kernels)
self.install_kernels(TaskOperation::Up, base_config.kernels)
.await?;
self.install_bootloader(TaskOperation::Up, config.bootloader)
self.install_bootloader(TaskOperation::Up, base_config.bootloader)
.await?;
self.configure_locale(TaskOperation::Up, config.locale)
self.configure_locale(TaskOperation::Up, base_config.locale)
.await?;
self.configure_network(TaskOperation::Up, config.network)
self.configure_network(TaskOperation::Up, base_config.network)
.await?;
if config.enable_zramd {
if base_config.enable_zramd {
self.install_zramd(TaskOperation::Up, ()).await?;
}
if config.enable_timeshift {
if base_config.enable_timeshift {
self.install_timeshift(TaskOperation::Up, ()).await?;
}
if config.enable_flatpak {
if base_config.enable_flatpak {
self.install_flatpak(TaskOperation::Up, ()).await?;
}
self.setup_users(TaskOperation::Up, config.users).await?;
self.setup_root_user(TaskOperation::Up, config.root_user)
self.setup_users(TaskOperation::Up, base_config.users)
.await?;
self.setup_root_user(TaskOperation::Up, base_config.root_user)
.await?;
self.install_desktop(TaskOperation::Up, config.desktop)
self.install_desktop(TaskOperation::Up, base_config.desktop)
.await?;
self.install_extra_packages(TaskOperation::Up, config.extra_packages)
self.install_extra_packages(TaskOperation::Up, base_config.extra_packages)
.await?;
if let Some(unakite) = config.unakite {
if let Some(unakite) = base_config.unakite {
self.configure_unakite(TaskOperation::Up, unakite).await?;
}
@ -116,7 +119,7 @@ impl TaskExecutor {
if let Some(cfg) = self.config.as_ref() {
script.set_global_var("TRM_CONFIG", cfg.to_owned())
} else {
script.set_global_var("TRM_CONFIG", Config::empty())
script.set_global_var("TRM_CONFIG", OSConfig::empty())
}
.set_global_var("TRM_VERSION", env!("CARGO_PKG_VERSION"))
.execute(args)

@ -1,7 +1,11 @@
use args::{Args, Command, GenerateScriptsArgs, InstallFromConfigArgs};
use args::{Args, Command, GenerateEmptyConfigArgs, GenerateScriptsArgs, InstallFromConfigArgs};
use clap::Parser;
use tokio::{fs::OpenOptions, io::AsyncReadExt};
use tourmaline::{config::Config, error::AppResult, generate_script_files, TaskExecutor};
use rusty_value::into_json::{EnumRepr, IntoJson, IntoJsonOptions};
use tokio::{
fs::{self, OpenOptions},
io::AsyncReadExt,
};
use tourmaline::{distro::OSConfig, error::AppResult, generate_script_files, TaskExecutor};
mod args;
@ -14,6 +18,7 @@ async fn main() {
match args.command {
Command::InstallFromConfig(args) => install_from_config(args).await,
Command::GenerateScripts(args) => generate_scripts(args).await,
Command::CreateEmptyConfig(args) => generate_empty_config(args).await,
}
.unwrap();
}
@ -22,7 +27,7 @@ async fn install_from_config(args: InstallFromConfigArgs) -> AppResult<()> {
let mut file = OpenOptions::new().read(true).open(args.path).await?;
let mut cfg_contents = String::new();
file.read_to_string(&mut cfg_contents).await?;
let config: Config = serde_json::from_str(&cfg_contents)?;
let config: OSConfig = serde_json::from_str(&cfg_contents)?;
TaskExecutor::with_config(config)
.install_from_config()
@ -32,3 +37,13 @@ async fn install_from_config(args: InstallFromConfigArgs) -> AppResult<()> {
async fn generate_scripts(args: GenerateScriptsArgs) -> AppResult<()> {
generate_script_files(args.path).await
}
async fn generate_empty_config(args: GenerateEmptyConfigArgs) -> AppResult<()> {
let config = OSConfig::empty().into_json_with_options(&IntoJsonOptions {
enum_repr: EnumRepr::Untagged,
});
let config_string = serde_json::to_string_pretty(&config)?;
fs::write(args.path, config_string).await?;
Ok(())
}

@ -13,7 +13,7 @@ impl ScriptLoader {
/// Creates a new script loader with the default config dir
pub fn new() -> Self {
Self {
script_dir: crate::utils::SCRIPT_PATH.to_owned(),
script_dir: crate::utils::CFG_PATH.to_owned(),
}
}

@ -65,12 +65,13 @@ impl<S: Script> NuScript<S> {
#[tracing::instrument(level = "trace", skip(self))]
pub async fn execute(&self, args: S::Args) -> AppResult<()> {
let mut ctx = ContextBuilder::default()
.with_command_groups(CommandGroupConfig::default().all_groups(true))
.with_command_groups(CommandGroupConfig::default().all_groups(true))?
.add_script(self.read_file().await?)?
.build()?;
if ctx.has_fn("main") {
ctx.call_fn("main", args.get_args())?;
let pipeline = ctx.call_fn("main", args.get_args())?;
ctx.print_pipeline(pipeline)?;
Ok(())
} else {
Err(AppError::MissingMain(self.path.clone()))

@ -70,7 +70,7 @@ macro_rules! task {
#[inline]
fn name() -> &'static str {
"up.nu"
concat!($name, "/up.nu")
}
}
@ -81,7 +81,7 @@ macro_rules! task {
#[inline]
fn name() -> &'static str {
"down.nu"
concat!($name, "/down.nu")
}
}
}

@ -10,7 +10,6 @@ const DEFAULT_CONFIG_DIR: &str = "/etc";
lazy_static::lazy_static! {
pub static ref CFG_PATH: PathBuf = env::var("TRM_CFG_PATH").map(PathBuf::from).unwrap_or_else(|_| PathBuf::from(DEFAULT_CONFIG_DIR).join("tourmaline"));
pub static ref SCRIPT_PATH: PathBuf = CFG_PATH.join("scripts");
}
pub async fn generate_script_files<P: AsRef<Path>>(output: P) -> AppResult<()> {

Loading…
Cancel
Save