From 855c42571955e88975a2e96b197b4a03297b1baa Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 2 Dec 2021 23:44:49 +0100 Subject: [PATCH] gron: Port to LibMain :^) --- Userland/Utilities/CMakeLists.txt | 1 + Userland/Utilities/gron.cpp | 48 ++++++++----------------------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index edc37d59e4..c2cb8381d7 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -80,6 +80,7 @@ target_link_libraries(functrace LibDebug LibX86) target_link_libraries(gml-format LibGUI) target_link_libraries(grep LibRegex) target_link_libraries(gunzip LibCompress) +target_link_libraries(gron LibMain) target_link_libraries(gzip LibCompress) target_link_libraries(id LibMain) target_link_libraries(jp LibMain) diff --git a/Userland/Utilities/gron.cpp b/Userland/Utilities/gron.cpp index 390657c3c9..ae93eb619d 100644 --- a/Userland/Utilities/gron.cpp +++ b/Userland/Utilities/gron.cpp @@ -10,7 +10,8 @@ #include #include #include -#include +#include +#include #include #include @@ -25,58 +26,33 @@ static const char* color_null = ""; static const char* color_string = ""; static const char* color_off = ""; -int main(int argc, char** argv) +ErrorOr serenity_main(Main::Arguments arguments) { - if (pledge("stdio tty rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath tty")); if (isatty(STDOUT_FILENO)) use_color = true; - if (pledge("stdio rpath", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio rpath")); Core::ArgsParser args_parser; args_parser.set_general_help("Print each value in a JSON file with its fully expanded key."); const char* path = nullptr; args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No); - - args_parser.parse(argc, argv); + args_parser.parse(arguments); RefPtr file; - if (!path) { + if (!path) file = Core::File::standard_input(); - } else { - auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly); - if (file_or_error.is_error()) { - warnln("Failed to open {}: {}", path, file_or_error.error()); - return 1; - } - file = file_or_error.value(); - } + else + file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly)); - if (pledge("stdio", nullptr) < 0) { - perror("pledge"); - return 1; - } + TRY(Core::System::pledge("stdio")); auto file_contents = file->read_all(); - auto json = JsonValue::from_string(file_contents); - - if (json.is_error()) { - if (path) { - warnln("Failed to parse '{}' as JSON", path); - } else { - warnln("Failed to parse stdin as JSON"); - } - return 1; - } + auto json = TRY(JsonValue::from_string(file_contents)); if (use_color) { color_name = "\033[33;1m"; @@ -89,7 +65,7 @@ int main(int argc, char** argv) } Vector trail; - print("json", json.value(), trail); + print("json", json, trail); return 0; }