mirror of
https://github.com/NixOS/nix.git
synced 2025-11-28 13:11:00 +01:00
Merge pull request #14632 from NixOS/path-setting
Add `Setting<std::filesystem::path>` and `Setting<std::optional<std::filesystem::path>>` specializations
This commit is contained in:
commit
35492fe94a
5 changed files with 50 additions and 12 deletions
|
|
@ -433,6 +433,42 @@ std::string BaseSetting<StringMap>::to_string() const
|
||||||
[](const auto & kvpair) { return kvpair.first + "=" + kvpair.second; });
|
[](const auto & kvpair) { return kvpair.first + "=" + kvpair.second; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Path parsePath(const AbstractSetting & s, const std::string & str)
|
||||||
|
{
|
||||||
|
if (str == "")
|
||||||
|
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
|
||||||
|
else
|
||||||
|
return canonPath(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
std::filesystem::path BaseSetting<std::filesystem::path>::parse(const std::string & str) const
|
||||||
|
{
|
||||||
|
return parsePath(*this, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
std::string BaseSetting<std::filesystem::path>::to_string() const
|
||||||
|
{
|
||||||
|
return value.string();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
std::optional<std::filesystem::path>
|
||||||
|
BaseSetting<std::optional<std::filesystem::path>>::parse(const std::string & str) const
|
||||||
|
{
|
||||||
|
if (str == "")
|
||||||
|
return std::nullopt;
|
||||||
|
else
|
||||||
|
return parsePath(*this, str);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
std::string BaseSetting<std::optional<std::filesystem::path>>::to_string() const
|
||||||
|
{
|
||||||
|
return value ? value->string() : "";
|
||||||
|
}
|
||||||
|
|
||||||
template class BaseSetting<int>;
|
template class BaseSetting<int>;
|
||||||
template class BaseSetting<unsigned int>;
|
template class BaseSetting<unsigned int>;
|
||||||
template class BaseSetting<long>;
|
template class BaseSetting<long>;
|
||||||
|
|
@ -445,14 +481,8 @@ template class BaseSetting<Strings>;
|
||||||
template class BaseSetting<StringSet>;
|
template class BaseSetting<StringSet>;
|
||||||
template class BaseSetting<StringMap>;
|
template class BaseSetting<StringMap>;
|
||||||
template class BaseSetting<std::set<ExperimentalFeature>>;
|
template class BaseSetting<std::set<ExperimentalFeature>>;
|
||||||
|
template class BaseSetting<std::filesystem::path>;
|
||||||
static Path parsePath(const AbstractSetting & s, const std::string & str)
|
template class BaseSetting<std::optional<std::filesystem::path>>;
|
||||||
{
|
|
||||||
if (str == "")
|
|
||||||
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
|
|
||||||
else
|
|
||||||
return canonPath(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
PathSetting::PathSetting(
|
PathSetting::PathSetting(
|
||||||
Config * options,
|
Config * options,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
#include "nix/util/configuration.hh"
|
#include "nix/util/configuration.hh"
|
||||||
#include "nix/util/args.hh"
|
#include "nix/util/args.hh"
|
||||||
#include "nix/util/logging.hh"
|
#include "nix/util/logging.hh"
|
||||||
|
#include "nix/util/file-path.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
|
@ -134,6 +135,8 @@ DECLARE_CONFIG_SERIALISER(Strings)
|
||||||
DECLARE_CONFIG_SERIALISER(StringSet)
|
DECLARE_CONFIG_SERIALISER(StringSet)
|
||||||
DECLARE_CONFIG_SERIALISER(StringMap)
|
DECLARE_CONFIG_SERIALISER(StringMap)
|
||||||
DECLARE_CONFIG_SERIALISER(std::set<ExperimentalFeature>)
|
DECLARE_CONFIG_SERIALISER(std::set<ExperimentalFeature>)
|
||||||
|
DECLARE_CONFIG_SERIALISER(std::filesystem::path)
|
||||||
|
DECLARE_CONFIG_SERIALISER(std::optional<std::filesystem::path>)
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T BaseSetting<T>::parse(const std::string & str) const
|
T BaseSetting<T>::parse(const std::string & str) const
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
#include "nix/util/types.hh"
|
#include "nix/util/types.hh"
|
||||||
#include "nix/util/os-string.hh"
|
#include "nix/util/os-string.hh"
|
||||||
|
#include "nix/util/json-non-null.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
|
@ -53,4 +54,8 @@ std::optional<std::filesystem::path> maybePath(PathView path);
|
||||||
|
|
||||||
std::filesystem::path pathNG(PathView path);
|
std::filesystem::path pathNG(PathView path);
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct json_avoids_null<std::filesystem::path> : std::true_type
|
||||||
|
{};
|
||||||
|
|
||||||
} // namespace nix
|
} // namespace nix
|
||||||
|
|
|
||||||
|
|
@ -54,9 +54,9 @@ struct LoggerSettings : Config
|
||||||
expression evaluation errors.
|
expression evaluation errors.
|
||||||
)"};
|
)"};
|
||||||
|
|
||||||
Setting<Path> jsonLogPath{
|
Setting<std::optional<std::filesystem::path>> jsonLogPath{
|
||||||
this,
|
this,
|
||||||
"",
|
{},
|
||||||
"json-log-path",
|
"json-log-path",
|
||||||
R"(
|
R"(
|
||||||
A file or unix socket to which JSON records of Nix's log output are
|
A file or unix socket to which JSON records of Nix's log output are
|
||||||
|
|
|
||||||
|
|
@ -367,10 +367,10 @@ std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool
|
||||||
|
|
||||||
void applyJSONLogger()
|
void applyJSONLogger()
|
||||||
{
|
{
|
||||||
if (!loggerSettings.jsonLogPath.get().empty()) {
|
if (auto & opt = loggerSettings.jsonLogPath.get()) {
|
||||||
try {
|
try {
|
||||||
std::vector<std::unique_ptr<Logger>> loggers;
|
std::vector<std::unique_ptr<Logger>> loggers;
|
||||||
loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false));
|
loggers.push_back(makeJSONLogger(*opt, false));
|
||||||
try {
|
try {
|
||||||
logger = makeTeeLogger(std::move(logger), std::move(loggers));
|
logger = makeTeeLogger(std::move(logger), std::move(loggers));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue