Skip to content

Commit e8c5089

Browse files
committed
Obtain Process handle via constructor method
1 parent 5c98694 commit e8c5089

26 files changed

+220
-196
lines changed

doc/dev-guide/src/coding-standards.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Coding standards
32

43
Generally we just follow good sensible Rust practices, clippy and so forth.
@@ -25,7 +24,7 @@ though that is helpful.
2524
The `rustup::currentprocess` module abstracts the global state that is
2625
`std::env::args`, `std::env::vars`, `std::io::std*`, `std::process::id`,
2726
`std::env::current_dir` and `std::process::exit` permitting threaded tests of
28-
the CLI logic; use `process()` rather than those APIs directly.
27+
the CLI logic; use `Process::get()` rather than those APIs directly.
2928

3029
## Clippy lints
3130

@@ -58,7 +57,7 @@ task, but not currently run per-platform, which means there is no way to find
5857
out the status of clippy per platform without running it on that platform as a
5958
developer.
6059

61-
### import rustup-macros::{integration,unit}_test into test modules
60+
### import rustup-macros::{integration,unit}\_test into test modules
6261

6362
These test helpers add pre-and-post logic to tests to enable the use of tracing
6463
inside tests, which can be helpful for tracking down behaviours in larger tests.

src/bin/rustup-init.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustup::cli::self_update;
2828
use rustup::cli::setup_mode;
2929
use rustup::env_var::RUST_RECURSION_COUNT_MAX;
3030
use rustup::is_proxyable_tools;
31-
use rustup::process::{process, with, Process};
31+
use rustup::process::{with, Process};
3232
use rustup::utils::utils;
3333

3434
fn main() {
@@ -105,11 +105,11 @@ fn maybe_trace_rustup() -> Result<utils::ExitCode> {
105105

106106
#[cfg_attr(feature = "otel", tracing::instrument)]
107107
fn run_rustup() -> Result<utils::ExitCode> {
108-
if let Ok(dir) = process().var("RUSTUP_TRACE_DIR") {
108+
if let Ok(dir) = Process::get().var("RUSTUP_TRACE_DIR") {
109109
open_trace_file!(dir)?;
110110
}
111111
let result = run_rustup_inner();
112-
if process().var("RUSTUP_TRACE_DIR").is_ok() {
112+
if Process::get().var("RUSTUP_TRACE_DIR").is_ok() {
113113
close_trace_file!();
114114
}
115115
result
@@ -126,7 +126,7 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
126126
utils::current_dir()?;
127127
utils::current_exe()?;
128128

129-
match process().name().as_deref() {
129+
match Process::get().name().as_deref() {
130130
Some("rustup") => rustup_mode::main(),
131131
Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => {
132132
// NB: The above check is only for the prefix of the file
@@ -158,7 +158,7 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
158158
}
159159

160160
fn do_recursion_guard() -> Result<()> {
161-
let recursion_count = process()
161+
let recursion_count = Process::get()
162162
.var("RUST_RECURSION_COUNT")
163163
.ok()
164164
.and_then(|s| s.parse().ok())

src/cli/common.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ use crate::utils::utils;
2323
use crate::{
2424
dist::notifications as dist_notifications, toolchain::distributable::DistributableToolchain,
2525
};
26-
use crate::{process, toolchain::toolchain::Toolchain};
26+
use crate::{process::Process, toolchain::toolchain::Toolchain};
2727
use crate::{Cfg, Notification};
2828

2929
pub(crate) const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language";
3030

3131
pub(crate) fn confirm(question: &str, default: bool) -> Result<bool> {
32-
write!(process().stdout().lock(), "{question} ")?;
32+
write!(Process::get().stdout().lock(), "{question} ")?;
3333
let _ = std::io::stdout().flush();
3434
let input = read_line()?;
3535

@@ -40,7 +40,7 @@ pub(crate) fn confirm(question: &str, default: bool) -> Result<bool> {
4040
_ => false,
4141
};
4242

43-
writeln!(process().stdout().lock())?;
43+
writeln!(Process::get().stdout().lock())?;
4444

4545
Ok(r)
4646
}
@@ -52,15 +52,15 @@ pub(crate) enum Confirm {
5252
}
5353

5454
pub(crate) fn confirm_advanced(customized_install: bool) -> Result<Confirm> {
55-
writeln!(process().stdout().lock())?;
55+
writeln!(Process::get().stdout().lock())?;
5656
let first_option = match customized_install {
5757
true => "1) Proceed with selected options (default - just press enter)",
5858
false => "1) Proceed with standard installation (default - just press enter)",
5959
};
60-
writeln!(process().stdout().lock(), "{first_option}")?;
61-
writeln!(process().stdout().lock(), "2) Customize installation")?;
62-
writeln!(process().stdout().lock(), "3) Cancel installation")?;
63-
write!(process().stdout().lock(), ">")?;
60+
writeln!(Process::get().stdout().lock(), "{first_option}")?;
61+
writeln!(Process::get().stdout().lock(), "2) Customize installation")?;
62+
writeln!(Process::get().stdout().lock(), "3) Cancel installation")?;
63+
write!(Process::get().stdout().lock(), ">")?;
6464

6565
let _ = std::io::stdout().flush();
6666
let input = read_line()?;
@@ -71,17 +71,17 @@ pub(crate) fn confirm_advanced(customized_install: bool) -> Result<Confirm> {
7171
_ => Confirm::No,
7272
};
7373

74-
writeln!(process().stdout().lock())?;
74+
writeln!(Process::get().stdout().lock())?;
7575

7676
Ok(r)
7777
}
7878

7979
pub(crate) fn question_str(question: &str, default: &str) -> Result<String> {
80-
writeln!(process().stdout().lock(), "{question} [{default}]")?;
80+
writeln!(Process::get().stdout().lock(), "{question} [{default}]")?;
8181
let _ = std::io::stdout().flush();
8282
let input = read_line()?;
8383

84-
writeln!(process().stdout().lock())?;
84+
writeln!(Process::get().stdout().lock())?;
8585

8686
if input.is_empty() {
8787
Ok(default.to_string())
@@ -92,12 +92,12 @@ pub(crate) fn question_str(question: &str, default: &str) -> Result<String> {
9292

9393
pub(crate) fn question_bool(question: &str, default: bool) -> Result<bool> {
9494
let default_text = if default { "(Y/n)" } else { "(y/N)" };
95-
writeln!(process().stdout().lock(), "{question} {default_text}")?;
95+
writeln!(Process::get().stdout().lock(), "{question} {default_text}")?;
9696

9797
let _ = std::io::stdout().flush();
9898
let input = read_line()?;
9999

100-
writeln!(process().stdout().lock())?;
100+
writeln!(Process::get().stdout().lock())?;
101101

102102
if input.is_empty() {
103103
Ok(default)
@@ -111,7 +111,7 @@ pub(crate) fn question_bool(question: &str, default: bool) -> Result<bool> {
111111
}
112112

113113
pub(crate) fn read_line() -> Result<String> {
114-
let stdin = process().stdin();
114+
let stdin = Process::get().stdin();
115115
let stdin = stdin.lock();
116116
let mut lines = stdin.lines();
117117
let lines = lines.next().transpose()?;
@@ -250,7 +250,7 @@ fn show_channel_updates(
250250
Ok((pkg, banner, width, color, version, previous_version))
251251
});
252252

253-
let mut t = process().stdout().terminal();
253+
let mut t = Process::get().stdout().terminal();
254254

255255
let data: Vec<_> = data.collect::<Result<_>>()?;
256256
let max_width = data
@@ -291,7 +291,7 @@ pub(crate) fn update_all_channels(
291291

292292
let show_channel_updates = || {
293293
if !toolchains.is_empty() {
294-
writeln!(process().stdout().lock())?;
294+
writeln!(Process::get().stdout().lock())?;
295295

296296
let t = toolchains
297297
.into_iter()
@@ -371,7 +371,7 @@ where
371371
}
372372

373373
pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<utils::ExitCode> {
374-
let mut t = process().stdout().terminal();
374+
let mut t = Process::get().stdout().terminal();
375375
let manifestation = distributable.get_manifestation()?;
376376
let config = manifestation.read_config()?.unwrap_or_default();
377377
let manifest = distributable.get_manifest()?;
@@ -399,7 +399,7 @@ pub(crate) fn list_targets(distributable: DistributableToolchain<'_>) -> Result<
399399
pub(crate) fn list_installed_targets(
400400
distributable: DistributableToolchain<'_>,
401401
) -> Result<utils::ExitCode> {
402-
let t = process().stdout();
402+
let t = Process::get().stdout();
403403
let manifestation = distributable.get_manifestation()?;
404404
let config = manifestation.read_config()?.unwrap_or_default();
405405
let manifest = distributable.get_manifest()?;
@@ -422,7 +422,7 @@ pub(crate) fn list_installed_targets(
422422
pub(crate) fn list_components(
423423
distributable: DistributableToolchain<'_>,
424424
) -> Result<utils::ExitCode> {
425-
let mut t = process().stdout().terminal();
425+
let mut t = Process::get().stdout().terminal();
426426

427427
let manifestation = distributable.get_manifestation()?;
428428
let config = manifestation.read_config()?.unwrap_or_default();
@@ -443,7 +443,7 @@ pub(crate) fn list_components(
443443
}
444444

445445
pub(crate) fn list_installed_components(distributable: DistributableToolchain<'_>) -> Result<()> {
446-
let t = process().stdout();
446+
let t = Process::get().stdout();
447447
for component in distributable.components()? {
448448
if component.installed {
449449
writeln!(t.lock(), "{}", component.name)?;
@@ -471,7 +471,7 @@ fn print_toolchain_path(
471471
String::new()
472472
};
473473
writeln!(
474-
process().stdout().lock(),
474+
Process::get().stdout().lock(),
475475
"{}{}{}{}",
476476
&toolchain,
477477
if_default,
@@ -489,7 +489,7 @@ pub(crate) fn list_toolchains(cfg: &Cfg, verbose: bool) -> Result<utils::ExitCod
489489
.map(Into::into)
490490
.collect::<Vec<_>>();
491491
if toolchains.is_empty() {
492-
writeln!(process().stdout().lock(), "no installed toolchains")?;
492+
writeln!(Process::get().stdout().lock(), "no installed toolchains")?;
493493
} else {
494494
let def_toolchain_name = cfg.get_default()?.map(|t| (&t).into());
495495
let cwd = utils::current_dir()?;
@@ -527,7 +527,7 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
527527
let overrides = cfg.settings_file.with(|s| Ok(s.overrides.clone()))?;
528528

529529
if overrides.is_empty() {
530-
writeln!(process().stdout().lock(), "no overrides")?;
530+
writeln!(Process::get().stdout().lock(), "no overrides")?;
531531
} else {
532532
let mut any_not_exist = false;
533533
for (k, v) in overrides {
@@ -536,15 +536,15 @@ pub(crate) fn list_overrides(cfg: &Cfg) -> Result<utils::ExitCode> {
536536
any_not_exist = true;
537537
}
538538
writeln!(
539-
process().stdout().lock(),
539+
Process::get().stdout().lock(),
540540
"{:<40}\t{:<20}",
541541
utils::format_path_for_display(&k)
542542
+ if dir_exists { "" } else { " (not a directory)" },
543543
v
544544
)?
545545
}
546546
if any_not_exist {
547-
writeln!(process().stdout().lock())?;
547+
writeln!(Process::get().stdout().lock())?;
548548
info!(
549549
"you may remove overrides for non-existent directories with
550550
`rustup override unset --nonexistent`"
@@ -567,51 +567,51 @@ pub(crate) fn version() -> &'static str {
567567
pub(crate) fn dump_testament() -> Result<utils::ExitCode> {
568568
use git_testament::GitModification::*;
569569
writeln!(
570-
process().stdout().lock(),
570+
Process::get().stdout().lock(),
571571
"Rustup version renders as: {}",
572572
version()
573573
)?;
574574
writeln!(
575-
process().stdout().lock(),
575+
Process::get().stdout().lock(),
576576
"Current crate version: {}",
577577
env!("CARGO_PKG_VERSION")
578578
)?;
579579
if TESTAMENT.branch_name.is_some() {
580580
writeln!(
581-
process().stdout().lock(),
581+
Process::get().stdout().lock(),
582582
"Built from branch: {}",
583583
TESTAMENT.branch_name.unwrap()
584584
)?;
585585
} else {
586-
writeln!(process().stdout().lock(), "Branch information missing")?;
586+
writeln!(Process::get().stdout().lock(), "Branch information missing")?;
587587
}
588588
writeln!(
589-
process().stdout().lock(),
589+
Process::get().stdout().lock(),
590590
"Commit info: {}",
591591
TESTAMENT.commit
592592
)?;
593593
if TESTAMENT.modifications.is_empty() {
594-
writeln!(process().stdout().lock(), "Working tree is clean")?;
594+
writeln!(Process::get().stdout().lock(), "Working tree is clean")?;
595595
} else {
596596
for fmod in TESTAMENT.modifications {
597597
match fmod {
598598
Added(f) => writeln!(
599-
process().stdout().lock(),
599+
Process::get().stdout().lock(),
600600
"Added: {}",
601601
String::from_utf8_lossy(f)
602602
)?,
603603
Removed(f) => writeln!(
604-
process().stdout().lock(),
604+
Process::get().stdout().lock(),
605605
"Removed: {}",
606606
String::from_utf8_lossy(f)
607607
)?,
608608
Modified(f) => writeln!(
609-
process().stdout().lock(),
609+
Process::get().stdout().lock(),
610610
"Modified: {}",
611611
String::from_utf8_lossy(f)
612612
)?,
613613
Untracked(f) => writeln!(
614-
process().stdout().lock(),
614+
Process::get().stdout().lock(),
615615
"Untracked: {}",
616616
String::from_utf8_lossy(f)
617617
)?,
@@ -622,15 +622,15 @@ pub(crate) fn dump_testament() -> Result<utils::ExitCode> {
622622
}
623623

624624
fn show_backtrace() -> bool {
625-
if let Ok(true) = process().var("RUSTUP_NO_BACKTRACE").map(|s| s == "1") {
625+
if let Ok(true) = Process::get().var("RUSTUP_NO_BACKTRACE").map(|s| s == "1") {
626626
return false;
627627
}
628628

629-
if let Ok(true) = process().var("RUST_BACKTRACE").map(|s| s == "1") {
629+
if let Ok(true) = Process::get().var("RUST_BACKTRACE").map(|s| s == "1") {
630630
return true;
631631
}
632632

633-
for arg in process().args() {
633+
for arg in Process::get().args() {
634634
if arg == "-v" || arg == "--verbose" {
635635
return true;
636636
}

src/cli/download_tracker.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::{Arc, Mutex};
55
use std::time::{Duration, Instant};
66

77
use crate::dist::Notification as In;
8-
use crate::process::{process, terminalsource};
8+
use crate::process::{Process, terminalsource};
99
use crate::utils::units::{Size, Unit, UnitMode};
1010
use crate::utils::Notification as Un;
1111
use crate::Notification;
@@ -58,7 +58,7 @@ impl DownloadTracker {
5858
downloaded_last_few_secs: VecDeque::with_capacity(DOWNLOAD_TRACK_COUNT),
5959
start_sec: None,
6060
last_sec: None,
61-
term: process().stdout().terminal(),
61+
term: Process::get().stdout().terminal(),
6262
displayed_charcount: None,
6363
units: vec![Unit::B],
6464
display_progress,
@@ -73,7 +73,7 @@ impl DownloadTracker {
7373
true
7474
}
7575
Notification::Install(In::Utils(Un::DownloadDataReceived(data))) => {
76-
if process().stdout().is_a_tty() {
76+
if Process::get().stdout().is_a_tty() {
7777
self.data_received(data.len());
7878
}
7979
true

0 commit comments

Comments
 (0)