Skip to content

Commit

Permalink
ui: Remove &mut requirement for prompt functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjmnt4n committed Mar 20, 2024
1 parent e51878f commit c45d64e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
24 changes: 8 additions & 16 deletions cli/src/git_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::Mutex;
use std::time::Instant;
use std::{error, iter};

Expand Down Expand Up @@ -60,11 +59,11 @@ pub fn is_colocated_git_workspace(workspace: &Workspace, repo: &ReadonlyRepo) ->
git_workdir.canonicalize().ok().as_deref() == dot_git_path.parent()
}

fn terminal_get_username(ui: &mut Ui, url: &str) -> Option<String> {
fn terminal_get_username(ui: &Ui, url: &str) -> Option<String> {
ui.prompt(&format!("Username for {url}")).ok()
}

fn terminal_get_pw(ui: &mut Ui, url: &str) -> Option<String> {
fn terminal_get_pw(ui: &Ui, url: &str) -> Option<String> {
ui.prompt_password(&format!("Passphrase for {url}: ")).ok()
}

Expand Down Expand Up @@ -138,13 +137,9 @@ fn get_ssh_keys(_username: &str) -> Vec<PathBuf> {
paths
}

pub fn with_remote_git_callbacks<T>(
ui: &mut Ui,
f: impl FnOnce(git::RemoteCallbacks<'_>) -> T,
) -> T {
let mut ui = Mutex::new(ui);
pub fn with_remote_git_callbacks<T>(ui: &Ui, f: impl FnOnce(git::RemoteCallbacks<'_>) -> T) -> T {
let mut callback = None;
if let Some(mut output) = ui.get_mut().unwrap().progress_output() {
if let Some(mut output) = ui.progress_output() {
let mut progress = Progress::new(Instant::now());
callback = Some(move |x: &git::Progress| {
_ = progress.update(Instant::now(), x, &mut output);
Expand All @@ -156,14 +151,11 @@ pub fn with_remote_git_callbacks<T>(
.map(|x| x as &mut dyn FnMut(&git::Progress));
let mut get_ssh_keys = get_ssh_keys; // Coerce to unit fn type
callbacks.get_ssh_keys = Some(&mut get_ssh_keys);
let mut get_pw = |url: &str, _username: &str| {
pinentry_get_pw(url).or_else(|| terminal_get_pw(*ui.lock().unwrap(), url))
};
let mut get_pw =
|url: &str, _username: &str| pinentry_get_pw(url).or_else(|| terminal_get_pw(ui, url));
callbacks.get_password = Some(&mut get_pw);
let mut get_user_pw = |url: &str| {
let ui = &mut *ui.lock().unwrap();
Some((terminal_get_username(ui, url)?, terminal_get_pw(ui, url)?))
};
let mut get_user_pw =
|url: &str| Some((terminal_get_username(ui, url)?, terminal_get_pw(ui, url)?));
callbacks.get_username_password = Some(&mut get_user_pw);
f(callbacks)
}
Expand Down
4 changes: 2 additions & 2 deletions cli/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ impl Ui {
.unwrap_or(false)
}

pub fn prompt(&mut self, prompt: &str) -> io::Result<String> {
pub fn prompt(&self, prompt: &str) -> io::Result<String> {
if !Self::can_prompt() {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
Expand Down Expand Up @@ -486,7 +486,7 @@ impl Ui {
Ok(choice.starts_with(['y', 'Y']))
}

pub fn prompt_password(&mut self, prompt: &str) -> io::Result<String> {
pub fn prompt_password(&self, prompt: &str) -> io::Result<String> {
if !io::stdout().is_terminal() {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
Expand Down

0 comments on commit c45d64e

Please sign in to comment.