Skip to content

Commit

Permalink
refactor: swhkd should never read environment
Browse files Browse the repository at this point in the history
  • Loading branch information
manthanabc committed Jan 9, 2025
1 parent 75bcd8d commit d0597f2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 12 deletions.
7 changes: 3 additions & 4 deletions swhkd/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
// Without this request, the environmental variables responsible for the reading for the config
// file will not be available.
// Thus, it is important to wait for the server to start before proceeding.
let mut env = environ::Env::construct(None);
let mut env_hash = 0;
let env;
let mut env_hash;
loop {
match refresh_env(invoking_uid, 0) {
Ok((Some(new_env), hash)) => {
env_hash = hash;
env = new_env;
break;
}
Ok((None, hash)) => {
env_hash = hash;
Ok((None, _)) => {
log::debug!("Waiting for env...");
continue;
}
Expand Down
10 changes: 2 additions & 8 deletions swhkd/src/environ.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use std::{collections::HashMap, error::Error, path::PathBuf, process::Command};
use std::{collections::HashMap, path::PathBuf};

#[derive(Debug, Clone)]
pub struct Env {
pub pairs: HashMap<String, String>,
}

impl Env {
fn get_env() -> Result<String, Box<dyn Error>> {
let cmd = Command::new("env").output()?;
let stdout = String::from_utf8(cmd.stdout)?;
Ok(stdout)
}

fn parse_env(env: &str) -> HashMap<String, String> {
let mut pairs = HashMap::new();
for line in env.lines() {
Expand All @@ -27,7 +21,7 @@ impl Env {
pub fn construct(env: Option<&str>) -> Self {
let env = match env {
Some(env) => env.to_string(),
None => Self::get_env().unwrap(),
None => "".to_string(),
};
let pairs = Self::parse_env(&env);
Self { pairs }
Expand Down

0 comments on commit d0597f2

Please sign in to comment.