Skip to content

Commit 2c7134f

Browse files
committed
address comments
1 parent 3670450 commit 2c7134f

File tree

5 files changed

+76
-29
lines changed

5 files changed

+76
-29
lines changed

doc/src/basics.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ info: downloading self-updates
3030

3131
## Keeping `rustup` up to date
3232

33-
## Keeping rustup up to date
33+
If your `rustup` build with the `no-self-update` feature, it will not be able to update itself.
3434

35-
It is possible to control Rustup's automatic self update mechanism with the `auto-self-update` configuration
36-
variable. This setting supports three values: `enable` and `disable` and `check-only`.
35+
If your `rustup` build without the `no-self-update` feature,
36+
it is possible to control Rustup's automatic self update mechanism with the `auto-self-update` configuration variable.
37+
This setting supports three values: `enable` and `disable` and `check-only`.
3738

3839
* `disable` will ensure that no automatic self updating actions are taken.
3940
* `enable` will mean that `rustup update` and similar commands will also check-for, and install, any update to Rustup.
4041
* `check-only` will cause any automatic self update to check and report on any updates, but not to automatically install them.
4142

42-
Whether automatic self-update is enabled or not, you can request that Rustup check for, and update itself to the
43+
Whether `auto-self-update` is `enable` or not, you can request that Rustup check for, and update itself to the
4344
latest version of `rustup` without updating any installed toolchains by running `rustup
4445
self update`:
4546

@@ -50,7 +51,7 @@ info: downloading self-updates
5051
```
5152

5253
> ### Disable automatic self-updates with the parameter of the command
53-
> If auto-self-update is `enable`, `rustup` will automatically update itself at the end of any
54+
> Since the default value of auto-self-update is `enable`, `rustup` will automatically update itself at the end of any
5455
> toolchain installation as well. You can prevent this automatic behaviour by
5556
> passing the `--no-self-update` argument when running `rustup update` or
5657
> `rustup toolchain install`.

src/cli/rustup_mode.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use super::term2::Terminal;
1515
use super::topical_doc;
1616
use super::{
1717
common,
18-
self_update::{check_rustup_update, SELF_UPDATE_MODES},
18+
self_update::{check_rustup_update, UpdateMode, SELF_UPDATE_MODES},
1919
};
2020
use crate::dist::dist::{
2121
PartialTargetTriple, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc,
@@ -937,7 +937,7 @@ fn update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
937937
// and auto-self-update is configured to **enable**
938938
// and has **no** no-self-update parameter.
939939
let self_update = !self_update::NEVER_SELF_UPDATE
940-
&& self_update_mode == "enable"
940+
&& self_update_mode == UpdateMode::Enable
941941
&& !m.is_present("no-self-update");
942942
let forced = m.is_present("force-non-host");
943943
if let Some(p) = m.value_of("profile") {
@@ -1024,7 +1024,7 @@ fn update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
10241024
cfg.temp_cfg.clean();
10251025
}
10261026

1027-
if !self_update::NEVER_SELF_UPDATE && self_update_mode == "check-only" {
1027+
if !self_update::NEVER_SELF_UPDATE && self_update_mode == UpdateMode::CheckOnly {
10281028
check_rustup_update()?;
10291029
}
10301030

@@ -1587,7 +1587,7 @@ fn set_profile(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
15871587

15881588
fn set_auto_self_update(cfg: &mut Cfg, m: &ArgMatches<'_>) -> Result<utils::ExitCode> {
15891589
if self_update::NEVER_SELF_UPDATE {
1590-
warn!("Your rustup is built with the no-self-update feature, so setting auto-self-update will not have any effect.");
1590+
warn!("{} is built with the no-self-update feature: setting auto-self-update will not have any effect.",cfg.rustup_dir.display());
15911591
}
15921592
cfg.set_auto_self_update(&m.value_of("auto-self-update-mode").unwrap())?;
15931593
Ok(utils::ExitCode(0))

src/cli/self_update.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod os {
4545
}
4646

4747
use std::borrow::Cow;
48+
use std::convert::TryFrom;
4849
use std::env;
4950
use std::env::consts::EXE_SUFFIX;
5051
use std::fs;
@@ -89,6 +90,35 @@ pub const NEVER_SELF_UPDATE: bool = true;
8990
pub const NEVER_SELF_UPDATE: bool = false;
9091

9192
pub const SELF_UPDATE_MODES: [&str; 3] = ["enable", "disable", "check-only"];
93+
#[derive(Clone, Debug, PartialEq)]
94+
pub enum UpdateMode {
95+
Enable,
96+
Disable,
97+
CheckOnly,
98+
}
99+
100+
impl TryFrom<&str> for UpdateMode {
101+
type Error = ();
102+
103+
fn try_from(value: &str) -> Result<Self, Self::Error> {
104+
match value {
105+
"enable" => Ok(Self::Enable),
106+
"disable" => Ok(Self::Disable),
107+
"check-only" => Ok(Self::CheckOnly),
108+
_ => Err(()),
109+
}
110+
}
111+
}
112+
113+
impl ToString for UpdateMode {
114+
fn to_string(&self) -> String {
115+
match self {
116+
UpdateMode::Enable => SELF_UPDATE_MODES[0].to_string(),
117+
UpdateMode::Disable => SELF_UPDATE_MODES[1].to_string(),
118+
UpdateMode::CheckOnly => SELF_UPDATE_MODES[2].to_string(),
119+
}
120+
}
121+
}
92122

93123
// The big installation messages. These are macros because the first
94124
// argument of format! needs to be a literal.

src/config.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::convert::TryFrom;
23
use std::fmt::{self, Display};
34
use std::io;
45
use std::path::{Path, PathBuf};
@@ -11,7 +12,7 @@ use pgp::{Deserializable, SignedPublicKey};
1112
use serde::Deserialize;
1213
use thiserror::Error as ThisError;
1314

14-
use crate::cli::self_update::{valid_self_update_modes, SELF_UPDATE_MODES};
15+
use crate::cli::self_update::{valid_self_update_modes, UpdateMode};
1516
use crate::dist::download::DownloadCfg;
1617
use crate::dist::{
1718
dist::{self, valid_profile_names},
@@ -406,21 +407,24 @@ impl Cfg {
406407
Ok(())
407408
}
408409

409-
// FIXME(hi-rustin): It is better to use enumerations rather than strings.
410410
pub fn set_auto_self_update(&mut self, mode: &str) -> Result<()> {
411-
if !SELF_UPDATE_MODES.contains(&mode) {
412-
return Err(anyhow!(
413-
"unknown self update mode: '{}'; valid modes are {}",
414-
mode.to_owned(),
415-
valid_self_update_modes(),
416-
));
411+
match UpdateMode::try_from(mode) {
412+
Ok(update_mode) => {
413+
self.settings_file.with_mut(|s| {
414+
s.auto_self_update = Some(update_mode);
415+
Ok(())
416+
})?;
417+
(self.notify_handler)(Notification::SetSelfUpdate(mode));
418+
Ok(())
419+
}
420+
Err(_) => {
421+
return Err(anyhow!(
422+
"unknown self update mode: '{}'; valid modes are {}",
423+
mode.to_owned(),
424+
valid_self_update_modes(),
425+
));
426+
}
417427
}
418-
self.settings_file.with_mut(|s| {
419-
s.auto_self_update = Some(mode.to_owned());
420-
Ok(())
421-
})?;
422-
(self.notify_handler)(Notification::SetSelfUpdate(mode));
423-
Ok(())
424428
}
425429

426430
pub fn set_toolchain_override(&mut self, toolchain_override: &str) {
@@ -448,11 +452,11 @@ impl Cfg {
448452
})
449453
}
450454

451-
pub fn get_self_update_mode(&self) -> Result<String> {
455+
pub fn get_self_update_mode(&self) -> Result<UpdateMode> {
452456
self.settings_file.with(|s| {
453457
let mode = match &s.auto_self_update {
454458
Some(mode) => mode.clone(),
455-
None => SELF_UPDATE_MODES[0].to_string(),
459+
None => UpdateMode::Enable,
456460
};
457461
Ok(mode)
458462
})

src/settings.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3+
use std::convert::TryFrom;
34
use std::path::{Path, PathBuf};
45

56
use anyhow::{Context, Result};
67

8+
use crate::cli::self_update::UpdateMode;
79
use crate::errors::*;
810
use crate::notifications::*;
911
use crate::toml_utils::*;
@@ -77,7 +79,7 @@ pub struct Settings {
7779
pub profile: Option<String>,
7880
pub overrides: BTreeMap<String, String>,
7981
pub pgp_keys: Option<String>,
80-
pub auto_self_update: Option<String>,
82+
pub auto_self_update: Option<UpdateMode>,
8183
}
8284

8385
impl Default for Settings {
@@ -89,7 +91,7 @@ impl Default for Settings {
8991
profile: Some("default".to_owned()),
9092
overrides: BTreeMap::new(),
9193
pgp_keys: None,
92-
auto_self_update: Some("enable".to_owned()),
94+
auto_self_update: Some(UpdateMode::Enable),
9395
}
9496
}
9597
}
@@ -148,14 +150,21 @@ impl Settings {
148150
if !SUPPORTED_METADATA_VERSIONS.contains(&&*version) {
149151
return Err(RustupError::UnknownMetadataVersion(version).into());
150152
}
153+
let auto_self_update = match get_opt_string(&mut table, "auto_self_update", path)? {
154+
Some(auto_self_update) => match UpdateMode::try_from(auto_self_update.as_str()) {
155+
Ok(mode) => Some(mode),
156+
Err(_) => None,
157+
},
158+
None => None,
159+
};
151160
Ok(Self {
152161
version,
153162
default_host_triple: get_opt_string(&mut table, "default_host_triple", path)?,
154163
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
155164
profile: get_opt_string(&mut table, "profile", path)?,
156165
overrides: Self::table_to_overrides(&mut table, path)?,
157166
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
158-
auto_self_update: get_opt_string(&mut table, "auto_self_update", path)?,
167+
auto_self_update,
159168
})
160169
}
161170
pub fn into_toml(self) -> toml::value::Table {
@@ -180,7 +189,10 @@ impl Settings {
180189
}
181190

182191
if let Some(v) = self.auto_self_update {
183-
result.insert("auto_self_update".to_owned(), toml::Value::String(v));
192+
result.insert(
193+
"auto_self_update".to_owned(),
194+
toml::Value::String(v.to_string()),
195+
);
184196
}
185197

186198
let overrides = Self::overrides_to_table(self.overrides);

0 commit comments

Comments
 (0)