From a344428b16bb39fa2d0e1205c4231dfe410bf64d Mon Sep 17 00:00:00 2001 From: Jack Westwood Date: Thu, 22 Aug 2024 08:07:35 +0100 Subject: [PATCH] Allow username and password to be passed into register with flags --- src/cli/cbenv_register.rs | 17 +++++++++++++- src/cli/credentials_create.rs | 2 +- src/cli/util.rs | 44 +++++++++++++++++++++++------------ 3 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/cli/cbenv_register.rs b/src/cli/cbenv_register.rs index ed194a13..ccb12364 100644 --- a/src/cli/cbenv_register.rs +++ b/src/cli/cbenv_register.rs @@ -42,6 +42,18 @@ impl Command for CbEnvRegister { SyntaxShape::String, "the connection string to use for this cluster", ) + .named( + "username", + SyntaxShape::String, + "the username to use for the registered cluster", + Some('u'), + ) + .named( + "password", + SyntaxShape::String, + "the password to use with the registered cluster", + Some('p'), + ) .named( "display_name", SyntaxShape::String, @@ -150,7 +162,10 @@ fn clusters_register( None }; - let (username, password) = get_username_and_password()?; + let username_flag = call.get_flag(engine_state, stack, "username")?; + let password_flag = call.get_flag(engine_state, stack, "password")?; + + let (username, password) = get_username_and_password(username_flag, password_flag)?; let cluster = RemoteCluster::new( RemoteClusterResources { diff --git a/src/cli/credentials_create.rs b/src/cli/credentials_create.rs index adddee71..ed517a0c 100644 --- a/src/cli/credentials_create.rs +++ b/src/cli/credentials_create.rs @@ -132,7 +132,7 @@ fn credentials_create( active_cluster.password().to_string(), ) } else { - get_username_and_password()? + get_username_and_password(None, None)? }; let payload = CredentialsCreateRequest::new(name.clone(), password.clone(), read, write); diff --git a/src/cli/util.rs b/src/cli/util.rs index ee8022fb..d1d42dfa 100644 --- a/src/cli/util.rs +++ b/src/cli/util.rs @@ -444,21 +444,35 @@ pub fn get_active_cluster<'a>( } } -pub fn get_username_and_password() -> Result<(String, String), ShellError> { - println!("Please enter username:"); - let username = match read_input() { - Some(user) => user, - None => { - return Err(generic_error("Username required", None, None)); - } - }; - - let password = match rpassword::prompt_password("Password: ") { - Ok(p) => p, - Err(_) => { - return Err(generic_error("Password required", None, None)); - } - }; +pub fn get_username_and_password( + user_flag: Option, + password_flag: Option, +) -> Result<(String, String), ShellError> { + let username = user_flag.map_or_else( + || { + println!("Please enter username:"); + read_input().ok_or_else(|| generic_error("Username required", None, None)) + }, + Ok, + )?; + + let password = password_flag.map_or_else( + || match rpassword::prompt_password("Password: ") { + Ok(p) => { + if p.is_empty() { + Err(generic_error("Password required", None, None)) + } else { + Ok(p) + } + } + Err(e) => Err(generic_error( + format!("Failed to parse password: {}", e), + None, + None, + )), + }, + Ok, + )?; Ok((username, password)) }