ladybird/Userland/Utilities/config.cpp
Mustafa Quraish b0bd4be59a Config CLI: Handle missing config values correctly
If the domain/group/key doesn't exist in the config, exit with
non-zero status and don't print out anything.

Previously the CLI would print a single empty line if the config
value was not found with LibConfig. Now, we use the proper
`Config::Client::the().read_string()` API which can return an
`Optional` type indicating failure.`
2021-08-31 16:52:58 +02:00

37 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibConfig/Client.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
int main(int argc, char** argv)
{
Core::EventLoop loop;
String domain;
String group;
String key;
String value_to_write;
Core::ArgsParser args_parser;
args_parser.set_general_help("Show or modify values in the configuration files through ConfigServer.");
args_parser.add_positional_argument(domain, "Config domain", "domain");
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);
args_parser.parse(argc, argv);
if (!value_to_write.is_empty()) {
Config::write_string(domain, group, key, value_to_write);
return 0;
}
auto value_or_error = Config::Client::the().read_string_value(domain, group, key);
if (!value_or_error.has_value())
return 1;
outln("{}", value_or_error.value());
return 0;
}