Skip to content

Commit e8309b9

Browse files
committed
refactor(cli/rustup-mode): pass self-update predicates into self_update()
1 parent 6cab968 commit e8309b9

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

src/cli/rustup_mode.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -946,13 +946,7 @@ async fn update(
946946

947947
common::warn_if_host_is_emulated(cfg.process);
948948
let self_update_mode = SelfUpdateMode::from_cfg(cfg)?;
949-
// Priority: no-self-update feature > self_update_mode > no-self-update args.
950-
// Update only if rustup does **not** have the no-self-update feature,
951-
// and auto-self-update is configured to **enable**
952-
// and has **no** no-self-update parameter.
953-
let self_update = !cfg!(feature = "no-self-update")
954-
&& self_update_mode == SelfUpdateMode::Enable
955-
&& !opts.no_self_update;
949+
let should_self_update = !opts.no_self_update;
956950
let force_non_host = opts.force_non_host;
957951
cfg.profile_override = opts.profile;
958952

@@ -1014,33 +1008,22 @@ async fn update(
10141008
cfg.set_default(Some(&desc.into()))?;
10151009
}
10161010
}
1017-
if self_update {
1018-
exit_code &= self_update::self_update(&dl_cfg).await?;
1019-
}
1011+
exit_code &=
1012+
self_update::self_update(self_update_mode, should_self_update, &dl_cfg).await?;
10201013
} else if ensure_active_toolchain {
10211014
let (toolchain, source) = cfg.ensure_active_toolchain(force_non_host, true).await?;
10221015
info!("the active toolchain `{toolchain}` has been installed");
10231016
info!("it's active because: {}", source.to_reason());
10241017
} else {
10251018
exit_code &= common::update_all_channels(cfg, opts.force).await?;
1026-
if self_update {
1027-
exit_code &= self_update::self_update(&dl_cfg).await?;
1028-
}
1019+
exit_code &=
1020+
self_update::self_update(self_update_mode, should_self_update, &dl_cfg).await?;
10291021

10301022
info!("cleaning up downloads & tmp directories");
10311023
utils::delete_dir_contents_following_links(&cfg.download_dir);
10321024
cfg.tmp_cx.clean();
10331025
}
10341026

1035-
if !cfg!(feature = "no-self-update") && self_update_mode == SelfUpdateMode::CheckOnly {
1036-
check_rustup_update(&dl_cfg).await?;
1037-
}
1038-
1039-
if cfg!(feature = "no-self-update") {
1040-
info!("self-update is disabled for this build of rustup");
1041-
info!("any updates to rustup will need to be fetched with your system package manager")
1042-
}
1043-
10441027
Ok(exit_code)
10451028
}
10461029

src/cli/self_update.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,8 +1125,34 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
11251125
Ok(SelfUpdatePermission::Permit)
11261126
}
11271127

1128-
/// Performs all of a self-update: check policy, download, apply and exit.
1129-
pub(crate) async fn self_update(dl_cfg: &DownloadCfg<'_>) -> Result<ExitCode> {
1128+
/// Optionally performs a self-update: check policy, download, apply and exit.
1129+
///
1130+
/// Whether the self-update is executed is based on both compile-time and runtime
1131+
/// configurations, where the priority is as follows:
1132+
/// no-self-update feature > self update mode > CLI flag
1133+
///
1134+
/// i.e. update only if rustup does **not** have the no-self-update feature,
1135+
/// and self update mode is configured to **enable**
1136+
/// and has **no** `--no-self-update` CLI flag.
1137+
pub(crate) async fn self_update(
1138+
mode: SelfUpdateMode,
1139+
should_self_update: bool,
1140+
dl_cfg: &DownloadCfg<'_>,
1141+
) -> Result<ExitCode> {
1142+
if cfg!(feature = "no-self-update") {
1143+
info!("self-update is disabled for this build of rustup");
1144+
info!("any updates to rustup will need to be fetched with your system package manager");
1145+
return Ok(ExitCode(0));
1146+
}
1147+
match mode {
1148+
SelfUpdateMode::Enable if should_self_update => (),
1149+
SelfUpdateMode::CheckOnly => {
1150+
check_rustup_update(dl_cfg).await?;
1151+
return Ok(ExitCode(0));
1152+
}
1153+
_ => return Ok(ExitCode(0)),
1154+
}
1155+
11301156
match self_update_permitted(false)? {
11311157
SelfUpdatePermission::HardFail => {
11321158
error!("Unable to self-update. STOP");

0 commit comments

Comments
 (0)