|
| 1 | +use crate::users::{get_all_users_keys, UserKeys}; |
| 2 | +use anyhow::{ensure, Context, Result}; |
| 3 | + |
| 4 | +fn prompt_single_user(user: &crate::users::UserKeys) -> Result<Vec<&crate::users::UserKeys>> { |
| 5 | + let prompt = format!( |
| 6 | + "Found only one user ({}) with {} SSH authorized keys. Would you like to install this user in the system?", |
| 7 | + user.user, |
| 8 | + user.num_keys(), |
| 9 | + ); |
| 10 | + let answer = ask_yes_no(&prompt, true)?; |
| 11 | + Ok(if answer { vec![&user] } else { vec![] }) |
| 12 | +} |
| 13 | + |
| 14 | +fn prompt_user_selection( |
| 15 | + all_users: &[crate::users::UserKeys], |
| 16 | +) -> Result<Vec<&crate::users::UserKeys>> { |
| 17 | + let keys: Vec<String> = all_users.iter().map(|x| x.user.clone()).collect(); |
| 18 | + |
| 19 | + // TODO: Handle https://github.com/console-rs/dialoguer/issues/77 |
| 20 | + let selected_user_indices: Vec<usize> = dialoguer::MultiSelect::new() |
| 21 | + .with_prompt("Select the users you want to install in the system (along with their authorized SSH keys)") |
| 22 | + .items(&keys) |
| 23 | + .interact()?; |
| 24 | + |
| 25 | + Ok(selected_user_indices |
| 26 | + .iter() |
| 27 | + // Safe unwrap because we know the index is valid |
| 28 | + .map(|x| all_users.get(*x).unwrap()) |
| 29 | + .collect()) |
| 30 | +} |
| 31 | + |
| 32 | +/// Temporary safety mechanism to stop devs from running it on their dev machine. TODO: Discuss |
| 33 | +/// final prompting UX in https://github.com/containers/bootc/discussions/1060 |
| 34 | +pub(crate) fn temporary_developer_protection_prompt() -> Result<()> { |
| 35 | + // Print an empty line so that the warning stands out from the rest of the output |
| 36 | + println!(); |
| 37 | + |
| 38 | + let prompt = "THIS WILL REINSTALL YOUR SYSTEM! Are you sure you want to continue?"; |
| 39 | + let answer = ask_yes_no(prompt, false)?; |
| 40 | + |
| 41 | + if !answer { |
| 42 | + println!("Exiting without reinstalling the system."); |
| 43 | + std::process::exit(0); |
| 44 | + } |
| 45 | + |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +pub(crate) fn ask_yes_no(prompt: &str, default: bool) -> Result<bool> { |
| 50 | + dialoguer::Confirm::new() |
| 51 | + .with_prompt(prompt) |
| 52 | + .default(default) |
| 53 | + .wait_for_newline(true) |
| 54 | + .interact() |
| 55 | + .context("prompting") |
| 56 | +} |
| 57 | + |
| 58 | +/// For now we only support the root user. This function returns the root user's SSH |
| 59 | +/// authorized_keys. In the future, when bootc supports multiple users, this function will need to |
| 60 | +/// be updated to return the SSH authorized_keys for all the users selected by the user. |
| 61 | +pub(crate) fn get_root_key() -> Result<Option<UserKeys>> { |
| 62 | + let users = get_all_users_keys()?; |
| 63 | + if users.is_empty() { |
| 64 | + return Ok(None); |
| 65 | + } |
| 66 | + |
| 67 | + let selected_users = if users.len() == 1 { |
| 68 | + prompt_single_user(&users[0])? |
| 69 | + } else { |
| 70 | + prompt_user_selection(&users)? |
| 71 | + }; |
| 72 | + |
| 73 | + ensure!( |
| 74 | + selected_users.iter().all(|x| x.user == "root"), |
| 75 | + "Only importing the root user keys is supported for now" |
| 76 | + ); |
| 77 | + |
| 78 | + let root_key = selected_users.into_iter().find(|x| x.user == "root"); |
| 79 | + |
| 80 | + Ok(root_key.cloned()) |
| 81 | +} |
0 commit comments