mirror of
https://github.com/imgurbot12/rmenu.git
synced 2024-11-10 11:33:48 +01:00
feat: use env-vars to preserve configuration between multi-stage cmds
This commit is contained in:
parent
024bc46d30
commit
9d292c3360
@ -10,7 +10,7 @@ use rmenu_plugin::{Entry, Message};
|
|||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use crate::config::{cfg_replace, Config, Keybind};
|
use crate::config::{cfg_replace, Config, Keybind};
|
||||||
use crate::{DEFAULT_CONFIG, DEFAULT_THEME, XDG_PREFIX};
|
use crate::{DEFAULT_CONFIG, DEFAULT_THEME, ENV_ACTIVE_PLUGINS, XDG_PREFIX};
|
||||||
|
|
||||||
/// Allowed Formats for Entry Ingestion
|
/// Allowed Formats for Entry Ingestion
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
@ -405,4 +405,39 @@ impl Args {
|
|||||||
entries.extend(self.load_plugins(config)?);
|
entries.extend(self.load_plugins(config)?);
|
||||||
Ok(entries)
|
Ok(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure Environment Variables for Multi-Stage Execution
|
||||||
|
pub fn set_env(&self) {
|
||||||
|
let mut running = self.run.join(",");
|
||||||
|
if let Ok(already_running) = std::env::var(ENV_ACTIVE_PLUGINS) {
|
||||||
|
running = format!("{running},{already_running}");
|
||||||
|
}
|
||||||
|
std::env::set_var(ENV_ACTIVE_PLUGINS, running);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load Settings from Environment Variables for Multi-Stage Execution
|
||||||
|
pub fn load_env(&mut self, config: &mut Config) -> Result<()> {
|
||||||
|
let env_plugins = std::env::var(ENV_ACTIVE_PLUGINS).unwrap_or_default();
|
||||||
|
let active_plugins: Vec<&str> = env_plugins
|
||||||
|
.split(",")
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.collect();
|
||||||
|
for name in active_plugins {
|
||||||
|
// retrieve plugin configuration
|
||||||
|
log::info!("reloading plugin configuration for {name:?}");
|
||||||
|
let plugin = config
|
||||||
|
.plugins
|
||||||
|
.get(name)
|
||||||
|
.cloned()
|
||||||
|
.ok_or_else(|| RMenuError::NoSuchPlugin(name.to_owned()))?;
|
||||||
|
// update config w/ plugin options when available
|
||||||
|
if let Some(options) = plugin.options.as_ref() {
|
||||||
|
config
|
||||||
|
.update(options)
|
||||||
|
.map_err(|e| RMenuError::InvalidKeybind(e))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,9 @@ static DEFAULT_CONFIG: &'static str = "config.yaml";
|
|||||||
static XDG_PREFIX: &'static str = "rmenu";
|
static XDG_PREFIX: &'static str = "rmenu";
|
||||||
static DEFAULT_CSS_CONTENT: &'static str = include_str!("../public/default.css");
|
static DEFAULT_CSS_CONTENT: &'static str = include_str!("../public/default.css");
|
||||||
|
|
||||||
|
static ENV_BIN: &'static str = "RMENU";
|
||||||
|
static ENV_ACTIVE_PLUGINS: &'static str = "RMENU_ACTIVE_PLUGINS";
|
||||||
|
|
||||||
/// Application State for GUI
|
/// Application State for GUI
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
@ -33,7 +36,7 @@ pub struct App {
|
|||||||
fn main() -> cli::Result<()> {
|
fn main() -> cli::Result<()> {
|
||||||
// export self to environment for other scripts
|
// export self to environment for other scripts
|
||||||
let exe = self_exe();
|
let exe = self_exe();
|
||||||
std::env::set_var("RMENU", exe);
|
std::env::set_var(ENV_BIN, exe);
|
||||||
|
|
||||||
// enable log and set default level
|
// enable log and set default level
|
||||||
if std::env::var("RUST_LOG").is_err() {
|
if std::env::var("RUST_LOG").is_err() {
|
||||||
@ -54,9 +57,16 @@ fn main() -> cli::Result<()> {
|
|||||||
.any(|e| e.icon.is_some() || e.icon_alt.is_some());
|
.any(|e| e.icon.is_some() || e.icon_alt.is_some());
|
||||||
config.use_comments = config.use_comments && entries.iter().any(|e| e.comment.is_some());
|
config.use_comments = config.use_comments && entries.iter().any(|e| e.comment.is_some());
|
||||||
|
|
||||||
|
// load additional configuration settings from env
|
||||||
|
cli.load_env(&mut config)?;
|
||||||
|
|
||||||
|
// configure css theme and css overrides
|
||||||
let theme = cli.get_theme();
|
let theme = cli.get_theme();
|
||||||
let css = cli.get_css(&config);
|
let css = cli.get_css(&config);
|
||||||
|
|
||||||
|
// set environment variables before running app
|
||||||
|
cli.set_env();
|
||||||
|
|
||||||
// genrate app context and run gui
|
// genrate app context and run gui
|
||||||
gui::run(App {
|
gui::run(App {
|
||||||
name: "rmenu".to_owned(),
|
name: "rmenu".to_owned(),
|
||||||
|
Loading…
Reference in New Issue
Block a user