mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 23:14:13 +01:00
ircd::conf: Add environ() to interface; minor cleanup/reorg.
This commit is contained in:
parent
3992af9388
commit
6ce5ab5b1d
2 changed files with 54 additions and 17 deletions
|
@ -61,7 +61,8 @@ namespace ircd::conf
|
|||
extern std::map<string_view, item<> *> items;
|
||||
extern callbacks<void (item<> &)> on_init;
|
||||
|
||||
bool exists(const string_view &key);
|
||||
bool exists(const string_view &key) noexcept;
|
||||
bool environ(const string_view &key) noexcept;
|
||||
bool persists(const string_view &key);
|
||||
string_view get(const mutable_buffer &out, const string_view &key);
|
||||
std::string get(const string_view &key);
|
||||
|
|
68
ircd/conf.cc
68
ircd/conf.cc
|
@ -8,13 +8,22 @@
|
|||
// copyright notice and this permission notice is present in all copies. The
|
||||
// full license for this software is available in the LICENSE file.
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
decltype(ircd::conf::items)
|
||||
ircd::conf::items
|
||||
{};
|
||||
ircd::conf::items;
|
||||
|
||||
decltype(ircd::conf::on_init)
|
||||
ircd::conf::on_init
|
||||
{};
|
||||
ircd::conf::on_init;
|
||||
|
||||
decltype(ircd::defaults)
|
||||
ircd::defaults
|
||||
|
@ -192,8 +201,36 @@ catch(const std::out_of_range &e)
|
|||
};
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
bool
|
||||
ircd::conf::exists(const string_view &key)
|
||||
noexcept
|
||||
{
|
||||
return items.count(key);
|
||||
}
|
||||
|
@ -341,16 +378,6 @@ const
|
|||
return {};
|
||||
}
|
||||
|
||||
namespace ircd::conf
|
||||
{
|
||||
static string_view make_env_name(const mutable_buffer &, const item<void> &, const string_view &);
|
||||
static string_view make_env_name(const mutable_buffer &, const item<void> &);
|
||||
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;
|
||||
}
|
||||
|
||||
void
|
||||
ircd::conf::item<void>::call_init()
|
||||
{
|
||||
|
@ -516,9 +543,18 @@ ircd::conf::make_env_name(const mutable_buffer &buf,
|
|||
ircd::string_view
|
||||
ircd::conf::make_env_name(const mutable_buffer &buf,
|
||||
const item<void> &item)
|
||||
noexcept
|
||||
{
|
||||
assert(size(item.name) <= conf::NAME_MAX_LEN);
|
||||
return replace(buf, item.name, '.', '_');
|
||||
return make_env_name(buf, item.name);
|
||||
}
|
||||
|
||||
ircd::string_view
|
||||
ircd::conf::make_env_name(const mutable_buffer &buf,
|
||||
const string_view &name)
|
||||
noexcept
|
||||
{
|
||||
assert(size(name) <= conf::NAME_MAX_LEN);
|
||||
return replace(buf, name, '.', '_');
|
||||
}
|
||||
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue