From 6959a9396740dc5a2dbc25caa6d66568d2c00ea3 Mon Sep 17 00:00:00 2001 From: amy Date: Sun, 30 Jan 2022 15:03:51 +0100 Subject: [PATCH] first part of config parsing added ( #7 ) --- Cargo.lock | 80 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 +- example_config.json | 43 ++++++++++++++++++++++ src/internal/config.rs | 83 ++++++++++++++++++++++++++++++++++++++++++ src/internal/mod.rs | 1 + src/main.rs | 11 ++++++ 6 files changed, 221 insertions(+), 1 deletion(-) create mode 100755 example_config.json create mode 100755 src/internal/config.rs diff --git a/Cargo.lock b/Cargo.lock index c59f500..afc0079 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,11 +52,19 @@ dependencies = [ "libc", ] +[[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + [[package]] name = "jade" version = "0.1.0" dependencies = [ "clap", + "serde", + "serde_json", ] [[package]] @@ -65,12 +73,78 @@ version = "0.2.112" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +[[package]] +name = "proc-macro2" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + +[[package]] +name = "serde" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.136" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.78" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d23c1ba4cf0efd44be32017709280b32d1cea5c3f1275c3b6d9e8bc54f758085" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "strsim" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -86,6 +160,12 @@ version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" +[[package]] +name = "unicode-xid" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" + [[package]] name = "vec_map" version = "0.8.2" diff --git a/Cargo.toml b/Cargo.toml index d08db40..5b06c68 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = "2.34.0" \ No newline at end of file +clap = "2.34.0" +serde_json = "1.0.59" +serde = { version = "1.0.117", features = [ "derive" ] } \ No newline at end of file diff --git a/example_config.json b/example_config.json new file mode 100755 index 0000000..e8e5cf2 --- /dev/null +++ b/example_config.json @@ -0,0 +1,43 @@ +{ + "partition": { + "device": "sda", + "mode": "auto", + "efi": true + }, + "bootloader": { + "type": "grub-efi", + "location": "/boot/efi" + }, + "locale": { + "locale": [ + "en_US.UTF-8 UTF-8" + ], + "keymap": "colemak", + "timezone": "Europe/Berlin" + }, + "networking": { + "hostname": "jade-test", + "ipv6": false + }, + "users": [ + { + "name": "jade", + "password": "jade", + "hasroot": true + }, + { + "name": "jade2", + "password": "jade2", + "hasroot": false + } + ], + "rootpass": "jaderoot", + "desktop": "onyx", + "timeshift": true, + "extra_packages": [ + "firefox", + "vim", + "git", + "tmux" + ] +} \ No newline at end of file diff --git a/src/internal/config.rs b/src/internal/config.rs new file mode 100755 index 0000000..a84eea4 --- /dev/null +++ b/src/internal/config.rs @@ -0,0 +1,83 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +struct Config { + partition: Partition, + bootloader: Bootloader, + locale: Locale, + networking: Networking, + users: Vec, + rootpass: String, + desktop: String, + timeshift: bool, + extra_packages: Vec, +} + +#[derive(Serialize, Deserialize)] +struct Partition { + device: String, + mode: String, + efi: bool, +} + +#[derive(Serialize, Deserialize)] +struct Bootloader { + r#type: String, + location: String, +} + +#[derive(Serialize, Deserialize)] +struct Locale { + locale: Vec, + keymap: String, + timezone: String, +} + +#[derive(Serialize, Deserialize)] +struct Networking { + hostname: String, + ipv6: bool, +} + +#[derive(Serialize, Deserialize)] +struct Users { + name: String, + password: String, + hasroot: bool, +} + +pub fn read_config() { + let data = + std::fs::read_to_string("example_config.json").expect("Unable to read example_config.json"); + let config: Config = serde_json::from_str(&data).expect("Unable to parse example_config.json"); + println!("---------Partition---------"); + println!("{}", config.partition.device); + println!("{}", config.partition.mode); + println!("{}", config.partition.efi); + println!("---------Bootloader---------"); + println!("{}", config.bootloader.r#type); + println!("{}", config.bootloader.location); + println!("---------Locale---------"); + println!("{:?}", config.locale.locale); + println!("{}", config.locale.keymap); + println!("{}", config.locale.timezone); + println!("---------Networking---------"); + println!("{}", config.networking.hostname); + println!("{}", config.networking.ipv6); + println!("---------Users---------"); + println!("---------"); + for i in 0..config.users.len() { + println!("{}", config.users[i].name); + println!("{}", config.users[i].password); + println!("{}", config.users[i].hasroot); + println!("---------"); + } + println!("---------Rootpass---------"); + println!("{}", config.rootpass); + println!("---------Desktop---------"); + println!("{}", config.desktop); + println!("---------Timeshift---------"); + println!("{}", config.timeshift); + println!("---------Extra packages---------"); + println!("{:?}", config.extra_packages); +} diff --git a/src/internal/mod.rs b/src/internal/mod.rs index b769472..b527384 100755 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,3 +1,4 @@ +pub mod config; pub mod exec; pub mod files; pub mod install; diff --git a/src/main.rs b/src/main.rs index c117ed6..123c545 100755 --- a/src/main.rs +++ b/src/main.rs @@ -139,6 +139,15 @@ fn main() { ), ) ) + .subcommand( + SubCommand::with_name("config") + .about("read a jade installation config") + .arg( + Arg::with_name("config") + .help("The config to read") + .required(true), + ), + ) .subcommand( SubCommand::with_name("desktops") .about("Graphical stuff (Desktop environment and Display Manager)") @@ -198,6 +207,8 @@ fn main() { base::genfstab(); } else if app.subcommand_matches("setup-timeshift").is_some() { base::setup_timeshift(); + } else if app.subcommand_matches("config").is_some() { + crate::internal::config::read_config(); } else { println!("Running TUI installer"); }