LibCore: Use ErrorOr<T> for Core::get_password()

This commit is contained in:
Andreas Kling 2021-11-07 11:24:26 +01:00
parent 801d46d02c
commit e76b21a66f
Notes: sideshowbarker 2024-07-18 01:23:43 +09:00
3 changed files with 11 additions and 13 deletions

View file

@ -13,19 +13,19 @@
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)
return OSError(errno);
return Error::from_errno(errno);
termios original {};
if (tcgetattr(STDIN_FILENO, &original) < 0)
return OSError(errno);
return Error::from_errno(errno);
termios no_echo = original;
no_echo.c_lflag &= ~ECHO;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &no_echo) < 0)
return OSError(errno);
return Error::from_errno(errno);
char* password = nullptr;
size_t n = 0;
@ -37,7 +37,7 @@ Result<SecretString, OSError> get_password(const StringView& prompt)
putchar('\n');
if (line_length < 0)
return OSError(saved_errno);
return Error::from_errno(saved_errno);
VERIFY(line_length != 0);

View file

@ -6,13 +6,11 @@
#pragma once
#include <AK/OSError.h>
#include <AK/Result.h>
#include <AK/String.h>
#include <AK/Error.h>
#include <LibCore/SecretString.h>
namespace Core {
Result<SecretString, OSError> get_password(const StringView& prompt = "Password: "sv);
ErrorOr<SecretString> get_password(StringView prompt = "Password: "sv);
}

View file

@ -35,12 +35,12 @@ int main(int argc, char** argv)
args_parser.parse(argc, argv);
if (interactive_password) {
auto password_or_err = Core::get_password();
if (password_or_err.is_error()) {
warnln("{}", password_or_err.error().string());
auto password_or_error = Core::get_password();
if (password_or_error.is_error()) {
warnln("{}", password_or_error.error());
return 1;
}
password = password_or_err.release_value();
password = password_or_error.release_value();
} else {
auto standard_input = Core::File::standard_input();
password = Core::SecretString::take_ownership(standard_input->read_all());