Skip to content

Commit ae7247f

Browse files
committed
Move current process name detection into helper
We're going to reuse it to get a better error message.
1 parent 6c33197 commit ae7247f

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/bin/rustup-init.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
1414
#![recursion_limit = "1024"]
1515

16-
use std::path::PathBuf;
17-
1816
use anyhow::{anyhow, Result};
1917
use cfg_if::cfg_if;
2018
use rs_tracing::*;
@@ -62,18 +60,7 @@ fn run_rustup_inner() -> Result<utils::ExitCode> {
6260
utils::current_dir()?;
6361
utils::current_exe()?;
6462

65-
// The name of arg0 determines how the program is going to behave
66-
let arg0 = match process().var("RUSTUP_FORCE_ARG0") {
67-
Ok(v) => Some(v),
68-
Err(_) => process().args().next(),
69-
}
70-
.map(PathBuf::from);
71-
let name = arg0
72-
.as_ref()
73-
.and_then(|a| a.file_stem())
74-
.and_then(std::ffi::OsStr::to_str);
75-
76-
match name {
63+
match process().name().as_deref() {
7764
Some("rustup") => rustup_mode::main(),
7865
Some(n) if n.starts_with("rustup-setup") || n.starts_with("rustup-init") => {
7966
// NB: The above check is only for the prefix of the file

src/currentprocess.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ pub trait CurrentProcess:
8787
+ Debug
8888
{
8989
fn clone_boxed(&self) -> Box<dyn CurrentProcess>;
90+
91+
fn name(&self) -> Option<String>;
9092
}
9193

9294
// Machinery for Cloning boxes
@@ -107,6 +109,19 @@ where
107109
fn clone_boxed(&self) -> Box<dyn CurrentProcess + 'static> {
108110
Box::new(T::clone(self))
109111
}
112+
113+
fn name(&self) -> Option<String> {
114+
let arg0 = match self.var("RUSTUP_FORCE_ARG0") {
115+
Ok(v) => Some(v),
116+
Err(_) => self.args().next(),
117+
}
118+
.map(PathBuf::from);
119+
120+
arg0.as_ref()
121+
.and_then(|a| a.file_stem())
122+
.and_then(std::ffi::OsStr::to_str)
123+
.map(String::from)
124+
}
110125
}
111126

112127
impl Clone for Box<dyn CurrentProcess + 'static> {

0 commit comments

Comments
 (0)