From d10efaff7bc259d964c2ba267b68af65f9ae52e2 Mon Sep 17 00:00:00 2001 From: Dmytro Stanchiev Date: Fri, 31 Oct 2025 11:39:21 -0400 Subject: [PATCH] fix config parsing Signed-off-by: Dmytro Stanchiev --- Cargo.lock | 327 ++++++---------------------------------------------- Cargo.toml | 4 +- src/main.rs | 73 ++++++------ 3 files changed, 74 insertions(+), 330 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96b4c2e..6ffe814 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,17 +2,6 @@ # It is not intended for manual editing. version = 4 -[[package]] -name = "ahash" -version = "0.7.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.4" @@ -22,17 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "async-trait" -version = "0.1.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "atty" version = "0.2.14" @@ -50,27 +28,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" -[[package]] -name = "base64" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" - [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "block-buffer" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" -dependencies = [ - "generic-array", -] - [[package]] name = "cfg-if" version = "1.0.4" @@ -102,58 +65,39 @@ dependencies = [ ] [[package]] -name = "config" -version = "0.13.4" +name = "const-random" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23738e11972c7643e4ec947840fc463b6a571afcd3e735bdfce7d03c7a784aca" +checksum = "87e00182fe74b066627d63b85fd550ac2998d4b0bd86bfed477a0ae4c7c71359" dependencies = [ - "async-trait", - "json5", - "lazy_static", - "nom", - "pathdiff", - "ron", - "rust-ini", - "serde", - "serde_json", - "toml", - "yaml-rust", + "const-random-macro", ] [[package]] -name = "cpufeatures" -version = "0.2.17" +name = "const-random-macro" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +checksum = "f9d839f2a20b0aee515dc581a6172f2321f96cab76c1a38a4c584a194955390e" dependencies = [ - "libc", + "getrandom", + "once_cell", + "tiny-keccak", ] [[package]] -name = "crypto-common" -version = "0.1.6" +name = "crunchy" +version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" -dependencies = [ - "generic-array", - "typenum", -] - -[[package]] -name = "digest" -version = "0.10.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" -dependencies = [ - "block-buffer", - "crypto-common", -] +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "dlv-list" -version = "0.3.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] [[package]] name = "env_logger" @@ -168,16 +112,6 @@ dependencies = [ "termcolor", ] -[[package]] -name = "generic-array" -version = "0.14.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bb6743198531e02858aeaea5398fcc883e71851fcbcb5a2f773e2fb6cb1edf2" -dependencies = [ - "typenum", - "version_check", -] - [[package]] name = "getrandom" version = "0.2.16" @@ -194,9 +128,12 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "hermit-abi" @@ -220,44 +157,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", ] -[[package]] -name = "itoa" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" - -[[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_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "log" version = "0.4.28" @@ -270,34 +178,18 @@ version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" -[[package]] -name = "minimal-lexical" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" - [[package]] name = "moshy" -version = "0.1.0" +version = "0.1.1" dependencies = [ "clap", - "config", "env_logger", "log", "rand", + "rust-ini", "shell-words", ] -[[package]] -name = "nom" -version = "7.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" -dependencies = [ - "memchr", - "minimal-lexical", -] - [[package]] name = "once_cell" version = "1.21.3" @@ -306,12 +198,12 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "ordered-multimap" -version = "0.4.3" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" dependencies = [ "dlv-list", - "hashbrown", + "hashbrown 0.14.5", ] [[package]] @@ -320,55 +212,6 @@ version = "6.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" -[[package]] -name = "pathdiff" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" - -[[package]] -name = "pest" -version = "2.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "989e7521a040efde50c3ab6bbadafbe15ab6dc042686926be59ac35d74607df4" -dependencies = [ - "memchr", - "ucd-trie", -] - -[[package]] -name = "pest_derive" -version = "2.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "187da9a3030dbafabbbfb20cb323b976dc7b7ce91fcd84f2f74d6e31d378e2de" -dependencies = [ - "pest", - "pest_generator", -] - -[[package]] -name = "pest_generator" -version = "2.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49b401d98f5757ebe97a26085998d6c0eecec4995cad6ab7fc30ffdf4b052843" -dependencies = [ - "pest", - "pest_meta", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "pest_meta" -version = "2.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72f27a2cfee9f9039c4d86faa5af122a0ac3851441a34865b8a043b46be0065a" -dependencies = [ - "pest", - "sha2", -] - [[package]] name = "ppv-lite86" version = "0.2.21" @@ -455,87 +298,16 @@ version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" -[[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" +version = "0.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +checksum = "796e8d2b6696392a43bea58116b667fb4c29727dc5abd27d6acf338bb4f688c7" dependencies = [ "cfg-if", "ordered-multimap", ] -[[package]] -name = "ryu" -version = "1.0.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" - -[[package]] -name = "serde" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" -dependencies = [ - "serde_core", - "serde_derive", -] - -[[package]] -name = "serde_core" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.228" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.145" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", - "serde_core", -] - -[[package]] -name = "sha2" -version = "0.10.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" -dependencies = [ - "cfg-if", - "cpufeatures", - "digest", -] - [[package]] name = "shell-words" version = "1.1.0" @@ -575,38 +347,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" [[package]] -name = "toml" -version = "0.5.11" +name = "tiny-keccak" +version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237" dependencies = [ - "serde", + "crunchy", ] -[[package]] -name = "typenum" -version = "1.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb" - -[[package]] -name = "ucd-trie" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" - [[package]] name = "unicode-ident" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" -[[package]] -name = "version_check" -version = "0.9.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" - [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" @@ -659,15 +413,6 @@ dependencies = [ "windows-link", ] -[[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 = "zerocopy" version = "0.8.27" diff --git a/Cargo.toml b/Cargo.toml index b728724..35a5ddd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,12 +1,12 @@ [package] name = "moshy" -version = "0.1.0" +version = "0.1.1" edition = "2024" [dependencies] clap = "3.0" -config = "0.13" env_logger = "0.9" log = "0.4" rand = "0.8" +rust-ini = "0.21.3" shell-words = "1.1" diff --git a/src/main.rs b/src/main.rs index 3800a56..4cbcaf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,9 @@ -use std::collections::HashMap; use std::env; use std::path::Path; use std::process::{self, Command}; -use std::str; -use clap::{App, Arg, ArgMatches}; -use config::{Config, File, FileFormat}; +use clap::{App, Arg}; +use ini::Ini; use log::{LevelFilter, debug, info}; use rand::Rng; use shell_words; @@ -68,37 +66,32 @@ fn main() { ), ]; - // Read config - let mut config_builder = Config::builder(); + // Read config using rust-ini + let mut conf = Ini::new(); for path in &config_paths { if Path::new(path).exists() { - config_builder = - config_builder.add_source(File::with_name(path).format(FileFormat::Ini)); + if let Ok(loaded) = Ini::load_from_file(path) { + // Merge sections and properties + for (sec, prop) in loaded.iter() { + for (k, v) in prop.iter() { + conf.with_section(sec.clone()).set(k, v); + } + } + } } } - let config = config_builder.build().unwrap_or_else(|e| { - eprintln!("Error reading config: {}", e); - process::exit(1); - }); if let Some(list_matches) = matches.subcommand_matches("list") { - let sections: Vec = config - .get_table("") - .unwrap_or_default() - .keys() - .cloned() + let sections: Vec = conf + .sections() + .filter_map(|s| s.map(|x| x.to_string())) .collect(); - if list_matches.is_present("verbose") { - // In verbose mode, perhaps print more details, but original only lists sections - println!("{}", sections.join("\n")); - } else { - println!("{}", sections.join("\n")); - } + println!("{}", sections.join("\n")); process::exit(0); } let hostname_arg = matches.value_of("hostname").unwrap_or(""); - let mut host_setup = parse_hostname(hostname_arg, &config); + let mut host_setup = parse_hostname(hostname_arg, &conf); debug!("Required host setup:\n{:?}", host_setup); let mut cmd = vec!["/usr/bin/env".to_string(), "mosh".to_string()]; @@ -163,7 +156,7 @@ struct HostSetup { cmd: Option>, } -fn parse_hostname(hostname_argument: &str, config: &Config) -> HostSetup { +fn parse_hostname(hostname_argument: &str, conf: &Ini) -> HostSetup { let mut host_setup = HostSetup { host: "".to_string(), ports: None, @@ -173,41 +166,47 @@ fn parse_hostname(hostname_argument: &str, config: &Config) -> HostSetup { if !hostname_argument.is_empty() { debug!("Parsing hostname argument '{}'", hostname_argument); let connection: Vec<&str> = hostname_argument.split('@').collect(); - let hostname = if connection.len() > 1 { + let mut hostname = if connection.len() > 1 { connection[1] } else { connection[0] }; + let sections: Vec = conf + .sections() + .filter_map(|s| s.map(|x| x.to_string())) + .collect(); + debug!("Config sections: {:?}", sections); + let mut profiles = Vec::new(); - if config.get_table(hostname).is_ok() { + if sections.contains(&hostname.to_string()) { profiles.push(hostname.to_string()); } let flavor: Vec<&str> = hostname.split(':').collect(); if flavor.len() > 1 { - let hostname_no_flavor = flavor[0]; - if config.get_table(hostname_no_flavor).is_ok() { - profiles.push(hostname_no_flavor.to_string()); + hostname = flavor[0]; + if sections.contains(&hostname.to_string()) { + profiles.push(hostname.to_string()); } } - debug!("Found hostname profile(s) {}", hostname); + debug!("Found hostname profile(s) {}", hostname_argument); debug!("Profile(s) to look for values :\n{:?}", profiles); host_setup.host = hostname.to_string(); profiles.reverse(); for profile in profiles { debug!("Get values from {}", profile); - if let Ok(table) = config.get_table(&profile) { - debug!("profile config:\n{:?}", table); - if let Some(ports) = table.get("port").and_then(|v| v.clone().into_string().ok()) { - host_setup.ports = Some(ports); + if let Some(section) = conf.section(Some(&profile)) { + debug!("profile config:\n{:?}", section); + if let Some(ports) = section.get("port") { + host_setup.ports = Some(ports.to_string()); } debug!("ports:{:?}", host_setup.ports); - if let Some(cmd_str) = table.get("cmd").and_then(|v| v.clone().into_string().ok()) { - if let Ok(cmd_vec) = shell_words::split(&cmd_str) { + if let Some(cmd_str) = section.get("cmd") { + if let Ok(cmd_vec) = shell_words::split(cmd_str) { host_setup.cmd = Some(cmd_vec); } }