Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --yes parameter for rye publish #270

Merged
merged 1 commit into from
May 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 33 additions & 18 deletions rye/src/cli/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct Args {
/// Path to alternate CA bundle.
#[arg(long)]
cert: Option<PathBuf>,
/// Skip prompts.
#[arg(short, long)]
yes: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -102,7 +105,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {

let token = if let Some(token) = cmd.token {
let secret = Secret::new(token);
let maybe_encrypted = prompt_maybe_encrypt(&secret)?;
let maybe_encrypted = maybe_encrypt(&secret, cmd.yes)?;
let maybe_encoded = maybe_encode(&secret, &maybe_encrypted);
credentials[repository]["token"] = Item::Value(maybe_encoded.expose_secret().into());
write_credentials(&credentials)?;
Expand All @@ -116,15 +119,19 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
{
let secret = Secret::new(token);

prompt_maybe_decrypt(&secret)?
maybe_decrypt(&secret, cmd.yes)?
} else {
eprintln!("No access token found, generate one at: https://pypi.org/manage/account/token/");
let token = prompt_for_token()?;
let token = if !cmd.yes {
prompt_for_token()?
} else {
"".to_string()
};
if token.is_empty() {
bail!("an access token is required")
}
let secret = Secret::new(token);
let maybe_encrypted = prompt_maybe_encrypt(&secret)?;
let maybe_encrypted = maybe_encrypt(&secret, cmd.yes)?;
let maybe_encoded = maybe_encode(&secret, &maybe_encrypted);
credentials[repository]["token"] = Item::Value(maybe_encoded.expose_secret().into());

Expand Down Expand Up @@ -177,13 +184,17 @@ fn prompt_for_token() -> Result<String, Error> {
Ok(token)
}

fn prompt_maybe_encrypt(secret: &Secret<String>) -> Result<Secret<Vec<u8>>, Error> {
let phrase = dialoguer::Password::new()
.with_prompt("Enter a passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?;
fn maybe_encrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<Vec<u8>>, Error> {
let phrase = if !yes {
dialoguer::Password::new()
.with_prompt("Enter a passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?
} else {
Secret::new("".to_string())
};

let token = if phrase.expose_secret().is_empty() {
secret.expose_secret().as_bytes().to_vec()
Expand All @@ -201,13 +212,17 @@ fn prompt_maybe_encrypt(secret: &Secret<String>) -> Result<Secret<Vec<u8>>, Erro
Ok(Secret::new(token.to_vec()))
}

fn prompt_maybe_decrypt(secret: &Secret<String>) -> Result<Secret<String>, Error> {
let phrase = dialoguer::Password::new()
.with_prompt("Enter a passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?;
fn maybe_decrypt(secret: &Secret<String>, yes: bool) -> Result<Secret<String>, Error> {
let phrase = if !yes {
dialoguer::Password::new()
.with_prompt("Enter a passphrase (optional)")
.allow_empty_password(true)
.report(false)
.interact()
.map(Secret::new)?
} else {
Secret::new("".to_string())
};

if phrase.expose_secret().is_empty() {
return Ok(secret.clone());
Expand Down