fix(config): Expand all environment variables and file references (#724)

This commit is contained in:
NBonaparte 2017-09-04 14:00:35 -07:00 committed by GitHub
parent e381d76908
commit e329a8150a
3 changed files with 19 additions and 5 deletions

View File

@ -338,6 +338,7 @@ class config {
fallback = var.substr(pos + 1); fallback = var.substr(pos + 1);
var.erase(pos); var.erase(pos);
} }
var = file_util::expand(var);
if (file_util::exists(var)) { if (file_util::exists(var)) {
m_log.info("File reference \"%s\" found", var); m_log.info("File reference \"%s\" found", var);

View File

@ -73,7 +73,8 @@ if(BUILD_IPC_MSG)
SOURCES SOURCES
ipc.cpp ipc.cpp
utils/env.cpp utils/env.cpp
utils/file.cpp) utils/file.cpp
utils/string.cpp)
target_compile_options(polybar-msg PUBLIC $<$<CXX_COMPILER_ID:GNU>:$<$<CONFIG:MinSizeRel>:-flto>>) target_compile_options(polybar-msg PUBLIC $<$<CXX_COMPILER_ID:GNU>:$<$<CONFIG:MinSizeRel>:-flto>>)
endif() endif()

View File

@ -9,6 +9,7 @@
#include "errors.hpp" #include "errors.hpp"
#include "utils/env.hpp" #include "utils/env.hpp"
#include "utils/file.hpp" #include "utils/file.hpp"
#include "utils/string.hpp"
POLYBAR_NS POLYBAR_NS
@ -236,11 +237,22 @@ namespace file_util {
* Path expansion * Path expansion
*/ */
const string expand(const string& path) { const string expand(const string& path) {
string p{path}; string ret;
if (p[0] == '~') { vector<string> p_exploded = string_util::split(path, '/');
p.replace(0, 1, env_util::get("HOME")); for (auto& section : p_exploded) {
switch(section[0]) {
case '$':
section = env_util::get(section.substr(1));
break;
case '~':
section = env_util::get("HOME");
break;
} }
return p; }
ret = string_util::join(p_exploded, "/");
if (ret[0] != '/')
ret.insert(0, 1, '/');
return ret;
} }
} }