2018-03-02 09:35:02 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
2022-07-11 21:48:23 +02:00
|
|
|
namespace ircd::conf
|
|
|
|
{
|
|
|
|
static string_view make_env_name(const mutable_buffer &, const string_view &) noexcept;
|
|
|
|
static string_view make_env_name(const mutable_buffer &, const item<void> &) noexcept;
|
|
|
|
static string_view make_env_name(const mutable_buffer &, const item<void> &, const string_view &);
|
|
|
|
static void prepend_from_env(item<void> &) noexcept;
|
|
|
|
static void append_from_env(item<void> &) noexcept;
|
|
|
|
static void set_from_env(item<void> &) noexcept;
|
|
|
|
static void set_from_closure(item<void> &) noexcept;
|
|
|
|
}
|
|
|
|
|
2018-08-13 23:51:06 +02:00
|
|
|
decltype(ircd::conf::items)
|
2022-07-11 21:48:23 +02:00
|
|
|
ircd::conf::items;
|
2018-03-02 09:35:02 +01:00
|
|
|
|
2018-09-15 09:37:16 +02:00
|
|
|
decltype(ircd::conf::on_init)
|
2022-07-11 21:48:23 +02:00
|
|
|
ircd::conf::on_init;
|
2018-09-03 13:39:44 +02:00
|
|
|
|
2020-04-25 06:26:06 +02:00
|
|
|
decltype(ircd::defaults)
|
|
|
|
ircd::defaults
|
|
|
|
{
|
|
|
|
{ "name", "ircd.defaults" },
|
|
|
|
{ "default", false },
|
|
|
|
{ "persist", false },
|
|
|
|
};
|
|
|
|
|
2018-09-03 05:59:18 +02:00
|
|
|
size_t
|
|
|
|
ircd::conf::reset()
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
for(const auto &p : items)
|
|
|
|
ret += reset(p.first);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::conf::reset(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return reset(std::nothrow, key);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::conf::reset(std::nothrow_t,
|
|
|
|
const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &item(*items.at(key));
|
|
|
|
if(!item.set_cb)
|
|
|
|
return false;
|
|
|
|
|
2023-02-27 23:21:08 +01:00
|
|
|
item.set_cb(item);
|
2018-09-03 05:59:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"conf item[%s] set callback :%s",
|
|
|
|
key,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2019-03-11 20:16:32 +01:00
|
|
|
void
|
|
|
|
ircd::conf::fault(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &item(*items.at(key));
|
|
|
|
item.fault();
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::conf::fault(std::nothrow_t,
|
|
|
|
const string_view &key)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
auto &item(*items.at(key));
|
|
|
|
item.fault();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-02 12:03:33 +01:00
|
|
|
bool
|
|
|
|
ircd::conf::set(std::nothrow_t,
|
|
|
|
const string_view &key,
|
|
|
|
const string_view &value)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return set(key, value);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::conf::set(const string_view &key,
|
|
|
|
const string_view &value)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto &item(*items.at(key));
|
|
|
|
return item.set(value);
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw bad_value
|
|
|
|
{
|
|
|
|
"Conf item '%s' rejected value '%s'", key, value
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:19:24 +01:00
|
|
|
template<>
|
|
|
|
bool
|
|
|
|
ircd::conf::as(const string_view &key)
|
|
|
|
{
|
|
|
|
char buf[5]; // "true" or "false"
|
|
|
|
const auto val
|
|
|
|
{
|
|
|
|
get(buf, key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return lex_cast<bool>(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
bool
|
|
|
|
ircd::conf::as(const string_view &key,
|
|
|
|
bool def)
|
|
|
|
{
|
|
|
|
char buf[5]; // "true" or "false"
|
|
|
|
const auto val
|
|
|
|
{
|
|
|
|
get(std::nothrow, buf, key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(val && ircd::lex_castable<bool>(val))
|
|
|
|
def = lex_cast<bool>(val);
|
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:13:14 +01:00
|
|
|
std::string
|
|
|
|
ircd::conf::get(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &item(*items.at(key));
|
|
|
|
return item.get();
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-26 05:45:30 +02:00
|
|
|
ircd::string_view
|
2020-06-09 02:15:17 +02:00
|
|
|
ircd::conf::get(const mutable_buffer &out,
|
|
|
|
const string_view &key)
|
2018-05-26 05:45:30 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &item(*items.at(key));
|
|
|
|
return item.get(out);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-19 20:04:56 +01:00
|
|
|
std::string
|
|
|
|
ircd::conf::get(std::nothrow_t,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
const auto it(items.find(key));
|
|
|
|
if(it == end(items))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto &item(*it->second);
|
|
|
|
return item.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::get(std::nothrow_t,
|
|
|
|
const mutable_buffer &out,
|
|
|
|
const string_view &key)
|
|
|
|
{
|
|
|
|
const auto it(items.find(key));
|
|
|
|
if(it == end(items))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
const auto &item(*it->second);
|
|
|
|
return item.get(out);
|
|
|
|
}
|
|
|
|
|
2018-12-09 01:15:22 +01:00
|
|
|
bool
|
|
|
|
ircd::conf::persists(const string_view &key)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &item(*items.at(key));
|
|
|
|
return item.feature.get("persist", true);
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw not_found
|
|
|
|
{
|
|
|
|
"Conf item '%s' is not available", key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:48:23 +02:00
|
|
|
bool
|
|
|
|
ircd::conf::environ(const string_view &key)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
char buf[conf::NAME_MAX_LEN];
|
|
|
|
const auto env_key
|
|
|
|
{
|
|
|
|
make_env_name(buf, key)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto val
|
|
|
|
{
|
|
|
|
util::getenv(env_key)
|
|
|
|
};
|
|
|
|
|
|
|
|
return bool(val);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"%s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-26 05:45:30 +02:00
|
|
|
bool
|
|
|
|
ircd::conf::exists(const string_view &key)
|
2022-07-11 21:48:23 +02:00
|
|
|
noexcept
|
2018-05-26 05:45:30 +02:00
|
|
|
{
|
|
|
|
return items.count(key);
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:35:02 +01:00
|
|
|
//
|
|
|
|
// item
|
|
|
|
//
|
|
|
|
|
|
|
|
/// Conf item abstract constructor.
|
2018-08-22 22:58:10 +02:00
|
|
|
ircd::conf::item<void>::item(const json::members &opts,
|
|
|
|
conf::set_cb set_cb)
|
2018-03-02 09:35:02 +01:00
|
|
|
:feature_
|
|
|
|
{
|
|
|
|
opts
|
|
|
|
}
|
|
|
|
,feature
|
|
|
|
{
|
|
|
|
feature_
|
|
|
|
}
|
|
|
|
,name
|
|
|
|
{
|
2018-03-02 12:03:33 +01:00
|
|
|
unquote(feature.at("name"))
|
2018-03-02 09:35:02 +01:00
|
|
|
}
|
2018-08-22 22:58:10 +02:00
|
|
|
,set_cb
|
|
|
|
{
|
|
|
|
std::move(set_cb)
|
|
|
|
}
|
2018-03-02 09:35:02 +01:00
|
|
|
{
|
2019-03-11 19:05:54 +01:00
|
|
|
if(name.size() > NAME_MAX_LEN)
|
2018-09-15 11:20:00 +02:00
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Conf item '%s' name length:%zu exceeds max:%zu",
|
|
|
|
name,
|
2019-03-11 19:05:54 +01:00
|
|
|
name.size(),
|
2018-09-15 11:20:00 +02:00
|
|
|
NAME_MAX_LEN
|
|
|
|
};
|
|
|
|
|
2018-03-02 12:03:33 +01:00
|
|
|
if(!items.emplace(name, this).second)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"Conf item named '%s' already exists", name
|
|
|
|
};
|
2018-03-02 09:35:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::conf::item<void>::~item()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-03-02 12:03:33 +01:00
|
|
|
if(name)
|
|
|
|
{
|
|
|
|
const auto it{items.find(name)};
|
|
|
|
assert(data(it->first) == data(name));
|
|
|
|
items.erase(it);
|
|
|
|
}
|
2018-03-02 09:35:02 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 20:16:32 +01:00
|
|
|
void
|
|
|
|
ircd::conf::item<void>::fault()
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const json::string &default_
|
|
|
|
{
|
|
|
|
feature.get("default")
|
|
|
|
};
|
|
|
|
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"conf item[%s] defaulting with featured value :%s",
|
|
|
|
name,
|
|
|
|
string_view{default_}
|
|
|
|
};
|
|
|
|
|
|
|
|
if(on_set(default_))
|
|
|
|
if(set_cb)
|
2023-02-27 23:21:08 +01:00
|
|
|
set_cb(*this);
|
2019-03-11 20:16:32 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-08-06 01:20:55 +02:00
|
|
|
terminate
|
2019-03-11 20:16:32 +01:00
|
|
|
{
|
2019-08-06 01:20:55 +02:00
|
|
|
panic
|
|
|
|
{
|
|
|
|
"Conf item '%s' failed to set its default value (double-fault) :%s",
|
|
|
|
name,
|
|
|
|
e.what()
|
|
|
|
}
|
2019-03-11 20:16:32 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-02 09:35:02 +01:00
|
|
|
bool
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::conf::item<void>::set(const string_view &val)
|
|
|
|
{
|
2019-03-11 19:17:35 +01:00
|
|
|
std::string existing(get()); try
|
2018-08-22 22:57:03 +02:00
|
|
|
{
|
2019-03-11 19:17:35 +01:00
|
|
|
if(on_set(val))
|
|
|
|
if(set_cb)
|
2023-02-27 23:21:08 +01:00
|
|
|
set_cb(*this);
|
2018-08-22 22:58:10 +02:00
|
|
|
}
|
2019-03-11 19:17:35 +01:00
|
|
|
catch(...)
|
2018-08-22 22:58:10 +02:00
|
|
|
{
|
2019-03-11 20:16:32 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if(on_set(existing))
|
|
|
|
if(set_cb)
|
2023-02-27 23:21:08 +01:00
|
|
|
set_cb(*this);
|
2019-03-11 20:16:32 +01:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
fault();
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:17:35 +01:00
|
|
|
throw;
|
2018-08-22 22:58:10 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:17:35 +01:00
|
|
|
return true;
|
2018-08-22 22:57:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:13:14 +01:00
|
|
|
std::string
|
|
|
|
ircd::conf::item<void>::get()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return ircd::string(size(), [this]
|
|
|
|
(const mutable_buffer &buf)
|
|
|
|
{
|
|
|
|
return get(buf);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::item<void>::get(const mutable_buffer &buf)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return on_get(buf);
|
|
|
|
}
|
|
|
|
|
2018-09-15 10:49:51 +02:00
|
|
|
bool
|
|
|
|
ircd::conf::item<void>::on_set(const string_view &)
|
|
|
|
{
|
2019-03-11 19:17:35 +01:00
|
|
|
return true;
|
2018-09-15 10:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::item<void>::on_get(const mutable_buffer &)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:39:44 +02:00
|
|
|
void
|
|
|
|
ircd::conf::item<void>::call_init()
|
|
|
|
{
|
2018-09-15 10:49:51 +02:00
|
|
|
// The conf item's default value specified by the constructor is its
|
|
|
|
// current value at this point; now we make callbacks to allow that value
|
|
|
|
// to be replaced with a better one (i.e reading a saved value from DB).
|
2021-12-19 23:25:44 +01:00
|
|
|
conf::set_from_closure(*this);
|
2018-09-15 10:49:51 +02:00
|
|
|
|
2021-12-19 23:59:14 +01:00
|
|
|
// Environment variables now get the final say; this allows any
|
2018-09-15 10:49:51 +02:00
|
|
|
// misconfiguration to be overridden by env vars. The variable name is
|
|
|
|
// the conf item name with any '.' replaced to '_', case is preserved.
|
2021-12-19 23:59:14 +01:00
|
|
|
|
|
|
|
// Prepend to item's current value from env.
|
|
|
|
conf::prepend_from_env(*this);
|
|
|
|
|
|
|
|
// Append to item's current value from env.
|
|
|
|
conf::append_from_env(*this);
|
|
|
|
|
|
|
|
// Overwrite item's value if env is set.
|
2021-12-19 23:25:44 +01:00
|
|
|
conf::set_from_env(*this);
|
2018-09-15 10:49:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-12-19 23:25:44 +01:00
|
|
|
ircd::conf::set_from_closure(item<void> &item)
|
2018-09-15 10:49:51 +02:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
on_init(item);
|
2018-09-03 13:39:44 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
2018-09-15 10:49:51 +02:00
|
|
|
"conf item[%s] on_init callback :%s",
|
|
|
|
item.name,
|
2018-09-03 13:39:44 +02:00
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:59:14 +01:00
|
|
|
void
|
|
|
|
ircd::conf::prepend_from_env(item<void> &item)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
thread_local char buf[conf::NAME_MAX_LEN];
|
|
|
|
const auto key
|
|
|
|
{
|
|
|
|
make_env_name(buf, item, "PREPEND")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto env
|
|
|
|
{
|
|
|
|
util::getenv(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(empty(env))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto val
|
|
|
|
{
|
|
|
|
item.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
val = std::string{env} + val;
|
|
|
|
item.set(val);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"conf item[%s] prepending from environment variable :%s",
|
|
|
|
item.name,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::conf::append_from_env(item<void> &item)
|
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
thread_local char buf[conf::NAME_MAX_LEN];
|
|
|
|
const auto key
|
|
|
|
{
|
|
|
|
make_env_name(buf, item, "APPEND")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto env
|
|
|
|
{
|
|
|
|
util::getenv(key)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(empty(env))
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto val
|
|
|
|
{
|
|
|
|
item.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
val += env;
|
|
|
|
item.set(val);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"conf item[%s] appending from environment variable :%s",
|
|
|
|
item.name,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-15 10:49:51 +02:00
|
|
|
void
|
2021-12-19 23:25:44 +01:00
|
|
|
ircd::conf::set_from_env(item<void> &item)
|
2018-09-15 10:49:51 +02:00
|
|
|
noexcept try
|
2018-03-02 09:35:02 +01:00
|
|
|
{
|
2021-12-19 23:25:44 +01:00
|
|
|
thread_local char buf[conf::NAME_MAX_LEN];
|
2021-12-19 23:59:14 +01:00
|
|
|
const auto key
|
2018-09-15 10:49:51 +02:00
|
|
|
{
|
2021-12-19 23:25:44 +01:00
|
|
|
make_env_name(buf, item)
|
2018-09-15 10:49:51 +02:00
|
|
|
};
|
2018-03-02 09:35:02 +01:00
|
|
|
|
2021-12-19 23:59:14 +01:00
|
|
|
const auto val
|
2018-09-15 10:49:51 +02:00
|
|
|
{
|
2021-12-19 23:25:44 +01:00
|
|
|
util::getenv(key)
|
2018-09-15 10:49:51 +02:00
|
|
|
};
|
|
|
|
|
2021-12-19 23:59:14 +01:00
|
|
|
if(empty(val) && !null(val))
|
|
|
|
return;
|
|
|
|
|
|
|
|
item.set(val);
|
2018-09-15 10:49:51 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
2018-03-02 12:03:33 +01:00
|
|
|
{
|
2018-09-15 10:49:51 +02:00
|
|
|
log::error
|
|
|
|
{
|
2021-12-19 23:59:14 +01:00
|
|
|
"conf item[%s] setting from environment variable :%s",
|
2018-09-15 10:49:51 +02:00
|
|
|
item.name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-02 12:03:33 +01:00
|
|
|
}
|
|
|
|
|
2021-12-19 23:59:14 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::make_env_name(const mutable_buffer &buf,
|
|
|
|
const item<void> &item,
|
|
|
|
const string_view &feature)
|
|
|
|
{
|
2022-05-20 00:25:21 +02:00
|
|
|
char tmp[conf::NAME_MAX_LEN] {0};
|
2021-12-19 23:59:14 +01:00
|
|
|
const auto name
|
|
|
|
{
|
|
|
|
make_env_name(tmp, item)
|
|
|
|
};
|
|
|
|
|
2022-05-20 00:25:21 +02:00
|
|
|
return string_view
|
2021-12-19 23:59:14 +01:00
|
|
|
{
|
2022-06-24 04:19:08 +02:00
|
|
|
data(buf), unsigned(::snprintf
|
2022-05-20 00:25:21 +02:00
|
|
|
(
|
|
|
|
data(buf), size(buf), "%s__%s",
|
|
|
|
name.c_str(),
|
|
|
|
feature.c_str()
|
2022-06-24 04:19:08 +02:00
|
|
|
))
|
2021-12-19 23:59:14 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-19 23:25:44 +01:00
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::make_env_name(const mutable_buffer &buf,
|
|
|
|
const item<void> &item)
|
2022-07-11 21:48:23 +02:00
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return make_env_name(buf, item.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
|
|
|
ircd::conf::make_env_name(const mutable_buffer &buf,
|
|
|
|
const string_view &name)
|
|
|
|
noexcept
|
2021-12-19 23:25:44 +01:00
|
|
|
{
|
2022-07-11 21:48:23 +02:00
|
|
|
assert(size(name) <= conf::NAME_MAX_LEN);
|
|
|
|
return replace(buf, name, '.', '_');
|
2021-12-19 23:25:44 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 03:33:36 +02:00
|
|
|
//
|
|
|
|
// Non-inline template specialization definitions
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
// std::string
|
|
|
|
//
|
|
|
|
|
2018-08-22 22:58:10 +02:00
|
|
|
ircd::conf::item<std::string>::item(const json::members &members,
|
|
|
|
conf::set_cb set_cb)
|
|
|
|
:conf::item<>
|
|
|
|
{
|
|
|
|
members, std::move(set_cb)
|
|
|
|
}
|
|
|
|
,value
|
|
|
|
{
|
|
|
|
unquote(feature.get("default"))
|
|
|
|
}
|
2018-05-21 03:33:36 +02:00
|
|
|
{
|
2018-09-03 13:39:44 +02:00
|
|
|
call_init();
|
2018-05-21 03:33:36 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:05:54 +01:00
|
|
|
size_t
|
|
|
|
ircd::conf::item<std::string>::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return _value.size();
|
|
|
|
}
|
|
|
|
|
2018-05-21 03:33:36 +02:00
|
|
|
bool
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::conf::item<std::string>::on_set(const string_view &s)
|
2018-05-21 03:33:36 +02:00
|
|
|
{
|
|
|
|
_value = std::string{s};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::conf::item<std::string>::on_get(const mutable_buffer &out)
|
2018-05-21 03:33:36 +02:00
|
|
|
const
|
|
|
|
{
|
2019-03-11 19:05:54 +01:00
|
|
|
return { data(out), _value.copy(data(out), buffer::size(out)) };
|
2018-05-21 03:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// bool
|
|
|
|
//
|
|
|
|
|
2018-08-22 22:58:10 +02:00
|
|
|
ircd::conf::item<bool>::item(const json::members &members,
|
|
|
|
conf::set_cb set_cb)
|
|
|
|
:conf::item<>
|
|
|
|
{
|
|
|
|
members, std::move(set_cb)
|
|
|
|
}
|
|
|
|
,value
|
|
|
|
{
|
|
|
|
feature.get<bool>("default", false)
|
|
|
|
}
|
2018-05-21 03:33:36 +02:00
|
|
|
{
|
2018-09-03 13:39:44 +02:00
|
|
|
call_init();
|
2018-05-21 03:33:36 +02:00
|
|
|
}
|
|
|
|
|
2019-03-11 19:05:54 +01:00
|
|
|
size_t
|
|
|
|
ircd::conf::item<bool>::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return _value?
|
|
|
|
ircd::size("true"_sv):
|
|
|
|
ircd::size("false"_sv);
|
|
|
|
}
|
|
|
|
|
2018-05-21 03:33:36 +02:00
|
|
|
bool
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::conf::item<bool>::on_set(const string_view &s)
|
2018-09-15 12:34:40 +02:00
|
|
|
try
|
2018-05-21 03:33:36 +02:00
|
|
|
{
|
2018-09-15 12:34:40 +02:00
|
|
|
_value = lex_cast<bool>(s);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const bad_lex_cast &e)
|
|
|
|
{
|
|
|
|
throw bad_value
|
2018-05-21 03:33:36 +02:00
|
|
|
{
|
2018-09-15 12:34:40 +02:00
|
|
|
"Conf item '%s' not assigned a bool literal :%s",
|
|
|
|
name,
|
|
|
|
e.what()
|
|
|
|
};
|
2018-05-21 03:33:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::string_view
|
2018-08-22 22:57:03 +02:00
|
|
|
ircd::conf::item<bool>::on_get(const mutable_buffer &out)
|
2018-05-21 03:33:36 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return _value?
|
2019-03-11 19:05:54 +01:00
|
|
|
string_view { data(out), copy(out, "true"_sv) }:
|
|
|
|
string_view { data(out), copy(out, "false"_sv) };
|
2018-05-21 03:33:36 +02:00
|
|
|
}
|