Serenity Operating System
1/*
2 * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibCore/ConfigFile.h>
9#include <LibCore/DeprecatedFile.h>
10#include <LibCore/System.h>
11#include <LibMain/Main.h>
12
13ErrorOr<int> serenity_main(Main::Arguments arguments)
14{
15 TRY(Core::System::pledge("stdio rpath wpath cpath"));
16
17 StringView path;
18 DeprecatedString group;
19 DeprecatedString key;
20 DeprecatedString value_to_write;
21
22 Core::ArgsParser args_parser;
23 args_parser.add_positional_argument(path, "Path to INI file", "path");
24 args_parser.add_positional_argument(group, "Group name", "group");
25 args_parser.add_positional_argument(key, "Key name", "key");
26 args_parser.add_positional_argument(value_to_write, "Value to write", "value", Core::ArgsParser::Required::No);
27 args_parser.parse(arguments);
28
29 if (!Core::DeprecatedFile::exists(path)) {
30 warnln("File does not exist: '{}'", path);
31 return 1;
32 }
33
34 auto config = TRY(Core::ConfigFile::open(path, value_to_write.is_null() ? Core::ConfigFile::AllowWriting::No : Core::ConfigFile::AllowWriting::Yes));
35
36 if (!value_to_write.is_null()) {
37 config->write_entry(group, key, value_to_write);
38 TRY(config->sync());
39 return 0;
40 }
41
42 auto value = config->read_entry(group, key);
43 if (!value.is_empty())
44 outln("{}", value);
45
46 return 0;
47}