Skip to content

Commit 31fd3c6

Browse files
committed
address comments
1 parent 3670450 commit 31fd3c6

File tree

5 files changed

+87
-34
lines changed

5 files changed

+87
-34
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: 6 additions & 6 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, SelfUpdateMode},
1919
};
2020
use crate::dist::dist::{
2121
PartialTargetTriple, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc,
@@ -718,8 +718,8 @@ pub fn cli() -> App<'static, 'static> {
718718
.arg(
719719
Arg::with_name("auto-self-update-mode")
720720
.required(true)
721-
.possible_values(&SELF_UPDATE_MODES)
722-
.default_value(SELF_UPDATE_MODES[0]),
721+
.possible_values(SelfUpdateMode::modes())
722+
.default_value(SelfUpdateMode::default_mode()),
723723
),
724724
),
725725
);
@@ -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 == SelfUpdateMode::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 == SelfUpdateMode::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: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use std::fs;
5151
use std::io::Write;
5252
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
5353
use std::process::Command;
54+
use std::str::FromStr;
5455

5556
use anyhow::{anyhow, Context, Result};
5657
use cfg_if::cfg_if;
@@ -88,7 +89,50 @@ pub const NEVER_SELF_UPDATE: bool = true;
8889
#[cfg(not(feature = "no-self-update"))]
8990
pub const NEVER_SELF_UPDATE: bool = false;
9091

91-
pub const SELF_UPDATE_MODES: [&str; 3] = ["enable", "disable", "check-only"];
92+
#[derive(Clone, Debug, PartialEq)]
93+
pub enum SelfUpdateMode {
94+
Enable,
95+
Disable,
96+
CheckOnly,
97+
}
98+
99+
impl SelfUpdateMode {
100+
pub fn modes() -> &'static [&'static str] {
101+
&["enable", "disable", "check-only"]
102+
}
103+
104+
pub fn default_mode() -> &'static str {
105+
"enable"
106+
}
107+
}
108+
109+
impl FromStr for SelfUpdateMode {
110+
type Err = anyhow::Error;
111+
112+
fn from_str(mode: &str) -> Result<Self> {
113+
match mode {
114+
"enable" => Ok(Self::Enable),
115+
"disable" => Ok(Self::Disable),
116+
"check-only" => Ok(Self::CheckOnly),
117+
_ => Err(anyhow!(format!(
118+
"unknown self update mode: '{}'; valid modes are {}",
119+
mode,
120+
valid_self_update_modes(),
121+
))),
122+
}
123+
}
124+
}
125+
126+
impl ToString for SelfUpdateMode {
127+
fn to_string(&self) -> String {
128+
let modes = Self::modes();
129+
match self {
130+
SelfUpdateMode::Enable => modes[0].to_string(),
131+
SelfUpdateMode::Disable => modes[1].to_string(),
132+
SelfUpdateMode::CheckOnly => modes[2].to_string(),
133+
}
134+
}
135+
}
92136

93137
// The big installation messages. These are macros because the first
94138
// argument of format! needs to be a literal.
@@ -494,7 +538,6 @@ fn do_pre_install_sanity_checks(no_prompt: bool) -> Result<()> {
494538
}
495539

496540
fn do_pre_install_options_sanity_checks(opts: &InstallOpts<'_>) -> Result<()> {
497-
use std::str::FromStr;
498541
// Verify that the installation options are vaguely sane
499542
(|| {
500543
let host_triple = opts
@@ -1144,7 +1187,7 @@ pub fn cleanup_self_updater() -> Result<()> {
11441187
}
11451188

11461189
pub fn valid_self_update_modes() -> String {
1147-
SELF_UPDATE_MODES
1190+
SelfUpdateMode::modes()
11481191
.iter()
11491192
.map(|s| format!("'{}'", s))
11501193
.collect::<Vec<_>>()

src/config.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use pgp::{Deserializable, SignedPublicKey};
1111
use serde::Deserialize;
1212
use thiserror::Error as ThisError;
1313

14-
use crate::cli::self_update::{valid_self_update_modes, SELF_UPDATE_MODES};
14+
use crate::cli::self_update::SelfUpdateMode;
1515
use crate::dist::download::DownloadCfg;
1616
use crate::dist::{
1717
dist::{self, valid_profile_names},
@@ -406,21 +406,18 @@ impl Cfg {
406406
Ok(())
407407
}
408408

409-
// FIXME(hi-rustin): It is better to use enumerations rather than strings.
410409
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-
));
410+
match SelfUpdateMode::from_str(mode) {
411+
Ok(update_mode) => {
412+
self.settings_file.with_mut(|s| {
413+
s.auto_self_update = Some(update_mode);
414+
Ok(())
415+
})?;
416+
(self.notify_handler)(Notification::SetSelfUpdate(mode));
417+
Ok(())
418+
}
419+
Err(err) => Err(err),
417420
}
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(())
424421
}
425422

426423
pub fn set_toolchain_override(&mut self, toolchain_override: &str) {
@@ -448,11 +445,11 @@ impl Cfg {
448445
})
449446
}
450447

451-
pub fn get_self_update_mode(&self) -> Result<String> {
448+
pub fn get_self_update_mode(&self) -> Result<SelfUpdateMode> {
452449
self.settings_file.with(|s| {
453450
let mode = match &s.auto_self_update {
454451
Some(mode) => mode.clone(),
455-
None => SELF_UPDATE_MODES[0].to_string(),
452+
None => SelfUpdateMode::Enable,
456453
};
457454
Ok(mode)
458455
})

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;
33
use std::path::{Path, PathBuf};
4+
use std::str::FromStr;
45

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

8+
use crate::cli::self_update::SelfUpdateMode;
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<SelfUpdateMode>,
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(SelfUpdateMode::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 SelfUpdateMode::from_str(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)