replace deps `edit` and `rpassword` with `dialoguer`

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-07 16:43:26 +02:00
parent 404e5371e3
commit f75d2fee7a
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 77 additions and 83 deletions

86
Cargo.lock generated
View File

@ -269,6 +269,19 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "console"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
dependencies = [
"encode_unicode 0.3.6",
"lazy_static",
"libc",
"unicode-width",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "const-oid" name = "const-oid"
version = "0.9.6" version = "0.9.6"
@ -381,6 +394,19 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "dialoguer"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658bce805d770f407bc62102fca7c2c64ceef2fbcb2b8bd19d2765ce093980de"
dependencies = [
"console",
"shell-words",
"tempfile",
"thiserror",
"zeroize",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.10.7" version = "0.10.7"
@ -420,16 +446,6 @@ version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b"
[[package]]
name = "edit"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f364860e764787163c8c8f58231003839be31276e821e2ad2092ddf496b1aa09"
dependencies = [
"tempfile",
"which",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.11.0" version = "1.11.0"
@ -439,6 +455,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]] [[package]]
name = "encode_unicode" name = "encode_unicode"
version = "1.0.0" version = "1.0.0"
@ -885,7 +907,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"edit", "dialoguer",
"env_logger", "env_logger",
"indoc", "indoc",
"itertools", "itertools",
@ -893,7 +915,6 @@ dependencies = [
"nix", "nix",
"prettytable", "prettytable",
"ratatui", "ratatui",
"rpassword",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
@ -1091,7 +1112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46480520d1b77c9a3482d39939fcf96831537a250ec62d4fd8fbdf8e0302e781" checksum = "46480520d1b77c9a3482d39939fcf96831537a250ec62d4fd8fbdf8e0302e781"
dependencies = [ dependencies = [
"csv", "csv",
"encode_unicode", "encode_unicode 1.0.0",
"is-terminal", "is-terminal",
"lazy_static", "lazy_static",
"term", "term",
@ -1230,17 +1251,6 @@ dependencies = [
"windows-sys 0.52.0", "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]] [[package]]
name = "rsa" name = "rsa"
version = "0.9.6" version = "0.9.6"
@ -1261,16 +1271,6 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[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]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"
@ -1410,6 +1410,12 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]] [[package]]
name = "signal-hook" name = "signal-hook"
version = "0.3.17" version = "0.3.17"
@ -2057,18 +2063,6 @@ version = "0.25.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1"
[[package]]
name = "which"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7"
dependencies = [
"either",
"home",
"once_cell",
"rustix",
]
[[package]] [[package]]
name = "whoami" name = "whoami"
version = "1.5.1" version = "1.5.1"

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.82" anyhow = "1.0.82"
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
edit = "0.1.5" dialoguer = "0.11.0"
env_logger = "0.11.3" env_logger = "0.11.3"
indoc = "2.0.5" indoc = "2.0.5"
itertools = "0.12.1" itertools = "0.12.1"
@ -14,7 +14,6 @@ log = "0.4.21"
nix = { version = "0.28.0", features = ["user"] } nix = { version = "0.28.0", features = ["user"] }
prettytable = "0.10.0" prettytable = "0.10.0"
ratatui = { version = "0.26.2", optional = true } ratatui = { version = "0.26.2", optional = true }
rpassword = "7.3.1"
serde = "1.0.198" serde = "1.0.198"
serde_json = "1.0.116" serde_json = "1.0.116"
sqlx = { version = "0.7.4", features = ["runtime-tokio", "mysql", "tls-rustls"] } sqlx = { version = "0.7.4", features = ["runtime-tokio", "mysql", "tls-rustls"] }

View File

@ -1,5 +1,6 @@
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use clap::Parser; use clap::Parser;
use dialoguer::Editor;
use indoc::indoc; use indoc::indoc;
use itertools::Itertools; use itertools::Itertools;
use prettytable::{Cell, Row, Table}; use prettytable::{Cell, Row, Table};
@ -494,27 +495,32 @@ pub async fn edit_permissions(
longest_database_name, longest_database_name,
); );
let result = edit::edit_with_builder( // TODO: handle errors better here
format!( let result = Editor::new()
"{}\n{}\n{}", .extension("tsv")
comment, .edit(
header.join(" "), format!(
if permission_data.is_empty() { "{}\n{}\n{}",
format!("# {}", example_line) comment,
} else { header.join(" "),
permission_data if permission_data.is_empty() {
.iter() format!("# {}", example_line)
.map(|perm| { } else {
format_privileges_line(perm, longest_username, longest_database_name) permission_data
}) .iter()
.join("\n") .map(|perm| {
} format_privileges_line(
), perm,
edit::Builder::new() longest_username,
.prefix("database-permissions") longest_database_name,
.suffix(".tsv") )
.rand_bytes(10), })
)?; .join("\n")
}
)
.as_str(),
)?
.unwrap();
parse_permission_data_from_editor(result) parse_permission_data_from_editor(result)
.context("Could not parse permission data from editor")? .context("Could not parse permission data from editor")?

View File

@ -2,6 +2,7 @@ use std::vec;
use anyhow::Context; use anyhow::Context;
use clap::Parser; use clap::Parser;
use dialoguer::{Confirm, Password};
use sqlx::MySqlConnection; use sqlx::MySqlConnection;
use crate::core::{common::close_database_connection, user_operations::validate_user_name}; use crate::core::{common::close_database_connection, user_operations::validate_user_name};
@ -101,20 +102,14 @@ async fn drop_users(args: UserDeleteArgs, conn: &mut MySqlConnection) -> anyhow:
} }
pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Result<String> { pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Result<String> {
let pass1 = rpassword::prompt_password(format!("New MySQL password for user '{}': ", username)) Password::new()
.context("Failed to read password")?; .with_prompt(format!("New MySQL password for user '{}'", username))
.with_confirmation(
let pass2 = rpassword::prompt_password(format!( format!("Retype new MySQL password for user '{}'", username),
"Retype new MySQL password for user '{}': ", "Passwords do not match",
username )
)) .interact()
.context("Failed to read password")?; .map_err(Into::into)
if pass1 != pass2 {
anyhow::bail!("Passwords do not match");
}
Ok(pass1)
} }
async fn change_password_for_user( async fn change_password_for_user(