Skip to content

Commit

Permalink
support runtime env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
dan committed Sep 25, 2023
1 parent 5916875 commit 6a96517
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`_


Expand Down
12 changes: 12 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use dotenv::dotenv;
use std::env;

pub fn get_blacklisted_displays() -> Vec<String> {
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()
}
33 changes: 25 additions & 8 deletions src/install.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use super::env;
use std::io::Write;
use std::process::Command;
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#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
Expand All @@ -22,16 +30,18 @@ pub fn run(path_to_binary: &str) -> std::io::Result<()> {
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>~/Library/logs/{}.log</string>
<string>{}</string>
<key>StandardErrorPath</key>
<string>~/Library/logs/{}.error.log</string>
<string>{}</string>
<key>EnvironmentVariables</key>
<dict>
<key>BLACKLIST_DISPLAYS</key>
<string>{}</string>
</dict>
</dict>
</plist>
"#,
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
Expand All @@ -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()
)
}
15 changes: 2 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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| {
Expand All @@ -84,16 +83,6 @@ fn is_blacklisted_display_connected() -> bool {
is_blacklisted_display_connected
}

fn get_blacklisted_displays() -> Vec<String> {
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()
Expand Down

0 comments on commit 6a96517

Please sign in to comment.