mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:07:44 +00:00
LibCore: Use ErrorOr<T> for Core::get_password()
This commit is contained in:
parent
801d46d02c
commit
e76b21a66f
3 changed files with 11 additions and 13 deletions
|
@ -13,19 +13,19 @@
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
Result<SecretString, OSError> get_password(const StringView& prompt)
|
ErrorOr<SecretString> get_password(StringView prompt)
|
||||||
{
|
{
|
||||||
if (write(STDOUT_FILENO, prompt.characters_without_null_termination(), prompt.length()) < 0)
|
if (write(STDOUT_FILENO, prompt.characters_without_null_termination(), prompt.length()) < 0)
|
||||||
return OSError(errno);
|
return Error::from_errno(errno);
|
||||||
|
|
||||||
termios original {};
|
termios original {};
|
||||||
if (tcgetattr(STDIN_FILENO, &original) < 0)
|
if (tcgetattr(STDIN_FILENO, &original) < 0)
|
||||||
return OSError(errno);
|
return Error::from_errno(errno);
|
||||||
|
|
||||||
termios no_echo = original;
|
termios no_echo = original;
|
||||||
no_echo.c_lflag &= ~ECHO;
|
no_echo.c_lflag &= ~ECHO;
|
||||||
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0)
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0)
|
||||||
return OSError(errno);
|
return Error::from_errno(errno);
|
||||||
|
|
||||||
char* password = nullptr;
|
char* password = nullptr;
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
|
@ -37,7 +37,7 @@ Result<SecretString, OSError> get_password(const StringView& prompt)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
if (line_length < 0)
|
if (line_length < 0)
|
||||||
return OSError(saved_errno);
|
return Error::from_errno(saved_errno);
|
||||||
|
|
||||||
VERIFY(line_length != 0);
|
VERIFY(line_length != 0);
|
||||||
|
|
||||||
|
|
|
@ -6,13 +6,11 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/OSError.h>
|
#include <AK/Error.h>
|
||||||
#include <AK/Result.h>
|
|
||||||
#include <AK/String.h>
|
|
||||||
#include <LibCore/SecretString.h>
|
#include <LibCore/SecretString.h>
|
||||||
|
|
||||||
namespace Core {
|
namespace Core {
|
||||||
|
|
||||||
Result<SecretString, OSError> get_password(const StringView& prompt = "Password: "sv);
|
ErrorOr<SecretString> get_password(StringView prompt = "Password: "sv);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,12 +35,12 @@ int main(int argc, char** argv)
|
||||||
args_parser.parse(argc, argv);
|
args_parser.parse(argc, argv);
|
||||||
|
|
||||||
if (interactive_password) {
|
if (interactive_password) {
|
||||||
auto password_or_err = Core::get_password();
|
auto password_or_error = Core::get_password();
|
||||||
if (password_or_err.is_error()) {
|
if (password_or_error.is_error()) {
|
||||||
warnln("{}", password_or_err.error().string());
|
warnln("{}", password_or_error.error());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
password = password_or_err.release_value();
|
password = password_or_error.release_value();
|
||||||
} else {
|
} else {
|
||||||
auto standard_input = Core::File::standard_input();
|
auto standard_input = Core::File::standard_input();
|
||||||
password = Core::SecretString::take_ownership(standard_input->read_all());
|
password = Core::SecretString::take_ownership(standard_input->read_all());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue