Skip to content

Commit 9335c43

Browse files
committed
refactor(cli/self-update): move self_update() to SelfUpdateMode::update()
1 parent e8309b9 commit 9335c43

File tree

2 files changed

+52
-54
lines changed

2 files changed

+52
-54
lines changed

src/cli/rustup_mode.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,14 @@ async fn update(
10081008
cfg.set_default(Some(&desc.into()))?;
10091009
}
10101010
}
1011-
exit_code &=
1012-
self_update::self_update(self_update_mode, should_self_update, &dl_cfg).await?;
1011+
exit_code &= self_update_mode.update(should_self_update, &dl_cfg).await?;
10131012
} else if ensure_active_toolchain {
10141013
let (toolchain, source) = cfg.ensure_active_toolchain(force_non_host, true).await?;
10151014
info!("the active toolchain `{toolchain}` has been installed");
10161015
info!("it's active because: {}", source.to_reason());
10171016
} else {
10181017
exit_code &= common::update_all_channels(cfg, opts.force).await?;
1019-
exit_code &=
1020-
self_update::self_update(self_update_mode, should_self_update, &dl_cfg).await?;
1018+
exit_code &= self_update_mode.update(should_self_update, &dl_cfg).await?;
10211019

10221020
info!("cleaning up downloads & tmp directories");
10231021
utils::delete_dir_contents_following_links(&cfg.download_dir);

src/cli/self_update.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,56 @@ impl SelfUpdateMode {
280280
Self::CheckOnly => "check-only",
281281
}
282282
}
283+
284+
/// Optionally performs a self-update: check policy, download, apply and exit.
285+
///
286+
/// Whether the self-update is executed is based on both compile-time and runtime
287+
/// configurations, where the priority is as follows:
288+
/// no-self-update feature > self update mode > CLI flag
289+
///
290+
/// i.e. update only if rustup does **not** have the no-self-update feature,
291+
/// and self update mode is configured to **enable**
292+
/// and has **no** `--no-self-update` CLI flag.
293+
pub(crate) async fn update(
294+
&self,
295+
should_self_update: bool,
296+
dl_cfg: &DownloadCfg<'_>,
297+
) -> Result<ExitCode> {
298+
if cfg!(feature = "no-self-update") {
299+
info!("self-update is disabled for this build of rustup");
300+
info!("any updates to rustup will need to be fetched with your system package manager");
301+
return Ok(ExitCode(0));
302+
}
303+
match self {
304+
Self::Enable if should_self_update => (),
305+
Self::CheckOnly => {
306+
check_rustup_update(dl_cfg).await?;
307+
return Ok(ExitCode(0));
308+
}
309+
_ => return Ok(ExitCode(0)),
310+
}
311+
312+
match self_update_permitted(false)? {
313+
SelfUpdatePermission::HardFail => {
314+
error!("Unable to self-update. STOP");
315+
return Ok(ExitCode(1));
316+
}
317+
#[cfg(not(windows))]
318+
SelfUpdatePermission::Skip => return Ok(ExitCode(0)),
319+
SelfUpdatePermission::Permit => {}
320+
}
321+
322+
let setup_path = prepare_update(dl_cfg).await?;
323+
324+
if let Some(setup_path) = &setup_path {
325+
return run_update(setup_path);
326+
} else {
327+
// Try again in case we emitted "tool `{}` is already installed" last time.
328+
install_proxies(dl_cfg.process)?;
329+
}
330+
331+
Ok(ExitCode(0))
332+
}
283333
}
284334

285335
impl ValueEnum for SelfUpdateMode {
@@ -1125,56 +1175,6 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
11251175
Ok(SelfUpdatePermission::Permit)
11261176
}
11271177

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-
1156-
match self_update_permitted(false)? {
1157-
SelfUpdatePermission::HardFail => {
1158-
error!("Unable to self-update. STOP");
1159-
return Ok(ExitCode(1));
1160-
}
1161-
#[cfg(not(windows))]
1162-
SelfUpdatePermission::Skip => return Ok(ExitCode(0)),
1163-
SelfUpdatePermission::Permit => {}
1164-
}
1165-
1166-
let setup_path = prepare_update(dl_cfg).await?;
1167-
1168-
if let Some(setup_path) = &setup_path {
1169-
return run_update(setup_path);
1170-
} else {
1171-
// Try again in case we emitted "tool `{}` is already installed" last time.
1172-
install_proxies(dl_cfg.process)?;
1173-
}
1174-
1175-
Ok(ExitCode(0))
1176-
}
1177-
11781178
/// Self update downloads rustup-init to `CARGO_HOME`/bin/rustup-init
11791179
/// and runs it.
11801180
///

0 commit comments

Comments
 (0)