Skip to content

Commit 204c8a9

Browse files
committed
Simplify process access to environment variables
1 parent 70420fa commit 204c8a9

File tree

22 files changed

+47
-101
lines changed

22 files changed

+47
-101
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustup::cli::rustup_mode;
2727
#[cfg(windows)]
2828
use rustup::cli::self_update;
2929
use rustup::cli::setup_mode;
30-
use rustup::currentprocess::{process, varsource::VarSource, with_runtime, OSProcess};
30+
use rustup::currentprocess::{process, with_runtime, OSProcess};
3131
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
3232
use rustup::is_proxyable_tools;
3333
use rustup::utils::utils::{self, ExitCode};

src/cli/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use once_cell::sync::Lazy;
1414

1515
use super::self_update;
1616
use crate::cli::download_tracker::DownloadTracker;
17-
use crate::currentprocess::{process, terminalsource, varsource::VarSource};
17+
use crate::currentprocess::{process, terminalsource};
1818
use crate::dist::dist::{TargetTriple, ToolchainDesc};
1919
use crate::dist::manifest::ComponentStatus;
2020
use crate::install::UpdateStatus;

src/cli/log.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22
use std::io::Write;
33

4-
use crate::currentprocess::{process, terminalsource, varsource::VarSource};
4+
use crate::currentprocess::{process, terminalsource};
55

66
macro_rules! warn {
77
( $ ( $ arg : tt ) * ) => ( $crate::cli::log::warn_fmt ( format_args ! ( $ ( $ arg ) * ) ) )

src/cli/rustup_mode.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,8 +1488,6 @@ async fn man(
14881488
command: &str,
14891489
toolchain: Option<PartialToolchainDesc>,
14901490
) -> Result<utils::ExitCode> {
1491-
use crate::currentprocess::varsource::VarSource;
1492-
14931491
let toolchain = Toolchain::from_partial(toolchain, cfg).await?;
14941492
let mut path = toolchain.path().to_path_buf();
14951493
path.push("share");

src/cli/self_update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::{
6666
markdown::md,
6767
},
6868
config::Cfg,
69-
currentprocess::{process, varsource::VarSource},
69+
currentprocess::process,
7070
dist::dist::{self, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc},
7171
install::UpdateStatus,
7272
toolchain::{

src/cli/self_update/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::path::PathBuf;
2929
use anyhow::{bail, Result};
3030

3131
use super::utils;
32-
use crate::currentprocess::{process, varsource::VarSource};
32+
use crate::currentprocess::process;
3333

3434
pub(crate) type Shell = Box<dyn UnixShell>;
3535

src/cli/self_update/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use anyhow::{bail, Context, Result};
55

66
use super::install_bins;
77
use super::shell;
8-
use crate::currentprocess::{process, varsource::VarSource};
8+
use crate::currentprocess::process;
99
use crate::utils::utils;
1010
use crate::utils::Notification;
1111

src/cli/self_update/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::super::errors::*;
1212
use super::common;
1313
use super::{install_bins, InstallOpts};
1414
use crate::cli::download_tracker::DownloadTracker;
15-
use crate::currentprocess::{process, varsource::VarSource};
15+
use crate::currentprocess::process;
1616
use crate::dist::dist::TargetTriple;
1717
use crate::utils::utils;
1818
use crate::utils::Notification;

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio_stream::StreamExt;
1313

1414
use crate::{
1515
cli::self_update::SelfUpdateMode,
16-
currentprocess::{process, varsource::VarSource},
16+
currentprocess::process,
1717
dist::{
1818
dist::{self, PartialToolchainDesc, Profile, ToolchainDesc},
1919
download::DownloadCfg,

src/currentprocess.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ pub mod cwdsource;
2323
pub mod filesource;
2424
mod homethunk;
2525
pub mod terminalsource;
26-
pub mod varsource;
2726

2827
use cwdsource::*;
29-
use varsource::*;
3028

3129
/// An abstraction for the current process.
3230
///
@@ -63,11 +61,11 @@ use varsource::*;
6361
/// methods are in performance critical loops (except perhaps progress bars -
6462
/// and even there we should be doing debouncing and managing update rates).
6563
#[enum_dispatch]
66-
pub trait CurrentProcess: CurrentDirSource + VarSource + Debug {}
64+
pub trait CurrentProcess: CurrentDirSource + Debug {}
6765

6866
/// Allows concrete types for the currentprocess abstraction.
6967
#[derive(Clone, Debug)]
70-
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource)]
68+
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
7169
pub enum Process {
7270
OSProcess(OSProcess),
7371
#[cfg(feature = "test")]
@@ -88,6 +86,25 @@ impl Process {
8886
.map(String::from)
8987
}
9088

89+
pub fn var(&self, key: &str) -> Result<String, env::VarError> {
90+
match self {
91+
Process::OSProcess(_) => env::var(key),
92+
#[cfg(feature = "test")]
93+
Process::TestProcess(p) => match p.vars.get(key) {
94+
Some(val) => Ok(val.to_owned()),
95+
None => Err(env::VarError::NotPresent),
96+
},
97+
}
98+
}
99+
100+
pub(crate) fn var_os(&self, key: &str) -> Option<OsString> {
101+
match self {
102+
Process::OSProcess(_) => env::var_os(key),
103+
#[cfg(feature = "test")]
104+
Process::TestProcess(p) => p.vars.get(key).map(OsString::from),
105+
}
106+
}
107+
91108
pub(crate) fn args(&self) -> Box<dyn Iterator<Item = String> + '_> {
92109
match self {
93110
Process::OSProcess(_) => Box::new(env::args()),

0 commit comments

Comments
 (0)