2022-08-23 18:17:38 +03:00
|
|
|
module os
|
|
|
|
|
2023-03-30 08:58:52 +03:00
|
|
|
import term.termios
|
2022-08-23 18:17:38 +03:00
|
|
|
|
|
|
|
// input_password prompts the user for a password-like secret. It disables
|
|
|
|
// the terminal echo during user input and resets it back to normal when done.
|
|
|
|
pub fn input_password(prompt string) !string {
|
|
|
|
if is_atty(1) <= 0 || getenv('TERM') == 'dumb' {
|
|
|
|
return error('Could not obtain password discretely.')
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:58:52 +03:00
|
|
|
mut old_state := termios.Termios{}
|
|
|
|
if termios.tcgetattr(0, mut old_state) != 0 {
|
2022-08-23 18:17:38 +03:00
|
|
|
return last_error()
|
|
|
|
}
|
|
|
|
|
|
|
|
mut new_state := old_state
|
2023-07-31 10:28:45 +03:00
|
|
|
new_state.disable_echo()
|
|
|
|
termios.set_state(0, new_state)
|
2022-08-23 18:17:38 +03:00
|
|
|
|
|
|
|
password := input_opt(prompt) or { return error('Failed to read password') }
|
|
|
|
|
2023-07-31 10:28:45 +03:00
|
|
|
termios.set_state(0, old_state)
|
|
|
|
|
|
|
|
println('')
|
2022-08-23 18:17:38 +03:00
|
|
|
return password
|
|
|
|
}
|