feat: Support reading config from non-regular files (#2545)

We had a check that restricted config files to "regular files".
This check was to restrictive as it didn't allow for things like:

```
polybar -c <(gen_config)
gen_config | polybar -c /dev/stdin
```

Now, polybar can easily read config data from stdin.
This commit is contained in:
Patrick Ziegler 2021-10-24 11:25:05 +02:00 committed by GitHub
parent 6d1ff41d37
commit 9e3b537817
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 2 deletions

View File

@ -73,6 +73,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `DEBUG_SHADED` cmake variable and its associated functionality.
### Added
- Polybar can now read config files from stdin: `polybar -c /dev/stdin`.
- `custom/script`: Implement `env-*` config option.
([2090](https://github.com/polybar/polybar/issues/2090))
- `drawtypes/ramp`: Add support for ramp weights.

View File

@ -83,6 +83,7 @@ class fd_stream : public StreamType {
namespace file_util {
bool exists(const string& filename);
bool is_file(const string& filename);
bool is_dir(const string& filename);
string pick(const vector<string>& filenames);
string contents(const string& filename);
void write_contents(const string& filename, const string& contents);

View File

@ -126,8 +126,8 @@ void config_parser::parse_file(const string& file, file_list path) {
throw application_error("Failed to open config file " + file + ": " + strerror(errno));
}
if (!file_util::is_file(file)) {
throw application_error("Config file " + file + " is not a file");
if (file_util::is_dir(file)) {
throw application_error("Config file " + file + " is a directory");
}
m_log.trace("config_parser: Parsing %s", file);

View File

@ -165,6 +165,19 @@ namespace file_util {
return S_ISREG(buffer.st_mode);
}
/**
* Checks if the given path exists and is a file
*/
bool is_dir(const string& filename) {
struct stat buffer {};
if (stat(filename.c_str(), &buffer) != 0) {
return false;
}
return S_ISDIR(buffer.st_mode);
}
/**
* Picks the first existing file out of given entries
*/