commit 65aa8374f3cbc3b37cb7086c7a57b281380b4cf8
parent c0ade447983937d6027ba2813a7a78aa4d9c7171
Author: deurzen <max@deurzen.net>
Date: Wed, 1 Jun 2022 12:17:30 +0200
implements user-supplied env var parsing
Diffstat:
5 files changed, 38 insertions(+), 4 deletions(-)
diff --git a/include/kranewl/conf/options.hh b/include/kranewl/conf/options.hh
@@ -6,13 +6,16 @@
struct Options final {
Options(
std::string const&& config_path_,
+ std::string const&& env_path_,
std::optional<std::string> autostart_path_
)
: config_path(config_path_),
+ env_path(env_path_),
autostart_path(autostart_path_)
{}
std::string config_path;
+ std::string env_path;
std::optional<std::string> autostart_path;
};
diff --git a/include/kranewl/model.hh b/include/kranewl/model.hh
@@ -34,7 +34,7 @@ class Config;
class Model final
{
public:
- Model(Config const&, std::optional<std::string>);
+ Model(Config const&, std::string const&, std::optional<std::string>);
~Model();
void register_server(Server_ptr);
diff --git a/src/kranewl/conf/options.cc b/src/kranewl/conf/options.cc
@@ -13,10 +13,13 @@ extern "C" {
static const std::string CONFIG_FILE = "kranewlrc.lua";
static const std::string DEFAULT_CONFIG = "/etc/kranewl/" + CONFIG_FILE;
+static const std::string ENV_FILE = "env";
+static const std::string DEFAULT_ENV = "/etc/kranewl/" + ENV_FILE;
static const std::string USAGE = "usage: kranewl [...options]\n\n"
"options: \n"
" -a <autostart_file> Path to an executable autostart file.\n"
" -c <config_file> Path to a configuration file.\n"
+ " -e <env_file> Path to file with environment variables.\n"
" -v Prints the version.\n"
" -h Prints this message.";
@@ -56,6 +59,20 @@ resolve_config_path(std::string& path) noexcept
return DEFAULT_CONFIG;
}
+static std::string const&
+resolve_env_path(std::string& path) noexcept
+{
+ if (!path.empty())
+ if (assert_permissions(path, R_OK))
+ return path;
+
+ path.assign(default_user_path(ENV_FILE));
+ if (assert_permissions(path, R_OK))
+ return path;
+
+ return DEFAULT_ENV;
+}
+
static std::optional<std::string>
resolve_autostart_path(std::string& path) noexcept
{
@@ -75,10 +92,10 @@ resolve_autostart_path(std::string& path) noexcept
Options
parse_options(int argc, char** argv) noexcept
{
- std::string autostart_path, config_path;
+ std::string autostart_path, config_path, env_path;
int opt;
- while ((opt = getopt(argc, argv, "vha:c:")) != -1) {
+ while ((opt = getopt(argc, argv, "vha:c:e:")) != -1) {
switch (opt) {
case 'a':
autostart_path = optarg;
@@ -88,6 +105,10 @@ parse_options(int argc, char** argv) noexcept
config_path = optarg;
break;
+ case 'e':
+ env_path = optarg;
+ break;
+
case 'v':
std::cout << VERSION << std::endl << std::flush;
std::exit(EXIT_SUCCESS);
@@ -104,6 +125,7 @@ parse_options(int argc, char** argv) noexcept
return Options(
std::move(resolve_config_path(config_path)),
+ std::move(resolve_env_path(env_path)),
resolve_autostart_path(autostart_path)
);
}
diff --git a/src/kranewl/main.cc b/src/kranewl/main.cc
@@ -35,7 +35,12 @@ main(int argc, char** argv)
const ConfigParser config_parser{options.config_path};
const Config config = config_parser.generate_config();
- Model model{config, options.autostart_path};
+ Model model{
+ config,
+ options.env_path,
+ options.autostart_path
+ };
+
Server{&model}.run();
return EXIT_SUCCESS;
diff --git a/src/kranewl/model.cc b/src/kranewl/model.cc
@@ -6,6 +6,7 @@
#include <kranewl/conf/config.hh>
#include <kranewl/context.hh>
#include <kranewl/cycle.t.hh>
+#include <kranewl/env.hh>
#include <kranewl/exec.hh>
#include <kranewl/input/cursor-bindings.hh>
#include <kranewl/input/cursor.hh>
@@ -37,6 +38,7 @@ extern "C" {
Model::Model(
Config const& config,
+ std::string const& env_path,
[[maybe_unused]] std::optional<std::string> autostart_path
)
: m_config{config},
@@ -61,6 +63,8 @@ Model::Model(
{
TRACE();
+ parse_and_set_env_vars(env_path);
+
#ifdef NDEBUG
if (autostart_path) {
spdlog::info("Executing autostart file at {}", *autostart_path);