Skip to content

Commit

Permalink
Allow username and password to be passed into register with flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Westwooo committed Aug 23, 2024
1 parent b105156 commit 19dec4c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
17 changes: 16 additions & 1 deletion src/cli/cbenv_register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
None,
)
.named(
"password",
SyntaxShape::String,
"the password to use with the registered cluster",
None,
)
.named(
"display_name",
SyntaxShape::String,
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/credentials_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 29 additions & 15 deletions src/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
password_flag: Option<String>,
) -> 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))
}
Expand Down

0 comments on commit 19dec4c

Please sign in to comment.