From b2001ed62d6a4abd7193daeb5d818395b46433b6 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sun, 9 Aug 2026 14:07:28 +0800 Subject: feat: add config command and resource for editing settings Add a new `cfg` command to view and edit configuration items stored as key-value pairs in a JSON file. --- mingling_cli/Cargo.lock | 4 +- mingling_cli/help/help.txt | 7 ++- mingling_cli/src/bin/cli.rs | 5 +- mingling_cli/src/config.rs | 110 ++++++++++++++++++++++++++++++++++++++++++++ mingling_cli/src/lib.rs | 1 + 5 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 mingling_cli/src/config.rs diff --git a/mingling_cli/Cargo.lock b/mingling_cli/Cargo.lock index e5993b4..85f41bc 100644 --- a/mingling_cli/Cargo.lock +++ b/mingling_cli/Cargo.lock @@ -1106,9 +1106,9 @@ dependencies = [ [[package]] name = "toml_parser" -version = "1.1.2+spec-1.1.0" +version = "1.1.3+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526" +checksum = "1d38ac1cf9b95face32296c0a3ede1fdc270627c9d9c02a7274dd6d960dc4d56" dependencies = [ "winnow 1.0.4", ] diff --git a/mingling_cli/help/help.txt b/mingling_cli/help/help.txt index 9e35194..91a7a7e 100644 --- a/mingling_cli/help/help.txt +++ b/mingling_cli/help/help.txt @@ -12,7 +12,7 @@ __ --no-deps *Do not include dependencies in metadata* **COMMANDS:** __ **METADATA:** __ metadata *Check your workspace metadata using 'cargo metadata'* -__ + __ **LINTER:** __ lint *Mingling Linter* __ ra-lint *Run* `mling lint` *and output the results* @@ -30,3 +30,8 @@ __ uninstall [PACKAGE] *Uninstall the project from the Mingling pac __ pkg-show *Show locally installed packages* __ pkg-enable *Enable the specified package* __ pkg-disable *Disable the specified package* + +__ **CONFIG:** +__ cfg *Edit the value of a configuration item* +__ cfg *Print the value of the configuration item; print nothing if it does not exist* +__ cfg *Print all configuration items, can be used with grep for searching* diff --git a/mingling_cli/src/bin/cli.rs b/mingling_cli/src/bin/cli.rs index 2e6c972..f641749 100644 --- a/mingling_cli/src/bin/cli.rs +++ b/mingling_cli/src/bin/cli.rs @@ -1,7 +1,7 @@ use mingling::setup::{DirectoryEnvironmentSetup, ExitCodeSetup, picker::HelpFlagSetup}; use mingling_cli::{ - ThisProgram, linter::registry::LintRegistrySetup, metadata::MinglingMetadataSetup, - pkg_mgr::PackageManagerSetup, + ThisProgram, config::MlingConfigSetup, linter::registry::LintRegistrySetup, + metadata::MinglingMetadataSetup, pkg_mgr::PackageManagerSetup, }; #[tokio::main] @@ -14,6 +14,7 @@ async fn main() { program.with_setup(DirectoryEnvironmentSetup::default()); program.with_setup(MinglingMetadataSetup); + program.with_setup(MlingConfigSetup); program.with_setup(LintRegistrySetup); program.with_setup(PackageManagerSetup); diff --git a/mingling_cli/src/config.rs b/mingling_cli/src/config.rs new file mode 100644 index 0000000..834b98c --- /dev/null +++ b/mingling_cli/src/config.rs @@ -0,0 +1,110 @@ +use std::collections::HashMap; + +use mingling::{LazyInit, Program, macros::program_setup}; + +use crate::ThisProgram; + +#[derive(Debug, Default, Clone)] +pub struct ResMlingConfig { + kvp: HashMap, +} + +impl ResMlingConfig { + /// Edit a KVP entry. Empty string removes the key. + pub fn edit(&mut self, key: &str, value: &str) { + if value.is_empty() { + self.kvp.remove(key); + } else { + self.kvp.insert(key.to_string(), value.to_string()); + } + } + + /// Get a value by key, returning "" if it doesn't exist. + pub fn get(&self, key: &str) -> &str { + self.kvp.get(key).map(|s| s.as_str()).unwrap_or("") + } + + /// Get a value by key, returning `or` if it doesn't exist. + pub fn get_or<'a>(&'a self, key: &'a str, or: &'a str) -> &'a str { + if self.kvp.contains_key(key) { + self.kvp.get(key).map(|s| s.as_str()).unwrap_or(or) + } else { + or + } + } + + /// Get a value by key, returning `or` if it doesn't exist and setting it. + pub fn get_or_set<'a>(&'a mut self, key: &'a str, or: &'a str) -> &'a str { + if !self.kvp.contains_key(key) { + self.kvp.insert(key.to_string(), or.to_string()); + } + self.kvp.get(key).map(|s| s.as_str()).unwrap_or(or) + } + + /// Read the config from disk, defaulting to empty if not present. + pub fn read() -> Self { + let path = std::path::Path::new(""); + Self::read_from_path(path) + } + + /// Read the config from a file at the given path. + pub fn read_from_path(path: &std::path::Path) -> Self { + let mut config = Self::default(); + if let Ok(content) = std::fs::read_to_string(path) + && let Ok(json_value) = serde_json::from_str::(&content) + && let serde_json::Value::Object(map) = json_value + { + for (key, value) in map { + if let serde_json::Value::String(s) = value { + config.kvp.insert(key, s); + } + } + } + config + } + + /// Write the config to disk at the given path. + pub fn write_to_path(&self, path: &std::path::Path) { + if let Some(parent) = path.parent() { + let _ = std::fs::create_dir_all(parent); + } + let json_value = serde_json::Value::Object( + self.kvp + .iter() + .map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone()))) + .collect(), + ); + if let Ok(json_string) = serde_json::to_string(&json_value) { + let _ = std::fs::write(path, json_string); + } + } + + /// Write the config to the default data directory path. + pub fn write(&self) { + if let Some(data_dir) = dirs::data_dir() { + let path = data_dir.join("mingling").join("mling-cfg.json"); + self.write_to_path(&path); + } + } +} + +impl From> for ResMlingConfig { + fn from(kvp: HashMap) -> Self { + Self { kvp } + } +} + +impl From for HashMap { + fn from(config: ResMlingConfig) -> Self { + config.kvp + } +} + +#[program_setup] +pub fn mling_config_setup(p: &mut Program) { + p.with_resource( + ResMlingConfig::lazy_init(ResMlingConfig::read).with_on_drop(|config: ResMlingConfig| { + config.write(); + }), + ); +} diff --git a/mingling_cli/src/lib.rs b/mingling_cli/src/lib.rs index 524b74b..fdd7636 100644 --- a/mingling_cli/src/lib.rs +++ b/mingling_cli/src/lib.rs @@ -12,6 +12,7 @@ use crate::{ utils::display::ColorCode, }; +pub mod config; pub mod diagnostic; pub mod errors; pub mod linter; -- cgit