util.{hh,cc}: Split out environment-variables.{hh,cc}

Change-Id: Icff0aa33fda5147bd5dbe256a0b9d6a6c8a2c3f6
This commit is contained in:
Tom Hubrecht 2024-05-28 10:50:54 +02:00
parent 0b91a4b0ec
commit 6fd6795bc4
15 changed files with 107 additions and 67 deletions

View file

@ -1,5 +1,6 @@
#include "util.hh"
#include "editor-for.hh"
#include "environment-variables.hh"
#include "source-path.hh"
namespace nix {

View file

@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "loggers.hh"
#include "progress-bar.hh"
#include "util.hh"

View file

@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "globals.hh"
#include "util.hh"
#include "archive.hh"

View file

@ -1,9 +1,9 @@
#pragma once
///@file
#include "environment-variables.hh"
#include "types.hh"
#include "config.hh"
#include "util.hh"
#include <map>
#include <limits>

View file

@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "ssh.hh"
#include "finally.hh"

View file

@ -2,6 +2,8 @@
#include "args/root.hh"
#include "hash.hh"
#include "json-utils.hh"
#include "environment-variables.hh"
#include "experimental-features-json.hh"
#include <glob.h>

View file

@ -0,0 +1,51 @@
#include <cstring>
#include <map>
#include <optional>
#include <string>
extern char * * environ __attribute__((weak));
namespace nix {
std::optional<std::string> getEnv(const std::string & key)
{
char * value = getenv(key.c_str());
if (!value) return {};
return std::string(value);
}
std::optional<std::string> getEnvNonEmpty(const std::string & key) {
auto value = getEnv(key);
if (value == "") return {};
return value;
}
std::map<std::string, std::string> getEnv()
{
std::map<std::string, std::string> env;
for (size_t i = 0; environ[i]; ++i) {
auto s = environ[i];
auto eq = strchr(s, '=');
if (!eq)
// invalid env, just keep going
continue;
env.emplace(std::string(s, eq), std::string(eq + 1));
}
return env;
}
void clearEnv()
{
for (auto & name : getEnv())
unsetenv(name.first.c_str());
}
void replaceEnv(const std::map<std::string, std::string> & newEnv)
{
clearEnv();
for (auto & newEnvVar : newEnv)
setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1);
}
}

View file

@ -0,0 +1,42 @@
#pragma once
/**
* @file
*
* Utilities for working with the current process's environment
* variables.
*/
#include <map>
#include <optional>
#include <string>
namespace nix {
/**
* @return an environment variable.
*/
std::optional<std::string> getEnv(const std::string & key);
/**
* @return a non empty environment variable. Returns nullopt if the env
* variable is set to ""
*/
std::optional<std::string> getEnvNonEmpty(const std::string & key);
/**
* Get the entire environment.
*/
std::map<std::string, std::string> getEnv();
/**
* Clear the environment.
*/
void clearEnv();
/**
* Replace the entire environment with the given one.
*/
void replaceEnv(const std::map<std::string, std::string> & newEnv);
}

View file

@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "error.hh"
#include "position.hh"

View file

@ -2,6 +2,7 @@
#include <filesystem>
#include <atomic>
#include "environment-variables.hh"
#include "finally.hh"
#include "util.hh"
#include "signals.hh"

View file

@ -1,3 +1,4 @@
#include "environment-variables.hh"
#include "logging.hh"
#include "util.hh"
#include "config.hh"

View file

@ -7,6 +7,7 @@ libutil_sources = files(
'compute-levels.cc',
'config.cc',
'english.cc',
'environment-variables.cc',
'error.cc',
'escape-char.cc',
'escape-string.cc',
@ -53,6 +54,7 @@ libutil_headers = files(
'config-impl.hh',
'config.hh',
'english.hh',
'environment-variables.hh',
'error.hh',
'escape-char.hh',
'escape-string.hh',

View file

@ -4,6 +4,7 @@
#include "serialise.hh"
#include "cgroup.hh"
#include "signals.hh"
#include "environment-variables.hh"
#include <array>
#include <cctype>
@ -43,57 +44,12 @@
#endif
extern char * * environ __attribute__((weak));
#ifdef NDEBUG
#error "Lix may not be built with assertions disabled (i.e. with -DNDEBUG)."
#endif
namespace nix {
std::optional<std::string> getEnv(const std::string & key)
{
char * value = getenv(key.c_str());
if (!value) return {};
return std::string(value);
}
std::optional<std::string> getEnvNonEmpty(const std::string & key) {
auto value = getEnv(key);
if (value == "") return {};
return value;
}
std::map<std::string, std::string> getEnv()
{
std::map<std::string, std::string> env;
for (size_t i = 0; environ[i]; ++i) {
auto s = environ[i];
auto eq = strchr(s, '=');
if (!eq)
// invalid env, just keep going
continue;
env.emplace(std::string(s, eq), std::string(eq + 1));
}
return env;
}
void clearEnv()
{
for (auto & name : getEnv())
unsetenv(name.first.c_str());
}
void replaceEnv(const std::map<std::string, std::string> & newEnv)
{
clearEnv();
for (auto & newEnvVar : newEnv)
setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1);
}
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
{
if (path.empty() || path[0] != '/') {

View file

@ -39,27 +39,6 @@ struct Source;
extern const std::string nativeSystem;
/**
* @return an environment variable.
*/
std::optional<std::string> getEnv(const std::string & key);
/**
* @return a non empty environment variable. Returns nullopt if the env
* variable is set to ""
*/
std::optional<std::string> getEnvNonEmpty(const std::string & key);
/**
* Get the entire environment.
*/
std::map<std::string, std::string> getEnv();
/**
* Clear the environment.
*/
void clearEnv();
/**
* @return An absolutized path, resolving paths relative to the
* specified directory, or the current directory otherwise. The path

View file

@ -1,6 +1,7 @@
#pragma once
///@file
#include "environment-variables.hh"
#include <gtest/gtest.h>
#include <filesystem>