Skip to content

Commit

Permalink
refactor: improve cross-platform support and env var handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen1 committed Jan 17, 2025
1 parent 08f614a commit c406f2e
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 642 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# stop-nagging

stop-nagging is a Rust-based CLI tool that silences or disables upgrade/advertising nags and other unnecessary warnings from various CLI tools and development tools.
`stop-nagging` is a Rust-based CLI tool that silences or disables upgrade/advertising nags and other unnecessary warnings from various CLI tools and development tools. It also disables telemetry and other tracking mechanisms.

It uses a YAML file (`tools.yaml`) to list each tool's name, environment variables, and commands to run, making it easy for new contributors to update the logic without writing Rust code.

## Features
Expand Down
70 changes: 46 additions & 24 deletions src/runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use crate::errors::StopNaggingError;
use crate::yaml_config::YamlToolsConfig;
use std::collections::HashMap;
use std::{collections::HashSet, env, process::Command};

struct EnvVarBackup {
key: String,
original_value: Option<String>,
}

pub fn disable_nags(
yaml_config: &YamlToolsConfig,
selected_ecosystems: &[String],
Expand All @@ -15,6 +21,8 @@ pub fn disable_nags(

let run_all_ecosystems = selected_ecosystems.is_empty();

let mut env_backups: Vec<EnvVarBackup> = Vec::new();

for (ecosystem_name, ecosystem_config) in &yaml_config.ecosystems {
let ecosystem_name_lower = ecosystem_name.to_lowercase();

Expand Down Expand Up @@ -45,18 +53,17 @@ pub fn disable_nags(

if let Some(env_vars) = &tool.env {
for (key, val) in env_vars {
if env::var_os(key).is_some() {
eprintln!(
"Warning: Env var '{}' is already set; skipping override for tool '{}'.",
key, tool.name
);
} else {
env::set_var(key, val);
println!(
"Set {}={} for tool {} in {}",
key, val, tool.name, ecosystem_name
);
}
let original = env::var(key).ok();
env_backups.push(EnvVarBackup {
key: key.clone(),
original_value: original,
});

env::set_var(key, val);
println!(
"Set {}={} for tool {} in {}",
key, val, tool.name, ecosystem_name
);
}
}

Expand All @@ -75,26 +82,41 @@ pub fn disable_nags(
}
}
}

// Restore environment variables
for backup in env_backups {
match backup.original_value {
Some(val) => env::set_var(&backup.key, val),
None => env::remove_var(&backup.key),
}
}
}

pub fn check_tool_executable(executable: &str) -> Result<(), String> {
let which = Command::new("which").arg(executable).output();
match which {
Ok(output) => {
if !output.status.success() {
return Err(format!("Executable '{}' not found in PATH", executable));
}
}
Err(e) => {
return Err(format!("Error running 'which {}': {}", executable, e));
}
#[cfg(windows)]
let (cmd, arg) = ("where", executable);
#[cfg(not(windows))]
let (cmd, arg) = ("which", executable);

let output = Command::new(cmd)
.arg(arg)
.output()
.map_err(|e| format!("Error running '{}': {}", cmd, e))?;

if !output.status.success() {
return Err(format!("Executable '{}' not found in PATH", executable));
}
Ok(())
}

pub fn run_shell_command(cmd_str: &str) -> Result<(), StopNaggingError> {
let status = Command::new("sh")
.arg("-c")
#[cfg(windows)]
let (shell, shell_arg) = ("cmd", "/C");
#[cfg(not(windows))]
let (shell, shell_arg) = ("sh", "-c");

let status = Command::new(shell)
.arg(shell_arg)
.arg(cmd_str)
.status()
.map_err(|e| StopNaggingError::Command(e.to_string()))?;
Expand Down
Loading

0 comments on commit c406f2e

Please sign in to comment.