From 6a96517b907619a9da8cea4a0449e8a6c86d8a77 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 25 Sep 2023 22:35:56 +1000 Subject: [PATCH] support runtime env vars --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- src/env.rs | 12 ++++++++++++ src/install.rs | 33 +++++++++++++++++++++++++-------- src/main.rs | 15 ++------------- 6 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 src/env.rs diff --git a/Cargo.lock b/Cargo.lock index aec1de1..cf5c38b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,7 +274,7 @@ dependencies = [ [[package]] name = "monmon" -version = "0.1.1" +version = "0.2.0" dependencies = [ "clap", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index 649e9a4..a23e9e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "monmon" -version = "0.1.1" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 1aa9976..7b9db79 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ You will then be able to run commands from your terminal from any directory, e.g ## Running as Background Service 1. First make sure you have [installed](#installation) the `monmon` binary. 2. Determine the global installation path with `which monmon` -3. Run `monmon install [path-to-binary]` +3. Run `BLACKLIST_DISPLAYS="[comma-separated-displays]" monmon install [path-to-binary]` _Note: the background service can be uninstalled with `monmon uninstall`_ diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..77d2aae --- /dev/null +++ b/src/env.rs @@ -0,0 +1,12 @@ +use dotenv::dotenv; +use std::env; + +pub fn get_blacklisted_displays() -> Vec { + dotenv().ok(); + let blacklist_displays_str = + env::var("BLACKLIST_DISPLAYS").expect("BLACKLIST_DISPLAYS must be set"); + blacklist_displays_str + .split(",") + .map(|s| s.to_string()) + .collect() +} diff --git a/src/install.rs b/src/install.rs index 4499446..a1aee69 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,3 +1,4 @@ +use super::env; use std::io::Write; use std::process::Command; use tempfile::NamedTempFile; @@ -5,6 +6,13 @@ use tempfile::NamedTempFile; static SERVICE_NAME: &str = "com.danitt.monmon"; pub fn run(path_to_binary: &str) -> std::io::Result<()> { + let blacklisted_displays_str = env::get_blacklisted_displays().join(","); + let output_log = format!("{}/Library/logs/{}.log", get_home_dir(), get_plist_name()); + let error_log = format!( + "{}/Library/logs/{}.error.log", + get_home_dir(), + get_plist_name() + ); let plist_content = format!( r#" @@ -22,16 +30,18 @@ pub fn run(path_to_binary: &str) -> std::io::Result<()> { KeepAlive StandardOutPath - ~/Library/logs/{}.log + {} StandardErrorPath - ~/Library/logs/{}.error.log + {} + EnvironmentVariables + + BLACKLIST_DISPLAYS + {} + "#, - SERVICE_NAME, - path_to_binary, - get_plist_name(), - get_plist_name() + SERVICE_NAME, path_to_binary, output_log, error_log, blacklisted_displays_str ); // Write plist to file @@ -55,11 +65,18 @@ pub fn run(path_to_binary: &str) -> std::io::Result<()> { Ok(()) } +fn get_home_dir() -> String { + std::env::var("HOME").expect("Home directory not found") +} + fn get_plist_name() -> String { format!("{}.plist", SERVICE_NAME) } pub fn get_plist_path() -> String { - let home_dir = std::env::var("HOME").expect("Home directory not found"); - format!("{}/Library/LaunchAgents/{}", home_dir, get_plist_name()) + format!( + "{}/Library/LaunchAgents/{}", + get_home_dir(), + get_plist_name() + ) } diff --git a/src/main.rs b/src/main.rs index 9343391..c91e2cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ extern crate dotenv; use clap::{Parser, Subcommand}; -use dotenv::dotenv; +mod env; mod install; mod uninstall; use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher}; -use std::env; use std::path::Path; use std::process::{Command, Stdio}; use std::str; @@ -72,7 +71,7 @@ fn watch() -> notify::Result<()> { } fn is_blacklisted_display_connected() -> bool { - let blacklist_displays = get_blacklisted_displays(); + let blacklist_displays = env::get_blacklisted_displays(); let mut is_blacklisted_display_connected = false; get_connected_displays().iter().for_each(|display| { @@ -84,16 +83,6 @@ fn is_blacklisted_display_connected() -> bool { is_blacklisted_display_connected } -fn get_blacklisted_displays() -> Vec { - dotenv().ok(); - let blacklist_displays_str = - env::var("BLACKLIST_DISPLAYS").expect("BLACKLIST_DISPLAYS must be set"); - blacklist_displays_str - .split(",") - .map(|s| s.to_string()) - .collect() -} - fn move_windows_to_primary_display() { let applescript = r#" on moveAppWindows()