1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
//! Git style prompting with support for `GIT_ASKPASS` and `askpass` program configuration.
//!
//! ### Compatibility
//!
//! This is a unix-only crate which will return with an error when trying to obtain any prompt on other platforms.
//! On those platforms it is common to have helpers which perform this task so it shouldn't be a problem.
#![deny(rust_2018_idioms, missing_docs)]
#![forbid(unsafe_code)]
mod types;
pub use types::{Error, Mode, Options};
///
pub mod unix;
#[cfg(unix)]
use unix::imp;
#[cfg(not(unix))]
mod imp {
use crate::{Error, Options};
pub(crate) fn ask(_prompt: &str, _opts: &Options<'_>) -> Result<String, Error> {
Err(Error::UnsupportedPlatform)
}
}
/// Ask the user given a `prompt`, returning the result.
pub fn ask(prompt: &str, opts: &Options<'_>) -> Result<String, Error> {
if let Some(askpass) = opts.askpass.as_deref() {
match gix_command::prepare(askpass).arg(prompt).spawn() {
Ok(cmd) => {
if let Some(mut stdout) = cmd
.wait_with_output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
{
if stdout.ends_with('\n') {
stdout.pop();
}
if stdout.ends_with('\r') {
stdout.pop();
}
return Ok(stdout);
}
}
Err(err) => eprintln!("Cannot run askpass program: {askpass:?} with error: {err}"),
}
}
imp::ask(prompt, opts)
}
/// Ask for information typed by the user into the terminal after showing the prompt, like `"Username: `.
///
/// Use [`ask()`] for more control.
pub fn openly(prompt: impl AsRef<str>) -> Result<String, Error> {
imp::ask(
prompt.as_ref(),
&Options {
mode: Mode::Visible,
askpass: None,
},
)
}
/// Ask for information _securely_ after showing the `prompt` (like `"password: "`) by not showing what's typed.
///
/// Use [`ask()`] for more control.
pub fn securely(prompt: impl AsRef<str>) -> Result<String, Error> {
imp::ask(
prompt.as_ref(),
&Options {
mode: Mode::Hidden,
askpass: None,
},
)
}
|