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