Skip to content

Commit 1cc78e0

Browse files
committed
Simplify process access to environment variables
1 parent 926827a commit 1cc78e0

File tree

22 files changed

+39
-99
lines changed

22 files changed

+39
-99
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustup::cli::rustup_mode;
2626
#[cfg(windows)]
2727
use rustup::cli::self_update;
2828
use rustup::cli::setup_mode;
29-
use rustup::currentprocess::{process, varsource::VarSource, with, OSProcess};
29+
use rustup::currentprocess::{process, with, OSProcess};
3030
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
3131
use rustup::is_proxyable_tools;
3232
use rustup::utils::utils;

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::{terminalsource, varsource::VarSource};
17+
use crate::currentprocess::terminalsource;
1818
use crate::dist::dist::{TargetTriple, ToolchainDesc};
1919
use crate::install::UpdateStatus;
2020
use crate::utils::notifications as util_notifications;

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
@@ -1622,8 +1622,6 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
16221622

16231623
#[cfg(not(windows))]
16241624
fn man(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1625-
use crate::currentprocess::varsource::VarSource;
1626-
16271625
let command = m.get_one::<String>("command").unwrap();
16281626

16291627
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;

src/cli/self_update.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ use crate::{
6565
errors::*,
6666
markdown::md,
6767
},
68-
currentprocess::varsource::VarSource,
6968
dist::dist::{self, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc},
7069
install::UpdateStatus,
7170
process,

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::varsource::VarSource, process};
32+
use crate::process;
3333

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

src/cli/self_update/unix.rs

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

66
use super::install_bins;
77
use super::shell;
8-
use crate::currentprocess::varsource::VarSource;
98
use crate::process;
109
use crate::utils::utils;
1110
use crate::utils::Notification;

src/cli/self_update/windows.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ 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::varsource::VarSource;
1615
use crate::dist::dist::TargetTriple;
1716
use crate::process;
1817
use crate::utils::utils;

src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use thiserror::Error as ThisError;
1212

1313
use crate::{
1414
cli::self_update::SelfUpdateMode,
15-
currentprocess::varsource::VarSource,
1615
dist::{
1716
dist::{self, PartialToolchainDesc, Profile, ToolchainDesc},
1817
download::DownloadCfg,

src/currentprocess.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ pub mod cwdsource;
2222
pub mod filesource;
2323
mod homethunk;
2424
pub mod terminalsource;
25-
pub mod varsource;
2625

2726
use cwdsource::*;
28-
use varsource::*;
2927

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

6765
/// Allows concrete types for the currentprocess abstraction.
6866
#[derive(Clone, Debug)]
69-
#[enum_dispatch(CurrentProcess, CurrentDirSource, VarSource)]
67+
#[enum_dispatch(CurrentProcess, CurrentDirSource)]
7068
pub enum Process {
7169
OSProcess(OSProcess),
7270
#[cfg(feature = "test")]
@@ -87,6 +85,25 @@ impl Process {
8785
.map(String::from)
8886
}
8987

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

0 commit comments

Comments
 (0)