From d07172d9557e2522f1fd454ef6cffed01e5152cc Mon Sep 17 00:00:00 2001 From: Robert Collins Date: Sat, 29 Feb 2020 12:36:56 +1300 Subject: [PATCH] Support for machine wide installs: fallback-settings file This provides a way for snaps, distro packages and other such installs to set the default-toolchain for users that have not gone through the rustup-init one-time set of questions. /etc/rustup/settings.toml can more now just provide a default toolchain for users of rustup on the machine. More keys will be supported as needed. No equivalent file is defined for Windows OS machines at this time. --- src/config.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index d99822a1602..cc3d7d3ab00 100644 --- a/src/config.rs +++ b/src/config.rs @@ -108,10 +108,13 @@ impl Display for PgpPublicKey { } } +pub const UNIX_FALLBACK_SETTINGS: &str = "/etc/rustup/settings.toml"; + pub struct Cfg { pub profile_override: Option, pub rustup_dir: PathBuf, pub settings_file: SettingsFile, + pub fallback_settings: Option, pub toolchains_dir: PathBuf, pub update_hash_dir: PathBuf, pub download_dir: PathBuf, @@ -133,6 +136,20 @@ impl Cfg { let settings_file = SettingsFile::new(rustup_dir.join("settings.toml")); + // Centralised file for multi-user systems to provide admin/distributor set initial values. + let fallback_settings = if cfg!(not(windows)) { + let f = SettingsFile::new(PathBuf::from(UNIX_FALLBACK_SETTINGS)); + // Users cannot fix issues with missing/unreadable/invalid centralised files, so we simply trap all errors. + // Ideally we would separate these into missing file vs others, so that we could log a message about the bad + // situation rather than entirely ignoring it. + match f.with(|s| Ok(Some(s.clone()))) { + Ok(res) => res, + Err(_) => None, + } + } else { + None + }; + let toolchains_dir = rustup_dir.join("toolchains"); let update_hash_dir = rustup_dir.join("update-hashes"); let download_dir = rustup_dir.join("downloads"); @@ -191,6 +208,7 @@ impl Cfg { profile_override: None, rustup_dir, settings_file, + fallback_settings, toolchains_dir, update_hash_dir, download_dir, @@ -365,9 +383,7 @@ impl Cfg { } pub fn find_default(&self) -> Result>> { - let opt_name = self - .settings_file - .with(|s| Ok(s.default_toolchain.clone()))?; + let opt_name = self.get_default()?; if let Some(name) = opt_name { let toolchain = self @@ -509,7 +525,14 @@ impl Cfg { } pub fn get_default(&self) -> Result> { - self.settings_file.with(|s| Ok(s.default_toolchain.clone())) + let user_opt = self.settings_file.with(|s| Ok(s.default_toolchain.clone())); + if let Some(fallback_settings) = &self.fallback_settings { + match user_opt { + Err(_) | Ok(None) => return Ok(fallback_settings.default_toolchain.clone()), + _ => {} + }; + }; + user_opt } pub fn list_toolchains(&self) -> Result> {