fix(config): Perform tilde expansion on include-file #603

This commit is contained in:
Michael Carlberg 2017-06-02 18:29:55 +02:00
parent 28431be67e
commit d3b0670f30
5 changed files with 42 additions and 7 deletions

View File

@ -106,6 +106,7 @@ namespace file_util {
string contents(const string& filename);
bool is_fifo(const string& filename);
vector<string> glob(string pattern);
const string expand(const string& path);
template <typename... Args>
decltype(auto) make_file_descriptor(Args&&... args) {

View File

@ -100,15 +100,16 @@ void config::parse_file() {
}
if (key == "include-file") {
if (value.empty() || !file_util::exists(value)) {
throw value_error("Invalid include file \"" + value + "\" defined on line " + to_string(lineno));
auto file_path = file_util::expand(value);
if (file_path.empty() || !file_util::exists(file_path)) {
throw value_error("Invalid include file \"" + file_path + "\" defined on line " + to_string(lineno));
}
if (std::find(files.begin(), files.end(), value) != files.end()) {
throw value_error("Recursive include file \"" + value + "\"");
if (std::find(files.begin(), files.end(), file_path) != files.end()) {
throw value_error("Recursive include file \"" + file_path + "\"");
}
files.push_back(value);
m_log.trace("config: Including file \"%s\"", value);
for (auto&& l : string_util::split(file_util::contents(value), '\n')) {
files.push_back(file_util::expand(file_path));
m_log.trace("config: Including file \"%s\"", file_path);
for (auto&& l : string_util::split(file_util::contents(file_path), '\n')) {
pushline(lineno, forward<string>(l));
}
files.pop_back();

View File

@ -231,6 +231,17 @@ namespace file_util {
return ret;
}
/**
* Path expansion
*/
const string expand(const string& path) {
string p{path};
if (p[0] == '~') {
p.replace(0, 1, env_util::get("HOME"));
}
return p;
}
}
POLYBAR_NS_END

View File

@ -18,6 +18,7 @@ unit_test(utils/color)
unit_test(utils/math)
unit_test(utils/memory)
unit_test(utils/string)
unit_test(utils/file)
unit_test(components/command_line)
# XXX: Requires mocked xcb connection

View File

@ -0,0 +1,21 @@
#include <iomanip>
#include <iostream>
#include "components/logger.cpp"
#include "utils/command.cpp"
#include "utils/concurrency.cpp"
#include "utils/env.cpp"
#include "utils/file.cpp"
#include "utils/io.cpp"
#include "utils/process.cpp"
#include "utils/string.cpp"
int main() {
using namespace polybar;
"expand"_test = [] {
auto cmd = command_util::make_command("echo $HOME");
cmd->exec();
cmd->tail([](string home) { expect(file_util::expand("~/test") == home + "/test"); });
};
}