Skip to content

Commit

Permalink
WIP: fallback-settings file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rbtcollins committed Feb 28, 2020
1 parent ed6f719 commit 2259d3f
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct Cfg {
pub profile_override: Option<dist::Profile>,
pub rustup_dir: PathBuf,
pub settings_file: SettingsFile,
pub fallback_settings: Option<Settings>,
pub toolchains_dir: PathBuf,
pub update_hash_dir: PathBuf,
pub download_dir: PathBuf,
Expand All @@ -133,6 +134,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("/etc/rustup/settings.toml"));
// 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");
Expand Down Expand Up @@ -191,6 +206,7 @@ impl Cfg {
profile_override: None,
rustup_dir,
settings_file,
fallback_settings,
toolchains_dir,
update_hash_dir,
download_dir,
Expand Down Expand Up @@ -365,9 +381,7 @@ impl Cfg {
}

pub fn find_default(&self) -> Result<Option<Toolchain<'_>>> {
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
Expand Down Expand Up @@ -509,7 +523,14 @@ impl Cfg {
}

pub fn get_default(&self) -> Result<Option<String>> {
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<Vec<String>> {
Expand Down

0 comments on commit 2259d3f

Please sign in to comment.