set_env_perm/
lib.rs

1//! This crate allows you to permanently set environment variables
2//!
3//! # Examples
4//! ```rust
5//! // Check if DUMMY is set, if not set it to 1
6//! // export DUMMY=1
7//! set_env_perm::check_or_set("DUMMY", 1).expect("Failed to find or set DUMMY");
8//! // Append $HOME/some/cool/bin to $PATH
9//! // export PATH= "$HOME/some/cool/bin:$PATH"
10//! set_env_perm::append("PATH", "$HOME/some/cool/bin").expect("Couldn't find PATH");
11//! // Sets a variable without checking if it exists.
12//! // Note you need to use a raw string literal to include ""
13//! // export DUMMY="/something"
14//! set_env_perm::set("DUMMY", r#""/something""#).expect("Failed to set DUMMY");
15//! ```
16
17#[cfg(target_family = "unix")]
18use dirs;
19#[cfg(target_family = "unix")]
20use std::fs::{File, OpenOptions};
21#[cfg(target_family = "unix")]
22use std::io::Write;
23#[cfg(target_family = "unix")]
24use std::path::PathBuf;
25
26use std::env;
27use std::env::VarError;
28use std::fmt;
29use std::io;
30
31#[cfg(target_family = "windows")]
32pub fn do_prerequisites() {
33    use std::fs;
34
35    let path = dirs::document_dir();
36    if let Some(path) = path {
37        let pkg_version = env!("CARGO_PKG_VERSION");
38
39        let template = include_str!("../scripts/profile.ps1").replace("${VER}", pkg_version);
40        let path = path.join("WindowsPowerShell");
41        if !path.exists() {
42            fs::create_dir_all(&path).unwrap();
43        }
44        let path = path.join("Profile.ps1");
45        if !path.exists() {
46            fs::write(&path, template).unwrap();
47        } else {
48            let prefix = "# ----------------------------------VER";
49            let content = fs::read_to_string(&path).unwrap();
50
51            let mut lines = content.lines();
52
53            let pos = lines.position(|it| it.starts_with(prefix));
54
55            if let Some(pos) = pos {
56                let content = lines.nth(pos).unwrap().replace(prefix, "");
57                if content != pkg_version {
58                    fs::write(&path, template).unwrap();
59                }
60            } else {
61                fs::write(&path, template).unwrap();
62            }
63        }
64        return;
65    }
66
67    eprintln!("document path is not exists");
68    std::process::exit(1);
69}
70
71#[cfg(target_os = "windows")]
72pub fn inject(it: &str) -> io::Result<()> {
73    use std::fs;
74
75    do_prerequisites();
76
77    let profile_path = dirs::document_dir()
78        .unwrap()
79        .join("WindowsPowerShell/Profile.ps1");
80
81    let content = fs::read_to_string(&profile_path)?;
82    let mut content_parts: Vec<&str> = content.split("\r\n").collect();
83
84    let idx = content_parts
85        .iter()
86        .position(|it| it == &"# ----------------------------------SET_ENV_DEFS_END")
87        .unwrap();
88    content_parts.insert(idx, it);
89
90    fs::write(profile_path, content_parts.join("\r\n"))
91}
92
93/// Checks if a environment variable is set.
94/// If it is then nothing will happen.
95/// If it's not then it will be added
96/// to your profile.
97pub fn check_or_set<T, U>(var: T, value: U) -> io::Result<()>
98where
99    T: fmt::Display + AsRef<std::ffi::OsStr>,
100    U: fmt::Display,
101{
102    env::var(&var).map(|_| ()).or_else(|_| set(var, value))
103}
104
105pub fn get<T: fmt::Display>(var: T) -> io::Result<String> {
106    env::var(var.to_string()).map_err(|err| match err {
107        VarError::NotPresent => io::Error::new(io::ErrorKind::NotFound, "Variable not present."),
108        VarError::NotUnicode(_) => {
109            io::Error::new(io::ErrorKind::Unsupported, "Encoding not supported.")
110        }
111    })
112}
113
114/// Appends a value to an environment variable
115/// Useful for appending a value to PATH
116#[cfg(target_family = "unix")]
117pub fn append<T: fmt::Display>(var: T, value: T) -> io::Result<()> {
118    let mut profile = get_profile()?;
119    writeln!(profile, "\nexport {}=\"{}:${}\"", var, value, var)?;
120    profile.flush()
121}
122/// Appends a value to an environment variable
123/// Useful for appending a value to PATH
124#[cfg(target_os = "windows")]
125pub fn append<T: fmt::Display>(var: T, value: T) -> io::Result<()> {
126    inject(format!("setenv_append {} {}", var, value).as_str())
127}
128
129/// Prepends a value to an environment variable
130/// Useful for prepending a value to PATH
131#[cfg(target_family = "unix")]
132pub fn prepend<T: fmt::Display>(var: T, value: T) -> io::Result<()> {
133    let mut profile = get_profile()?;
134    writeln!(profile, "\nexport {}=\"${}:{}\"", var, value, var)?;
135    profile.flush()
136}
137
138/// Prepends a value to an environment variable
139/// Useful for prepending a value to PATH
140#[cfg(target_os = "windows")]
141pub fn prepend<T: fmt::Display>(var: T, value: T) -> io::Result<()> {
142    inject(format!("setenv_prepend {} {}", var, value).as_str())
143}
144
145/// Sets an environment variable without checking
146/// if it exists.
147/// If it does you will end up with two
148/// assignments in your profile.
149/// It's recommended to use `check_or_set`
150/// unless you are certain it doesn't exist.
151#[cfg(target_family = "unix")]
152pub fn set<T: fmt::Display, U: fmt::Display>(var: T, value: U) -> io::Result<()> {
153    let mut profile = get_profile()?;
154    writeln!(profile, "\nexport {}={}", var, value)?;
155    profile.flush()
156}
157/// Sets an environment variable without checking
158/// if it exists.
159/// If it does you will override the value.
160#[cfg(target_os = "windows")]
161pub fn set<T: fmt::Display, U: fmt::Display>(var: T, value: U) -> io::Result<()> {
162    inject(format!("setenv_set {} {}", var, value).as_str())?;
163    Ok(())
164}
165
166#[cfg(target_family = "unix")]
167fn get_profile() -> io::Result<File> {
168    let home_dir = dirs::home_dir()
169        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "No home directory"))?;
170
171    let profile_path = find_profile(home_dir.clone()).unwrap_or_else(|_| {
172        let mut fallback = home_dir;
173        fallback.push(".profile");
174        fallback
175    });
176
177    let mut oo = OpenOptions::new();
178    oo.append(true).create(true);
179    oo.open(profile_path)
180}
181
182#[cfg(target_family = "unix")]
183struct Shell {
184    name: &'static str,
185    config_files: &'static [&'static str],
186}
187
188#[cfg(target_family = "unix")]
189static SHELLS: &[Shell] = &[
190    Shell {
191        name: "zsh",
192        config_files: &[".zshenv", ".zprofile", ".zshrc", ".zlogin"],
193    },
194    Shell {
195        name: "fish",
196        config_files: &[".config/fish/config.fish"],
197    },
198    Shell {
199        name: "tcsh",
200        config_files: &[".tcshrc", ".cshrc", ".login"],
201    },
202    Shell {
203        name: "csh",
204        config_files: &[".cshrc", ".tcshrc", ".login"],
205    },
206    Shell {
207        name: "ksh",
208        config_files: &[".kshrc"],
209    },
210    Shell {
211        name: "bash",
212        config_files: &[".bash_profile", ".bashrc", ".bash_login"],
213    },
214];
215
216#[cfg(target_family = "unix")]
217fn find_profile(mut home_dir: PathBuf) -> io::Result<PathBuf> {
218    let shell_env = env::var("SHELL").unwrap_or_default();
219
220    let selected_shell = SHELLS
221        .iter()
222        .find(|s| shell_env.contains(s.name))
223        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Unsupported shell"))?;
224
225    for config_file in selected_shell.config_files {
226        let mut config_path = home_dir.clone();
227        for part in config_file.split('/') {
228            config_path.push(part);
229        }
230
231        if config_path.exists() {
232            return Ok(config_path);
233        }
234
235        if config_file.contains('/') {
236            if std::fs::create_dir_all(config_path.parent().unwrap()).is_ok() {
237                return Ok(config_path);
238            }
239            return Err(io::Error::new(
240                io::ErrorKind::Other,
241                "Cannot create config directory",
242            ));
243        }
244    }
245
246    home_dir.push(selected_shell.config_files[0]);
247    Ok(home_dir)
248}