forked from tauri-apps/cargo-mobile2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.rs
More file actions
71 lines (58 loc) · 1.9 KB
/
env.rs
File metadata and controls
71 lines (58 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::util::cli::{Report, Reportable};
use std::{collections::HashMap, ffi::OsString, fmt::Debug, path::Path};
use thiserror::Error;
pub trait ExplicitEnv: Debug {
fn explicit_env(&self) -> HashMap<String, OsString>;
}
#[derive(Debug, Error)]
pub enum Error {
#[error("The `{0}` environment variable isn't set, which is quite weird")]
NotSet(&'static str),
}
impl Reportable for Error {
fn report(&self) -> Report {
Report::error("Failed to initialize base environment", self)
}
}
#[derive(Clone, Debug)]
pub struct Env {
vars: HashMap<String, std::ffi::OsString>,
}
impl Env {
pub fn new() -> Result<Self, Error> {
let mut vars = HashMap::new();
let home = std::env::var_os("HOME").ok_or(Error::NotSet("HOME"))?;
let path = std::env::var_os("PATH").ok_or(Error::NotSet("PATH"))?;
if let Some(term) = std::env::var_os("TERM") {
vars.insert("TERM".into(), term);
}
if let Some(ssh_auth_sock) = std::env::var_os("SSH_AUTH_SOCK") {
vars.insert("SSH_AUTH_SOCK".into(), ssh_auth_sock);
}
vars.insert("HOME".into(), home);
vars.insert("PATH".into(), path);
Ok(Self { vars })
}
pub fn path(&self) -> &OsString {
self.vars.get("PATH").unwrap()
}
pub fn prepend_to_path(mut self, path: impl AsRef<Path>) -> Self {
let mut path = path.as_ref().as_os_str().to_os_string();
path.push(":");
path.push(self.path().clone());
self.vars.insert("PATH".into(), path);
self
}
pub fn insert_env_var(&mut self, key: String, value: OsString) {
self.vars.insert(key, value);
}
pub fn explicit_env_vars(mut self, vars: HashMap<String, OsString>) -> Self {
self.vars.extend(vars);
self
}
}
impl ExplicitEnv for Env {
fn explicit_env(&self) -> HashMap<String, OsString> {
self.vars.clone()
}
}