1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:07:45 +00:00

gron: Port to LibMain :^)

This commit is contained in:
Andreas Kling 2021-12-02 23:44:49 +01:00
parent b0659b50dc
commit 855c425719
2 changed files with 13 additions and 36 deletions

View file

@ -80,6 +80,7 @@ target_link_libraries(functrace LibDebug LibX86)
target_link_libraries(gml-format LibGUI) target_link_libraries(gml-format LibGUI)
target_link_libraries(grep LibRegex) target_link_libraries(grep LibRegex)
target_link_libraries(gunzip LibCompress) target_link_libraries(gunzip LibCompress)
target_link_libraries(gron LibMain)
target_link_libraries(gzip LibCompress) target_link_libraries(gzip LibCompress)
target_link_libraries(id LibMain) target_link_libraries(id LibMain)
target_link_libraries(jp LibMain) target_link_libraries(jp LibMain)

View file

@ -10,7 +10,8 @@
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <stdio.h> #include <LibCore/System.h>
#include <LibMain/Main.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
@ -25,58 +26,33 @@ static const char* color_null = "";
static const char* color_string = ""; static const char* color_string = "";
static const char* color_off = ""; static const char* color_off = "";
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
if (pledge("stdio tty rpath", nullptr) < 0) { TRY(Core::System::pledge("stdio rpath tty"));
perror("pledge");
return 1;
}
if (isatty(STDOUT_FILENO)) if (isatty(STDOUT_FILENO))
use_color = true; use_color = true;
if (pledge("stdio rpath", nullptr) < 0) { TRY(Core::System::pledge("stdio rpath"));
perror("pledge");
return 1;
}
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.set_general_help("Print each value in a JSON file with its fully expanded key."); args_parser.set_general_help("Print each value in a JSON file with its fully expanded key.");
const char* path = nullptr; const char* path = nullptr;
args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No); args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
args_parser.parse(argc, argv);
RefPtr<Core::File> file; RefPtr<Core::File> file;
if (!path) { if (!path)
file = Core::File::standard_input(); file = Core::File::standard_input();
} else { else
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly); file = TRY(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();
}
if (pledge("stdio", nullptr) < 0) { TRY(Core::System::pledge("stdio"));
perror("pledge");
return 1;
}
auto file_contents = file->read_all(); auto file_contents = file->read_all();
auto json = JsonValue::from_string(file_contents); auto json = TRY(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;
}
if (use_color) { if (use_color) {
color_name = "\033[33;1m"; color_name = "\033[33;1m";
@ -89,7 +65,7 @@ int main(int argc, char** argv)
} }
Vector<String> trail; Vector<String> trail;
print("json", json.value(), trail); print("json", json, trail);
return 0; return 0;
} }