From 91901eafd8d607241363e481440786ef32b7ab78 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 30 Sep 2020 16:16:53 +0200 Subject: [PATCH] Introduce EnvResolver refs #6259 --- lib/icinga/CMakeLists.txt | 1 + lib/icinga/envresolver.cpp | 20 ++++++++++++++++++++ lib/icinga/envresolver.hpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 lib/icinga/envresolver.cpp create mode 100644 lib/icinga/envresolver.hpp diff --git a/lib/icinga/CMakeLists.txt b/lib/icinga/CMakeLists.txt index 7079d84e5..62077bce7 100644 --- a/lib/icinga/CMakeLists.txt +++ b/lib/icinga/CMakeLists.txt @@ -41,6 +41,7 @@ set(icinga_SOURCES customvarobject.cpp customvarobject.hpp customvarobject-ti.hpp dependency.cpp dependency.hpp dependency-ti.hpp dependency-apply.cpp downtime.cpp downtime.hpp downtime-ti.hpp + envresolver.cpp envresolver.hpp eventcommand.cpp eventcommand.hpp eventcommand-ti.hpp externalcommandprocessor.cpp externalcommandprocessor.hpp host.cpp host.hpp host-ti.hpp diff --git a/lib/icinga/envresolver.cpp b/lib/icinga/envresolver.cpp new file mode 100644 index 000000000..633255c86 --- /dev/null +++ b/lib/icinga/envresolver.cpp @@ -0,0 +1,20 @@ +/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */ + +#include "base/string.hpp" +#include "base/value.hpp" +#include "icinga/envresolver.hpp" +#include "icinga/checkresult.hpp" +#include + +using namespace icinga; + +bool EnvResolver::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *result) const +{ + auto value (getenv(macro.CStr())); + + if (value) { + *result = value; + } + + return value; +} diff --git a/lib/icinga/envresolver.hpp b/lib/icinga/envresolver.hpp new file mode 100644 index 000000000..b3f0076fa --- /dev/null +++ b/lib/icinga/envresolver.hpp @@ -0,0 +1,30 @@ +/* Icinga 2 | (c) 2020 Icinga GmbH | GPLv2+ */ + +#ifndef ENVRESOLVER_H +#define ENVRESOLVER_H + +#include "base/object.hpp" +#include "base/string.hpp" +#include "base/value.hpp" +#include "icinga/macroresolver.hpp" +#include "icinga/checkresult.hpp" + +namespace icinga +{ + +/** + * Resolves env var names. + * + * @ingroup icinga + */ +class EnvResolver final : public Object, public MacroResolver +{ +public: + DECLARE_PTR_TYPEDEFS(EnvResolver); + + bool ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *result) const override; +}; + +} + +#endif /* ENVRESOLVER_H */