Skip to content

Commit 370cd17

Browse files
committed
Pass InstallOpts around directly
1 parent 368f133 commit 370cd17

File tree

1 file changed

+41
-58
lines changed

1 file changed

+41
-58
lines changed

src/cli/self_update.rs

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ pub(crate) async fn install(
462462
}
463463
}
464464

465+
let no_modify_path = opts.no_modify_path;
465466
let install_res = move || async move {
466467
install_bins()?;
467468

@@ -483,19 +484,7 @@ pub(crate) async fn install(
483484
fs::create_dir_all(home).context("unable to create ~/.rustup")?;
484485
}
485486

486-
maybe_install_rust(
487-
opts.default_toolchain,
488-
&opts.profile,
489-
opts.default_host_triple.as_deref(),
490-
!opts.no_update_toolchain,
491-
opts.components,
492-
opts.targets,
493-
current_dir,
494-
verbose,
495-
quiet,
496-
)
497-
.await?;
498-
487+
maybe_install_rust(current_dir, verbose, quiet, opts).await?;
499488
Ok(utils::ExitCode(0))
500489
};
501490

@@ -518,7 +507,7 @@ pub(crate) async fn install(
518507
#[cfg(windows)]
519508
let cargo_home = cargo_home.replace('\\', r"\\");
520509
#[cfg(windows)]
521-
let msg = if opts.no_modify_path {
510+
let msg = if no_modify_path {
522511
format!(
523512
post_install_msg_win_no_modify_path!(),
524513
cargo_home = cargo_home
@@ -527,7 +516,7 @@ pub(crate) async fn install(
527516
format!(post_install_msg_win!(), cargo_home = cargo_home)
528517
};
529518
#[cfg(not(windows))]
530-
let msg = if opts.no_modify_path {
519+
let msg = if no_modify_path {
531520
format!(
532521
post_install_msg_unix_no_modify_path!(),
533522
cargo_home = cargo_home
@@ -856,27 +845,15 @@ pub(crate) fn install_proxies() -> Result<()> {
856845
}
857846

858847
async fn maybe_install_rust(
859-
toolchain: Option<MaybeOfficialToolchainName>,
860-
profile_str: &str,
861-
default_host_triple: Option<&str>,
862-
update_existing_toolchain: bool,
863-
components: &[&str],
864-
targets: &[&str],
865848
current_dir: PathBuf,
866849
verbose: bool,
867850
quiet: bool,
851+
opts: InstallOpts<'_>,
868852
) -> Result<()> {
869853
let mut cfg = common::set_globals(current_dir, verbose, quiet)?;
870854

871-
let toolchain = _install_selection(
872-
&mut cfg,
873-
toolchain,
874-
profile_str,
875-
default_host_triple,
876-
update_existing_toolchain,
877-
components,
878-
targets,
879-
)?;
855+
let (components, targets) = (opts.components, opts.targets);
856+
let toolchain = _install_selection(opts, &mut cfg)?;
880857
if let Some(ref desc) = toolchain {
881858
let status = if Toolchain::exists(&cfg, &desc.into())? {
882859
warn!("Updating existing toolchain, profile choice will be ignored");
@@ -909,37 +886,39 @@ async fn maybe_install_rust(
909886
Ok(())
910887
}
911888

912-
fn _install_selection(
913-
cfg: &mut Cfg,
914-
toolchain_opt: Option<MaybeOfficialToolchainName>,
915-
profile_str: &str,
916-
default_host_triple: Option<&str>,
917-
update_existing_toolchain: bool,
918-
components: &[&str],
919-
targets: &[&str],
920-
) -> Result<Option<ToolchainDesc>> {
921-
cfg.set_profile(profile_str)?;
922-
923-
if let Some(default_host_triple) = default_host_triple {
889+
fn _install_selection(opts: InstallOpts<'_>, cfg: &mut Cfg) -> Result<Option<ToolchainDesc>> {
890+
let InstallOpts {
891+
default_host_triple,
892+
default_toolchain,
893+
profile,
894+
no_modify_path: _no_modify_path,
895+
no_update_toolchain,
896+
components,
897+
targets,
898+
} = opts;
899+
900+
cfg.set_profile(&profile)?;
901+
902+
if let Some(default_host_triple) = &default_host_triple {
924903
// Set host triple now as it will affect resolution of toolchain_str
925904
info!("setting default host triple to {}", default_host_triple);
926905
cfg.set_default_host_triple(default_host_triple.to_owned())?;
927906
} else {
928907
info!("default host triple is {}", cfg.get_default_host_triple()?);
929908
}
930909

931-
let user_specified_something = toolchain_opt.is_some()
910+
let user_specified_something = default_toolchain.is_some()
932911
|| !targets.is_empty()
933912
|| !components.is_empty()
934-
|| update_existing_toolchain;
913+
|| !no_update_toolchain;
935914

936915
// If the user specified they want no toolchain, we skip this, otherwise
937916
// if they specify something directly, or we have no default, then we install
938917
// a toolchain (updating if it's already present) and then if neither of
939918
// those are true, we have a user who doesn't mind, and already has an
940919
// install, so we leave their setup alone.
941920
Ok(
942-
if matches!(toolchain_opt, Some(MaybeOfficialToolchainName::None)) {
921+
if matches!(default_toolchain, Some(MaybeOfficialToolchainName::None)) {
943922
info!("skipping toolchain installation");
944923
if !components.is_empty() {
945924
warn!(
@@ -958,9 +937,9 @@ fn _install_selection(
958937
writeln!(process().stdout().lock())?;
959938
None
960939
} else if user_specified_something
961-
|| (update_existing_toolchain && cfg.find_default()?.is_none())
940+
|| (!no_update_toolchain && cfg.find_default()?.is_none())
962941
{
963-
match toolchain_opt {
942+
match default_toolchain {
964943
Some(s) => {
965944
let toolchain_name = match s {
966945
MaybeOfficialToolchainName::None => unreachable!(),
@@ -1350,6 +1329,7 @@ mod tests {
13501329
use rustup_macros::unit_test as test;
13511330

13521331
use crate::cli::common;
1332+
use crate::cli::self_update::InstallOpts;
13531333
use crate::dist::dist::PartialToolchainDesc;
13541334
use crate::test::{test_dir, with_rustup_home, Env};
13551335
use crate::{currentprocess, for_host};
@@ -1363,6 +1343,17 @@ mod tests {
13631343
vars,
13641344
..Default::default()
13651345
};
1346+
1347+
let opts = InstallOpts {
1348+
default_host_triple: None,
1349+
default_toolchain: None, // No toolchain specified
1350+
profile: "default".to_owned(), // default profile
1351+
no_modify_path: false,
1352+
components: &[],
1353+
targets: &[],
1354+
no_update_toolchain: false,
1355+
};
1356+
13661357
currentprocess::with(tp.clone().into(), || -> Result<()> {
13671358
// TODO: we could pass in a custom cfg to get notification
13681359
// callbacks rather than output to the tp sink.
@@ -1373,17 +1364,9 @@ mod tests {
13731364
.unwrap()
13741365
.resolve(&cfg.get_default_host_triple().unwrap())
13751366
.unwrap(),
1376-
super::_install_selection(
1377-
&mut cfg,
1378-
None, // No toolchain specified
1379-
"default", // default profile
1380-
None,
1381-
true,
1382-
&[],
1383-
&[],
1384-
)
1385-
.unwrap() // result
1386-
.unwrap() // option
1367+
super::_install_selection(opts, &mut cfg)
1368+
.unwrap() // result
1369+
.unwrap() // option
13871370
);
13881371
Ok(())
13891372
})?;

0 commit comments

Comments
 (0)