From 3378aff826d35f1dde25357a8b308dabc01d480d Mon Sep 17 00:00:00 2001 From: cncsmonster Date: Mon, 4 Nov 2024 21:43:49 +0800 Subject: [PATCH] support ~ to in path, use ~/.local/share/fgm/go as default gate_path --- Cargo.lock | 155 +++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 3 +- src/config.rs | 39 +++++++++++-- src/lib.rs | 4 +- src/main.rs | 2 +- 5 files changed, 180 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 717fda6..b26ad7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,7 +109,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -271,6 +271,27 @@ dependencies = [ "syn", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + [[package]] name = "dtoa" version = "1.0.9" @@ -331,11 +352,12 @@ checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" [[package]] name = "fgm" -version = "0.2.3" +version = "0.2.4" dependencies = [ "anyhow", "clap", "clap_complete", + "dirs", "flate2", "itertools", "reqwest", @@ -886,6 +908,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "parking_lot" version = "0.12.3" @@ -906,7 +934,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1115,6 +1143,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "reqwest" version = "0.12.7" @@ -1537,6 +1576,26 @@ dependencies = [ "utf-8", ] +[[package]] +name = "thiserror" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tinyvec" version = "1.8.0" @@ -1855,7 +1914,7 @@ checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" dependencies = [ "windows-result", "windows-strings", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1864,7 +1923,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1874,7 +1933,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" dependencies = [ "windows-result", - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", ] [[package]] @@ -1883,7 +1951,7 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1892,7 +1960,22 @@ version = "0.59.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" dependencies = [ - "windows-targets", + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", ] [[package]] @@ -1901,28 +1984,46 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + [[package]] name = "windows_i686_gnu" version = "0.52.6" @@ -1935,24 +2036,48 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + [[package]] name = "windows_x86_64_msvc" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index 9560514..7ca324f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "fgm" authors = ["cncsmonster "] -version = "0.2.3" +version = "0.2.4" edition = "2021" license = "MIT OR Apache-2.0" description = "Fast and simple go version manager" @@ -29,6 +29,7 @@ toml = "0.8.19" anyhow.workspace = true clap = { workspace = true, features = ["derive"] } clap_complete.workspace = true +dirs = "5.0.1" flate2.workspace = true itertools.workspace = true reqwest = { workspace = true, features = ["blocking"] } diff --git a/src/config.rs b/src/config.rs index 4d55e4c..2cb8742 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,5 @@ -use std::path::Path; +use core::panic; +use std::{path::Path, sync::OnceLock}; use anyhow::Result; @@ -37,13 +38,30 @@ impl FgmContext { self.remote_source = v.to_owned(); } } + + pub fn legalize_home_dir(&mut self) { + legalize(&mut self.installations_dir); + legalize(&mut self.gate_path); + legalize(&mut self.remote_source); + } +} + +fn legalize(path: &mut String) { + static HOME_DATA: OnceLock = OnceLock::new(); + if path.contains("~") { + let home_dir = HOME_DATA.get_or_init(|| match dirs::home_dir() { + Some(d) => d.display().to_string(), + None => panic!("fail to get home dir"), + }); + *path = path.replace("~", home_dir); + } } impl Default for FgmContext { fn default() -> Self { FgmContext { installations_dir: "/usr/local/share/fgm".to_string(), - gate_path: "/usr/local/go".to_string(), + gate_path: "~/.local/share/fgm/go".to_string(), remote_source: "https://go.dev/dl/".to_string(), update: false, } @@ -54,10 +72,10 @@ pub const XDG_CONFIG_HOME: &str = "XDG_CONFIG_HOME"; pub const XDG_DATA_HOME: &str = "XDG_DATA_HOME"; pub fn count_config_path() -> Result { - let cf_dir = std::env::var(XDG_CONFIG_HOME).map(|v| format!("{}/fgm", v)); + let cf_dir = std::env::var(XDG_CONFIG_HOME).map(|v| format!("{v}/fgm/config.toml")); let cf_dir = match cf_dir { Ok(v) => v, - Err(_) => std::env::var("HOME").map(|v| format!("{}/.fgm", v))?, + Err(_) => std::env::var("HOME").map(|v| format!("{v}/.fgm/config.toml"))?, }; Ok(cf_dir) @@ -78,9 +96,22 @@ impl FgmConfig { let toml_str = std::fs::read_to_string(cf_path)?; Ok(toml::from_str(&toml_str)?) } + pub fn save>(&self, cf_path: P) -> Result<()> { let toml_str = toml::to_string(self)?; std::fs::write(cf_path, toml_str)?; Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_legalize() { + let mut m = "~/gg".to_string(); + legalize(&mut m); + dbg!(m); + } +} diff --git a/src/lib.rs b/src/lib.rs index 540fc8a..4767269 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,10 +3,10 @@ pub mod cli; pub mod config; pub mod mpsc; +use std::process::Command; use std::{ fs::{self, create_dir_all}, path::Path, - process::Command, }; use anyhow::{anyhow, Context, Result}; @@ -287,7 +287,7 @@ pub fn current_version(ctx: &FgmContext) -> Result { Ok(version.to_string()) } -// 生成设置环境变量的脚本 +// gen script for setting environment var pub fn init_script(config: &FgmContext) -> String { format!("export PATH={}/bin:$PATH", config.gate_path) } diff --git a/src/main.rs b/src/main.rs index 99222c3..d8dd966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,7 @@ fn init_context() -> FgmContext { }); let config = fgm::config::FgmConfig::load(&config_path).unwrap_or_default(); ctx.update_from_config(&config); - + ctx.legalize_home_dir(); ctx }