warn = null
}
+### getenv <a id="global-functions-getenv"></a>
+
+Signature:
+
+ function getenv(key)
+
+Returns the value from the specified environment variable key.
+
+Example:
+
+ $ MY_ENV_VAR=icinga2 icinga2 console
+ Icinga 2 (version: v2.11.0)
+ Type $help to view available commands.
+ <1> => getenv("MY_ENV_VAR")
+ "icinga2"
+
### dirname <a id="global-functions-dirname"></a>
Signature:
int main(int argc, char **argv)
{
#ifndef _WIN32
- if (!getenv("ICINGA2_KEEP_FDS")) {
+ String keepFDs = Utility::GetFromEnvironment("ICINGA2_KEEP_FDS");
+ if (keepFDs.IsEmpty()) {
rlimit rl;
if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
rlim_t maxfds = rl.rlim_max;
}
if (!foundSlash) {
- const char *pathEnv = getenv("PATH");
- if (pathEnv) {
+ String pathEnv = Utility::GetFromEnvironment("PATH");
+ if (!pathEnv.IsEmpty()) {
std::vector<String> paths = String(pathEnv).Split(":");
bool foundPath = false;
REGISTER_SAFE_FUNCTION(System, get_time, &Utility::GetTime, "");
REGISTER_SAFE_FUNCTION(System, basename, &Utility::BaseName, "path");
REGISTER_SAFE_FUNCTION(System, dirname, &Utility::DirName, "path");
+REGISTER_SAFE_FUNCTION(System, getenv, &ScriptUtils::GetEnv, "value");
REGISTER_SAFE_FUNCTION(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim, "component");
REGISTER_SAFE_FUNCTION(System, track_parents, &ScriptUtils::TrackParents, "child");
REGISTER_SAFE_FUNCTION(System, escape_shell_cmd, &Utility::EscapeShellCmd, "cmd");
return Array::FromVector(paths);
}
+
+String ScriptUtils::GetEnv(const String& key)
+{
+ return Utility::GetFromEnvironment(key);
+}
static double Ptr(const Object::Ptr& object);
static Value Glob(const std::vector<Value>& args);
static Value GlobRecursive(const std::vector<Value>& args);
+ static String GetEnv(const String& key);
private:
ScriptUtils();
#include <ios>
#include <fstream>
#include <iostream>
+#include <stdlib.h>
#include <future>
#ifdef __FreeBSD__
#endif /* _WIN32 */
+/**
+ * Retrieve the environment variable value by given key.
+ *
+ * @param env Environment variable name.
+ */
+
String Utility::GetFromEnvironment(const String& env)
{
-#ifndef _WIN32
const char *envValue = getenv(env.CStr());
+
if (envValue == NULL)
return String();
else
return String(envValue);
-#else /* _WIN32 */
- // While getenv exists on Windows, we don't set them. Therefore there is no reason to read them.
- return String();
-#endif /* _WIN32 */
}
static String DirName(const String& path);
static String BaseName(const String& path);
+ static String GetEnv(const String& key);
+
static void NullDeleter(void *);
static double GetTime();
<< "Type $help to view available commands.\n";
- const char *addrEnv = getenv("ICINGA2_API_URL");
- if (addrEnv)
+ String addrEnv = Utility::GetFromEnvironment("ICINGA2_API_URL");
+ if (!addrEnv.IsEmpty())
addr = addrEnv;
if (vm.count("connect"))
int next_line = 1;
#ifdef HAVE_EDITLINE
- char *homeEnv = getenv("HOME");
+ String homeEnv = Utility::GetFromEnvironment("HOME");
String historyPath;
std::fstream historyfp;
- if (homeEnv) {
+ if (!homeEnv.IsEmpty()) {
historyPath = String(homeEnv) + "/.icinga2_history";
historyfp.open(historyPath.CStr(), std::fstream::in);
return EXIT_FAILURE;
}
- const char *usernameEnv = getenv("ICINGA2_API_USERNAME");
- const char *passwordEnv = getenv("ICINGA2_API_PASSWORD");
+ String usernameEnv = Utility::GetFromEnvironment("ICINGA2_API_USERNAME");
+ String passwordEnv = Utility::GetFromEnvironment("ICINGA2_API_PASSWORD");
- if (usernameEnv)
+ if (!usernameEnv.IsEmpty())
url->SetUsername(usernameEnv);
- if (passwordEnv)
+ if (!passwordEnv.IsEmpty())
url->SetPassword(passwordEnv);
if (url->GetPort().IsEmpty())