From a7146f6d33a38415fe8fff8ea1fc48c80d2c96c9 Mon Sep 17 00:00:00 2001 From: n4n5 Date: Wed, 8 Jan 2025 16:57:41 +0100 Subject: [PATCH] use rpassword --- Cargo.lock | 124 +++++++++++++++++++++++++++++++++++------ Cargo.toml | 3 +- src/codeberg/config.rs | 4 +- src/github/config.rs | 4 +- src/gitlab/config.rs | 4 +- src/lib.rs | 2 +- src/macros.rs | 57 ++++++++++++++++--- src/utils.rs | 5 ++ 8 files changed, 170 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62f1cba..1d8c36c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,7 +99,7 @@ dependencies = [ "miniz_oxide", "object", "rustc-demangle", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -273,7 +273,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -370,7 +370,7 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "git-mover" -version = "1.0.2" +version = "1.0.3" dependencies = [ "clap", "dotenv", @@ -380,6 +380,7 @@ dependencies = [ "log", "rand", "reqwest", + "rpassword", "serde", "serde_json", "tokio", @@ -948,7 +949,7 @@ dependencies = [ "libc", "redox_syscall", "smallvec", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1129,6 +1130,27 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rpassword" +version = "7.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80472be3c897911d0137b2d2b9055faf6eeac5b14e324073d83bc17b191d7e3f" +dependencies = [ + "libc", + "rtoolbox", + "windows-sys 0.48.0", +] + +[[package]] +name = "rtoolbox" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c247d24e63230cdb56463ae328478bd5eac8b8faa8c69461a77e8e323afac90e" +dependencies = [ + "libc", + "windows-sys 0.48.0", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1145,7 +1167,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1411,7 +1433,7 @@ dependencies = [ "getrandom", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1719,7 +1741,7 @@ checksum = "e400001bb720a623c1c69032f8e3e4cf09984deec740f007dd2b03ec864804b0" dependencies = [ "windows-result", "windows-strings", - "windows-targets", + "windows-targets 0.52.6", ] [[package]] @@ -1728,7 +1750,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]] @@ -1738,7 +1760,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]] @@ -1747,7 +1778,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]] @@ -1756,7 +1787,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]] @@ -1765,28 +1811,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" @@ -1799,24 +1863,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 513eb33..7a76199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "git-mover" -version = "1.0.2" +version = "1.0.3" edition = "2021" keywords = ["utility", "git", "cli"] categories = ["command-line-utilities"] @@ -19,6 +19,7 @@ home = "0.5.11" log = "0.4.22" rand = "0.8.5" reqwest = { version = "0.12", features = ["json"] } +rpassword = "7.3.1" serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.135" tokio = { version = "1", features = ["full"] } diff --git a/src/codeberg/config.rs b/src/codeberg/config.rs index 011d8ec..8213252 100644 --- a/src/codeberg/config.rs +++ b/src/codeberg/config.rs @@ -1,6 +1,6 @@ //! Codeberg configuration use super::platform::CodebergPlatform; -use crate::{config::Config, config_value_wrap}; +use crate::{config::Config, config_password_wrap, config_value_wrap}; use serde::{Deserialize, Serialize}; /// Codeberg configuration @@ -23,7 +23,7 @@ impl CodebergConfig { username, "your codeberg username" ); - let token = config_value_wrap!( + let token = config_password_wrap!( config, codeberg, CodebergConfig, diff --git a/src/github/config.rs b/src/github/config.rs index 71e626e..3b22031 100644 --- a/src/github/config.rs +++ b/src/github/config.rs @@ -2,7 +2,7 @@ use super::platform::GithubPlatform; use serde::{Deserialize, Serialize}; -use crate::{config::Config, config_value_wrap}; +use crate::{config::Config, config_password_wrap, config_value_wrap}; /// Github configuration #[derive(Deserialize, Serialize, Default, Debug, Clone)] @@ -24,7 +24,7 @@ impl GithubConfig { username, "your github username" ); - let token = config_value_wrap!( + let token = config_password_wrap!( config, github, GithubConfig, diff --git a/src/gitlab/config.rs b/src/gitlab/config.rs index 86b1d8b..ae3ecd6 100644 --- a/src/gitlab/config.rs +++ b/src/gitlab/config.rs @@ -1,7 +1,7 @@ //! Gitlab configuration use super::platform::GitlabPlatform; -use crate::config::Config; use crate::config_value_wrap; +use crate::{config::Config, config_password_wrap}; use serde::{Deserialize, Serialize}; /// Gitlab configuration @@ -24,7 +24,7 @@ impl GitlabConfig { username, "your gitlab username" ); - let token = config_value_wrap!( + let token = config_password_wrap!( config, gitlab, GitlabConfig, diff --git a/src/lib.rs b/src/lib.rs index fd3e25c..a5f51dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ pub(crate) mod macros; pub(crate) mod platform; pub(crate) mod sync; pub(crate) mod utils; -pub(crate) use macros::config_value; +pub(crate) use macros::config_password_wrap; pub(crate) use macros::config_value_wrap; mod codeberg; diff --git a/src/macros.rs b/src/macros.rs index 24f4949..37567d8 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -2,7 +2,7 @@ /// automatically generate the input path macro_rules! config_value { - ($config:ident, $setting_name:ident, $struct_name:ident, $key_name:ident, $string:expr) => { + ($config:ident, $setting_name:ident, $struct_name:ident, $key_name:ident, $string:expr, $func:expr) => { match &$config.config_data.$setting_name { Some($struct_name { $key_name: Some(value), @@ -10,7 +10,7 @@ macro_rules! config_value { }) => value.clone(), _ => { println!(concat!("Please enter ", $string, ":")); - let value = $crate::utils::input(); + let value = $func(); let cloned_value = value.clone(); $config.update(|config_data| { if let Some(local_config) = config_data.$setting_name.as_mut() { @@ -28,21 +28,64 @@ macro_rules! config_value { } }; } +pub(crate) use config_value; /// automatically generate the input path by wrapping config_value macro macro_rules! config_value_wrap { ($config:ident, $setting_name:ident, $struct_name:ident, $key_name:ident, $string:expr) => { + $crate::macros::config_value_wrap_internal!( + $config, + $setting_name, + $struct_name, + $key_name, + $string, + $crate::utils::input + ) + }; +} +pub(crate) use config_value_wrap; + +/// automatically generate the input path by wrapping config_value macro +macro_rules! config_password_wrap { + ($config:ident, $setting_name:ident, $struct_name:ident, $key_name:ident, $string:expr) => { + $crate::macros::config_value_wrap_internal!( + $config, + $setting_name, + $struct_name, + $key_name, + $string, + $crate::utils::get_password + ) + }; +} +pub(crate) use config_password_wrap; + +/// automatically generate the input path by wrapping config_value macro +macro_rules! config_value_wrap_internal { + ($config:ident, $setting_name:ident, $struct_name:ident, $key_name:ident, $string:expr, $func:expr) => { match &$config.config_data.$setting_name { Some(c) => match &c.$key_name { Some(u) => u.clone(), None => { - $crate::config_value!($config, $setting_name, $struct_name, $key_name, $string) + $crate::macros::config_value!( + $config, + $setting_name, + $struct_name, + $key_name, + $string, + $func + ) } }, - None => $crate::config_value!($config, $setting_name, $struct_name, $key_name, $string), + None => $crate::macros::config_value!( + $config, + $setting_name, + $struct_name, + $key_name, + $string, + $func + ), } }; } - -pub(crate) use config_value; -pub(crate) use config_value_wrap; +pub(crate) use config_value_wrap_internal; diff --git a/src/utils.rs b/src/utils.rs index 44e0246..486b021 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -261,6 +261,11 @@ pub(crate) fn yes_no_input(msg: &str) -> bool { } } +/// Get password from the user +pub(crate) fn get_password() -> String { + rpassword::read_password().expect("Error reading password") +} + #[cfg(test)] mod test {