2020-07-11 22:18:13 +01:00
|
|
|
/*
|
2021-05-31 15:43:25 +01:00
|
|
|
* Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
|
2020-07-11 22:18:13 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-11 22:18:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/ConfigFile.h>
|
2023-02-08 21:08:01 +01:00
|
|
|
#include <LibCore/DeprecatedFile.h>
|
2021-12-24 08:24:01 -08:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-07-11 22:18:13 +01:00
|
|
|
|
2021-12-24 08:24:01 -08:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2020-07-11 22:18:13 +01:00
|
|
|
{
|
2021-12-24 08:24:01 -08:00
|
|
|
TRY(Core::System::pledge("stdio rpath wpath cpath"));
|
2020-07-11 22:18:13 +01:00
|
|
|
|
2022-04-24 16:09:19 +02:00
|
|
|
StringView path;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString group;
|
|
|
|
DeprecatedString key;
|
|
|
|
DeprecatedString value_to_write;
|
2020-07-11 22:18:13 +01:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(path, "Path to INI file", "path");
|
|
|
|
args_parser.add_positional_argument(group, "Group name", "group");
|
|
|
|
args_parser.add_positional_argument(key, "Key name", "key");
|
|
|
|
args_parser.add_positional_argument(value_to_write, "Value to write", "value", Core::ArgsParser::Required::No);
|
2021-12-24 08:24:01 -08:00
|
|
|
args_parser.parse(arguments);
|
2020-07-11 22:18:13 +01:00
|
|
|
|
2023-02-08 21:08:01 +01:00
|
|
|
if (!Core::DeprecatedFile::exists(path)) {
|
2021-05-31 15:43:25 +01:00
|
|
|
warnln("File does not exist: '{}'", path);
|
2020-07-11 22:18:13 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-04-24 16:09:19 +02:00
|
|
|
auto config = TRY(Core::ConfigFile::open(path, value_to_write.is_null() ? Core::ConfigFile::AllowWriting::No : Core::ConfigFile::AllowWriting::Yes));
|
2020-07-11 22:18:13 +01:00
|
|
|
|
2022-04-24 16:09:19 +02:00
|
|
|
if (!value_to_write.is_null()) {
|
2020-07-11 22:18:13 +01:00
|
|
|
config->write_entry(group, key, value_to_write);
|
2022-02-06 14:26:33 +00:00
|
|
|
TRY(config->sync());
|
2020-07-11 22:18:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto value = config->read_entry(group, key);
|
|
|
|
if (!value.is_empty())
|
2021-05-31 15:43:25 +01:00
|
|
|
outln("{}", value);
|
2020-07-11 22:18:13 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|