Skip to content

Commit

Permalink
Support for machine wide installs: 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.

/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.
  • Loading branch information
rbtcollins committed Mar 5, 2020
1 parent 6de2f76 commit d07172d
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 +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");
Expand Down Expand Up @@ -191,6 +208,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 +383,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 +525,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 d07172d

Please sign in to comment.