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",
]
[[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]]
name = "const-oid"
version = "0.9.6"
@ -381,6 +394,19 @@ dependencies = [
"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]]
name = "digest"
version = "0.10.7"
@ -420,16 +446,6 @@ version = "0.15.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "either"
version = "1.11.0"
@ -439,6 +455,12 @@ dependencies = [
"serde",
]
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "encode_unicode"
version = "1.0.0"
@ -885,7 +907,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"edit",
"dialoguer",
"env_logger",
"indoc",
"itertools",
@ -893,7 +915,6 @@ dependencies = [
"nix",
"prettytable",
"ratatui",
"rpassword",
"serde",
"serde_json",
"sqlx",
@ -1091,7 +1112,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46480520d1b77c9a3482d39939fcf96831537a250ec62d4fd8fbdf8e0302e781"
dependencies = [
"csv",
"encode_unicode",
"encode_unicode 1.0.0",
"is-terminal",
"lazy_static",
"term",
@ -1230,17 +1251,6 @@ 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 = "rsa"
version = "0.9.6"
@ -1261,16 +1271,6 @@ dependencies = [
"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]]
name = "rustc-demangle"
version = "0.1.23"
@ -1410,6 +1410,12 @@ dependencies = [
"digest",
]
[[package]]
name = "shell-words"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
[[package]]
name = "signal-hook"
version = "0.3.17"
@ -2057,18 +2063,6 @@ version = "0.25.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "whoami"
version = "1.5.1"

View File

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

View File

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

View File

@ -2,6 +2,7 @@ use std::vec;
use anyhow::Context;
use clap::Parser;
use dialoguer::{Confirm, Password};
use sqlx::MySqlConnection;
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> {
let pass1 = rpassword::prompt_password(format!("New MySQL password for user '{}': ", username))
.context("Failed to read password")?;
let pass2 = rpassword::prompt_password(format!(
"Retype new MySQL password for user '{}': ",
username
))
.context("Failed to read password")?;
if pass1 != pass2 {
anyhow::bail!("Passwords do not match");
}
Ok(pass1)
Password::new()
.with_prompt(format!("New MySQL password for user '{}'", username))
.with_confirmation(
format!("Retype new MySQL password for user '{}'", username),
"Passwords do not match",
)
.interact()
.map_err(Into::into)
}
async fn change_password_for_user(