Skip to content

Commit

Permalink
Merge pull request #420 from rust-lang-nursery/toml-settings
Browse files Browse the repository at this point in the history
Use a .toml file to store settings
  • Loading branch information
brson committed May 17, 2016
2 parents 1a335ec + e16565b commit 66af8c1
Show file tree
Hide file tree
Showing 24 changed files with 402 additions and 284 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ scopeguard = "0.1.2"
rustc-serialize = "0.3"
sha2 = "0.1.2"
markdown = { git="https://github.com/Diggsey/markdown.rs.git" }
toml = "0.1.27"

[target."cfg(windows)".dependencies]
winapi = "0.2.4"
Expand Down
24 changes: 5 additions & 19 deletions src/rustup-cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use self_update;
use std::io::{Write, BufRead};
use std::process::Command;
use std::{cmp, iter};
use std::str::FromStr;
use std;
use term2;

Expand Down Expand Up @@ -321,19 +320,15 @@ pub fn list_toolchains(cfg: &Cfg) -> Result<()> {
}

pub fn list_overrides(cfg: &Cfg) -> Result<()> {
let mut overrides = try!(cfg.override_db.list());

overrides.sort();
let overrides = try!(cfg.settings_file.with(|s| Ok(s.overrides.clone())));

if overrides.is_empty() {
println!("no overrides");
} else {
for o in overrides {
split_override::<String>(&o, ';').map(|li|
println!("{:<40}\t{:<20}",
utils::format_path_for_display(&li.0),
li.1)
);
for (k, v) in overrides {
println!("{:<40}\t{:<20}",
utils::format_path_for_display(&k),
v)
}
}
Ok(())
Expand All @@ -344,15 +339,6 @@ pub fn version() -> &'static str {
concat!(env!("CARGO_PKG_VERSION"), include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt")))
}

fn split_override<T: FromStr>(s: &str, separator: char) -> Option<(T, T)> {
s.find(separator).and_then(|index| {
match (T::from_str(&s[..index]), T::from_str(&s[index + 1..])) {
(Ok(l), Ok(r)) => Some((l, r)),
_ => None
}
})
}


pub fn report_error(e: &Error) {
err!("{}", e);
Expand Down
12 changes: 5 additions & 7 deletions src/rustup-cli/multirust_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,13 @@ fn remove_override(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref path = m.value_of("override")
.map(|p| PathBuf::from(p)).unwrap_or(cwd);

if try!(cfg.override_db.find(path, cfg.notify_handler.as_ref())).is_none() {
if try!(cfg.settings_file.with_mut(|s| {
Ok(s.remove_override(path, cfg.notify_handler.as_ref()))
})) {
info!("override toolchain for '{}' removed", path.display());
} else {
info!("no override toolchain for '{}'", path.display());
return Ok(());
}

try!(cfg.override_db.remove(path,
&cfg.temp_cfg,
cfg.notify_handler.as_ref()));
info!("override toolchain for '{}' removed", path.display());
Ok(())
}

Expand Down
15 changes: 6 additions & 9 deletions src/rustup-cli/rustup_mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{App, Arg, ArgGroup, AppSettings, SubCommand, ArgMatches};
use common;
use rustup::{Cfg, Toolchain, command};
use rustup::telemetry::TelemetryMode;
use rustup::settings::TelemetryMode;
use errors::*;
use rustup_dist::manifest::Component;
use rustup_dist::dist::TargetTriple;
Expand Down Expand Up @@ -510,16 +510,13 @@ fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
fn override_remove(cfg: &Cfg) -> Result<()> {
let ref path = try!(utils::current_dir());

let ref override_db = cfg.override_db;
let notify_handler = cfg.notify_handler.as_ref();

if try!(override_db.find(path, notify_handler)).is_none() {
if try!(cfg.settings_file.with_mut(|s| {
Ok(s.remove_override(path, cfg.notify_handler.as_ref()))
})) {
info!("override toolchain for '{}' removed", path.display());
} else {
info!("no override toolchain for '{}'", path.display());
return Ok(());
}

try!(override_db.remove(path, &cfg.temp_cfg, notify_handler));
info!("override toolchain for '{}' removed", path.display());
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/rustup-dist/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use toml;

use toml_utils::*;
use rustup_utils::toml_utils::*;
use errors::*;
use super::manifest::Component;

Expand Down
6 changes: 1 addition & 5 deletions src/rustup-dist/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ error_chain! {
Parsing(e: Vec<toml::ParserError>) {
description("error parsing manifest")
}
ExpectedType(t: &'static str, n: String) {
description("expected type")
display("expected type: '{}' for '{}'", t, n)
}
UnsupportedVersion(v: String) {
description("unsupported manifest version")
display("manifest version '{}' is not supported", v)
Expand All @@ -116,7 +112,7 @@ fn component_unavailable_msg(cs: &[Component]) -> String {
assert!(!cs.is_empty());

let mut buf = vec![];

if cs.len() == 1 {
let _ = write!(buf, "component '{}' for '{}' is unavailable for download",
cs[0].pkg, cs[0].target);
Expand Down
1 change: 0 additions & 1 deletion src/rustup-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@ pub mod manifestation;
pub mod download;
pub mod manifest;
pub mod config;
mod toml_utils;
2 changes: 1 addition & 1 deletion src/rustup-dist/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use errors::*;
use toml;
use toml_utils::*;
use rustup_utils::toml_utils::*;

use std::collections::HashMap;
use dist::TargetTriple;
Expand Down
3 changes: 2 additions & 1 deletion src/rustup-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ error-chain = { path = "../error-chain", version = "0.1.12" }
libc = "0.2.0"
rustc-serialize = "0.3.19"
sha2 = "0.1.2"
curl = { git = "https://github.com/alexcrichton/curl-rust", branch = "rewrite" }
curl = { git = "https://github.com/alexcrichton/curl-rust", rev = "ccf35d3e" }
url = "1.1"
toml = "0.1.27"

[target."cfg(windows)".dependencies]
winapi = "0.2.4"
Expand Down
4 changes: 4 additions & 0 deletions src/rustup-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ error_chain! {
description("could not create directory")
display("could not crate {} directory: '{}'", name, path.display())
}
ExpectedType(t: &'static str, n: String) {
description("expected type")
display("expected type: '{}' for '{}'", t, n)
}
FilteringFile {
name: &'static str,
src: PathBuf,
Expand Down
2 changes: 2 additions & 0 deletions src/rustup-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern crate error_chain;
extern crate rustc_serialize;
extern crate sha2;
extern crate url;
extern crate toml;

#[cfg(windows)]
extern crate winapi;
Expand All @@ -35,6 +36,7 @@ pub mod notifications;
pub mod raw;
pub mod tty;
pub mod utils;
pub mod toml_utils;

pub use errors::*;
pub use notifications::{Notification, NotifyHandler};
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,40 @@ pub fn get_string(table: &mut toml::Table, key: &str, path: &str) -> Result<Stri
})
}

pub fn get_opt_string(table: &mut toml::Table, key: &str, path: &str) -> Result<Option<String>> {
if let Ok(v) = get_value(table, key, path) {
if let toml::Value::String(s) = v {
Ok(Some(s))
} else {
Err(ErrorKind::ExpectedType("string", path.to_owned() + key).into())
}
} else {
Ok(None)
}
}

pub fn get_bool(table: &mut toml::Table, key: &str, path: &str) -> Result<bool> {
get_value(table, key, path).and_then(|v| {
if let toml::Value::Boolean(b) = v {
Ok(b)
} else {
Err(ErrorKind::ExpectedType("string", path.to_owned() + key).into())
Err(ErrorKind::ExpectedType("bool", path.to_owned() + key).into())
}
})
}

pub fn get_opt_bool(table: &mut toml::Table, key: &str, path: &str) -> Result<Option<bool>> {
if let Ok(v) = get_value(table, key, path) {
if let toml::Value::Boolean(b) = v {
Ok(Some(b))
} else {
Err(ErrorKind::ExpectedType("bool", path.to_owned() + key).into())
}
} else {
Ok(None)
}
}

pub fn get_table(table: &mut toml::Table, key: &str, path: &str) -> Result<toml::Table> {
if let Some(v) = table.remove(key) {
if let toml::Value::Table(t) = v {
Expand Down
24 changes: 12 additions & 12 deletions src/rustup/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ pub fn run_command_for_dir<S: AsRef<OsStr>>(cmd: Command,
.and_then(|a| a.file_name())
.and_then(|a| a.to_str());
let arg0 = try!(arg0.ok_or(ErrorKind::NoExeName));
if (arg0 == "rustc" || arg0 == "rustc.exe") && cfg.telemetry_enabled() {
if (arg0 == "rustc" || arg0 == "rustc.exe") && try!(cfg.telemetry_enabled()) {
return telemetry_rustc(cmd, &args, &cfg);
}

run_command_for_dir_without_telemetry(cmd, &args)
}

fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) -> Result<()> {
let now = Instant::now();

cmd.args(&args[1..]);

let has_color_args = (&args).iter().any(|e| {
let e = e.as_ref().to_str().unwrap_or("");
e.starts_with("--color")
});

if stderr_isatty() && !has_color_args
{
cmd.arg("--color");
cmd.arg("always");
cmd.arg("always");
}

// FIXME rust-lang/rust#32254. It's not clear to me
Expand Down Expand Up @@ -77,7 +77,7 @@ fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) ->

while buffered_stderr.read_line(&mut buffer).unwrap() > 0 {
let b = buffer.to_owned();
buffer.clear();
buffer.clear();
let _ = handle.write(b.as_bytes());

let c = re.captures(&b);
Expand All @@ -91,15 +91,15 @@ fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) ->
};
}

let e = match errors.len() {
let e = match errors.len() {
0 => None,
_ => Some(errors),
};

let te = TelemetryEvent::RustcRun { duration_ms: ms,
let te = TelemetryEvent::RustcRun { duration_ms: ms,
exit_code: exit_code,
errors: e };

let _ = t.log_telemetry(te).map_err(|xe| {
cfg.notify_handler.call(Notification::TelemetryCleanupError(&xe));
});
Expand All @@ -111,7 +111,7 @@ fn telemetry_rustc<S: AsRef<OsStr>>(mut cmd: Command, args: &[S], cfg: &Cfg) ->
let te = TelemetryEvent::RustcRun { duration_ms: ms,
exit_code: exit_code,
errors: None };

let _ = t.log_telemetry(te).map_err(|xe| {
cfg.notify_handler.call(Notification::TelemetryCleanupError(&xe));
});
Expand Down Expand Up @@ -141,7 +141,7 @@ fn run_command_for_dir_without_telemetry<S: AsRef<OsStr>>(mut cmd: Command, args
name: args[0].as_ref().to_owned(),
})
}
}
}
}

#[cfg(unix)]
Expand Down
Loading

0 comments on commit 66af8c1

Please sign in to comment.