From 3aa4d9da67737f550dcc07f309b1dee5130d9bad Mon Sep 17 00:00:00 2001 From: Fries Date: Thu, 8 Sep 2022 08:12:02 -0700 Subject: [PATCH] config: use config crate Parsing of the config is now done with the config crate --- Cargo.lock | 245 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 + src/internal/commands.rs | 7 +- src/internal/config.rs | 81 +++++++++---- src/internal/detect.rs | 4 +- src/internal/mod.rs | 2 +- src/operations/clean.rs | 1 - 7 files changed, 312 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7c62e08..b248476 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,7 @@ dependencies = [ "clap_complete_fig", "color-eyre", "colored", + "config", "console", "crossterm 0.25.0", "dialoguer", @@ -56,6 +57,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "aho-corasick" version = "0.7.19" @@ -123,6 +135,17 @@ dependencies = [ "syn", ] +[[package]] +name = "async-trait" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76464446b8bc32758d7e88ee1a804d9914cd9b1cb264c029899680b0be29826f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "atty" version = "0.2.14" @@ -179,6 +202,15 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "block-buffer" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +dependencies = [ + "generic-array", +] + [[package]] name = "bumpalo" version = "3.11.0" @@ -321,6 +353,25 @@ dependencies = [ "winapi", ] +[[package]] +name = "config" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f1667b8320afa80d69d8bbe40830df2c8a06003d86f73d8e003b2c48df416d" +dependencies = [ + "async-trait", + "json5", + "lazy_static", + "nom", + "pathdiff", + "ron", + "rust-ini", + "serde", + "serde_json", + "toml", + "yaml-rust", +] + [[package]] name = "console" version = "0.15.1" @@ -351,6 +402,15 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" +[[package]] +name = "cpufeatures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d997bd5e24a5928dd43e46dc529867e207907fe0b239c3477d924f7f2ca320" +dependencies = [ + "libc", +] + [[package]] name = "crossbeam-channel" version = "0.5.6" @@ -412,6 +472,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "dialoguer" version = "0.10.2" @@ -424,6 +494,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "digest" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "directories" version = "4.0.1" @@ -444,6 +524,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "dlv-list" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" + [[package]] name = "encode_unicode" version = "0.3.6" @@ -613,6 +699,16 @@ dependencies = [ "thread_local", ] +[[package]] +name = "generic-array" +version = "0.14.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bff49e947297f3312447abdca79f45f4738097cc82b06e72054d2223f601f1b9" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.2.7" @@ -654,6 +750,9 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] [[package]] name = "heck" @@ -824,6 +923,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "lazy-regex" version = "2.3.0" @@ -859,6 +969,12 @@ version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "lock_api" version = "0.4.8" @@ -905,6 +1021,12 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.5.4" @@ -981,6 +1103,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nom" +version = "7.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8903e5a29a317527874d0402f867152a3d21c908bb0b933e416c65e301d4c36" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "ntapi" version = "0.3.7" @@ -1085,6 +1217,16 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-multimap" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +dependencies = [ + "dlv-list", + "hashbrown", +] + [[package]] name = "os_str_bytes" version = "6.3.0" @@ -1157,12 +1299,62 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" +[[package]] +name = "pest" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b0560d531d1febc25a3c9398a62a71256c0178f2e3443baedd9ad4bb8c9deb4" +dependencies = [ + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "905708f7f674518498c1f8d644481440f476d39ca6ecae83319bba7c6c12da91" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5803d8284a629cc999094ecd630f55e91b561a1d1ba75e233b00ae13b91a69ad" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1538eb784f07615c6d9a8ab061089c6c54a344c5b4301db51990ca1c241e8c04" +dependencies = [ + "once_cell", + "pest", + "sha-1", +] + [[package]] name = "petgraph" version = "0.6.2" @@ -1325,6 +1517,27 @@ dependencies = [ "winreg", ] +[[package]] +name = "ron" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +dependencies = [ + "base64", + "bitflags", + "serde", +] + +[[package]] +name = "rust-ini" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + [[package]] name = "rustc-demangle" version = "0.1.21" @@ -1419,6 +1632,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha-1" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.4" @@ -1775,6 +1999,18 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + +[[package]] +name = "ucd-trie" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" + [[package]] name = "unicode-bidi" version = "0.3.8" @@ -2016,6 +2252,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "zeroize" version = "1.5.7" diff --git a/Cargo.toml b/Cargo.toml index a706b68..28bcc69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,10 @@ console = "0.15.1" tracing-error = "0.2.0" fuzzy-matcher = "0.3.7" +[dependencies.config] +version = "0.13.2" +features = ["toml"] + [dependencies.tokio] version = "1.21.0" features = ["rt", "rt-multi-thread", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] diff --git a/src/internal/commands.rs b/src/internal/commands.rs index f06c604..b326fae 100644 --- a/src/internal/commands.rs +++ b/src/internal/commands.rs @@ -4,8 +4,9 @@ use std::path::{Path, PathBuf}; use std::process::{ExitStatus, Stdio}; use tokio::process::{Child, Command}; +use crate::internal::config::Config; use crate::internal::error::{AppError, AppResult}; -use crate::internal::{config, is_tty}; +use crate::internal::is_tty; pub struct StringOutput { pub stdout: String, @@ -52,7 +53,7 @@ impl ShellCommand { pub fn sudo() -> Self { Self::new( - config::read() + Config::read() .bin .sudo .unwrap_or_else(|| "sudo".to_string()), @@ -152,7 +153,7 @@ impl ShellCommand { }; let mut command = if self.elevated { let mut cmd = Command::new( - config::read() + Config::read() .bin .sudo .unwrap_or_else(|| "sudo".to_string()), diff --git a/src/internal/config.rs b/src/internal/config.rs index 7d4bd33..aa98fb3 100644 --- a/src/internal/config.rs +++ b/src/internal/config.rs @@ -1,58 +1,89 @@ #![allow(clippy::module_name_repetitions)] -use serde::Deserialize; -use std::{env, fs, path::PathBuf}; +use config::FileFormat; +use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use std::fs; -#[derive(Debug, Deserialize)] +use super::utils::get_config_dir; + +#[derive(Default, Debug, Deserialize, Serialize)] pub struct Config { pub base: ConfigBase, pub extra: ConfigExtra, pub bin: ConfigBin, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct ConfigBase { pub pacdiff_warn: bool, pub highlight_optdepends: bool, pub powerpill: bool, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct ConfigExtra { pub uwu: Option, pub uwu_debug: Option, pub review_user_shell: bool, } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Serialize)] pub struct ConfigBin { pub sudo: Option, } -impl Default for Config { +impl Default for ConfigBase { + fn default() -> Self { + Self { + pacdiff_warn: true, + highlight_optdepends: true, + powerpill: false, + } + } +} + +impl Default for ConfigExtra { + fn default() -> Self { + Self { + uwu: None, + uwu_debug: None, + review_user_shell: false, + } + } +} + +impl Default for ConfigBin { fn default() -> Self { Self { - base: ConfigBase { - pacdiff_warn: true, - highlight_optdepends: true, - powerpill: false, - }, - extra: ConfigExtra { - uwu: None, - uwu_debug: None, - review_user_shell: false, - }, - bin: ConfigBin { - sudo: Some("sudo".to_string()), - }, + sudo: Some("sudo".to_string()), } } } -pub fn read() -> Config { - let config_path = PathBuf::from(env::var("HOME").unwrap()).join(".config/ame/config.toml"); - match fs::read_to_string(config_path) { - Ok(contents) => toml::from_str(&contents).expect("Could not parse the config file"), - Err(_) => Config::default(), +impl Config { + pub fn read() -> Self { + let config_path = get_config_dir().join("config.toml"); + if config_path.exists() { + let builder = config::Config::builder() + .add_source(config::File::from_str( + &toml::ser::to_string(&Config::default()).unwrap(), + FileFormat::Toml, + )) + .add_source(config::File::with_name(config_path.to_str().unwrap())); + let conf = builder.build().unwrap(); + conf.try_deserialize().unwrap() + } else { + let default_conf = Config::default(); + let toml_string = toml::ser::to_string_pretty(&default_conf).unwrap(); + fs::write(config_path, toml_string).unwrap(); + default_conf + } + } + pub fn get() -> &'static Config { + lazy_static! { + static ref CONFIG: Config = Config::read(); + } + &*CONFIG } } diff --git a/src/internal/detect.rs b/src/internal/detect.rs index d2d295f..bbd81b1 100644 --- a/src/internal/detect.rs +++ b/src/internal/detect.rs @@ -1,7 +1,7 @@ use crossterm::style::Stylize; use crate::internal::commands::ShellCommand; -use crate::internal::config; +use crate::internal::config::Config; use crate::internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode; use crate::logging::get_logger; @@ -47,7 +47,7 @@ pub async fn detect() { let choice = prompt!(default no, "Would you like to run pacdiff now?"); if choice { - let config = config::read(); + let config = Config::get(); if config.base.pacdiff_warn { ShellCommand::pacdiff() .elevated() diff --git a/src/internal/mod.rs b/src/internal/mod.rs index f8bc079..dd195e0 100644 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -38,7 +38,7 @@ macro_rules! uwu { } pub fn uwu_enabled() -> bool { - let config = config::read(); + let config = config::Config::get(); config.extra.uwu.unwrap_or(false) } diff --git a/src/operations/clean.rs b/src/operations/clean.rs index fe865c1..65b5653 100644 --- a/src/operations/clean.rs +++ b/src/operations/clean.rs @@ -1,7 +1,6 @@ use crate::crash; use crate::internal::commands::ShellCommand; -use crate::internal::config; use crate::internal::error::SilentUnwrap; use crate::internal::exit_code::AppExitCode;