Skip to content

Commit

Permalink
feat: --quiet option
Browse files Browse the repository at this point in the history
--quiet disables progress output
tests: see `tests/cli-rustup.rs`
  • Loading branch information
PicoJr authored and PicoJr committed Aug 10, 2019
1 parent d2ef3fc commit d8248dc
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 10 deletions.
1 change: 1 addition & 0 deletions rustup-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ USAGE:
FLAGS:
-v, --verbose Enable verbose output
-q, --quiet Disable progress output
-y Disable confirmation prompt.
--no-modify-path Don't configure the PATH environment variable
-h, --help Prints help information
Expand Down
4 changes: 2 additions & 2 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ pub fn read_line() -> Result<String> {
.ok_or_else(|| "unable to read from stdin for confirmation".into())
}

pub fn set_globals(verbose: bool) -> Result<Cfg> {
pub fn set_globals(verbose: bool, quiet: bool) -> Result<Cfg> {
use crate::download_tracker::DownloadTracker;
use std::cell::RefCell;

let download_tracker = RefCell::new(DownloadTracker::new());
let download_tracker = RefCell::new(DownloadTracker::new().with_display_progress(!quiet));

Ok(Cfg::from_env(Arc::new(move |n: Notification<'_>| {
if download_tracker.borrow_mut().handle_notification(&n) {
Expand Down
12 changes: 11 additions & 1 deletion src/cli/download_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct DownloadTracker {
displayed_charcount: Option<usize>,
/// What units to show progress in
units: Vec<String>,
/// Whether we display progress
display_progress: bool,
}

impl DownloadTracker {
Expand All @@ -59,9 +61,15 @@ impl DownloadTracker {
term: term2::stdout(),
displayed_charcount: None,
units: vec!["B".into(); 1],
display_progress: true,
}
}

pub fn with_display_progress(mut self, display_progress: bool) -> DownloadTracker {
self.display_progress = display_progress;
self
}

pub fn handle_notification(&mut self, n: &Notification<'_>) -> bool {
match *n {
Notification::Install(In::Utils(Un::DownloadContentLengthReceived(content_len))) => {
Expand Down Expand Up @@ -109,7 +117,9 @@ impl DownloadTracker {
Some(prev) => {
let elapsed = current_time - prev;
if elapsed >= 1.0 {
self.display();
if self.display_progress {
self.display();
}
self.last_sec = Some(current_time);
if self.downloaded_last_few_secs.len() == DOWNLOAD_TRACK_COUNT {
self.downloaded_last_few_secs.pop_back();
Expand Down
2 changes: 1 addition & 1 deletion src/cli/proxy_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn main() -> Result<()> {
env::args_os().skip(2).collect()
};

let cfg = set_globals(false)?;
let cfg = set_globals(false, true)?;
cfg.check_metadata_version()?;
direct_proxy(&cfg, &arg0, toolchain, &cmd_args)?
};
Expand Down
9 changes: 8 additions & 1 deletion src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ pub fn main() -> Result<()> {

let matches = cli().get_matches();
let verbose = matches.is_present("verbose");
let cfg = &common::set_globals(verbose)?;
let quiet = matches.is_present("quiet");
let cfg = &common::set_globals(verbose, quiet)?;

if maybe_upgrade_data(cfg, &matches)? {
return Ok(());
Expand Down Expand Up @@ -118,6 +119,12 @@ pub fn cli() -> App<'static, 'static> {
.short("v")
.long("verbose"),
)
.arg(
Arg::with_name("quiet")
.help("Disable progress output")
.short("q")
.long("quiet"),
)
.subcommand(
SubCommand::with_name("dump-testament")
.about("Dump information about the build")
Expand Down
18 changes: 14 additions & 4 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn canonical_cargo_home() -> Result<String> {
/// Installing is a simple matter of copying the running binary to
/// `CARGO_HOME`/bin, hard-linking the various Rust tools to it,
/// and adding `CARGO_HOME`/bin to PATH.
pub fn install(no_prompt: bool, verbose: bool, mut opts: InstallOpts) -> Result<()> {
pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpts) -> Result<()> {
do_pre_install_sanity_checks()?;
do_pre_install_options_sanity_checks(&opts)?;
check_existence_of_rustc_or_cargo_in_path(no_prompt)?;
Expand Down Expand Up @@ -278,7 +278,12 @@ pub fn install(no_prompt: bool, verbose: bool, mut opts: InstallOpts) -> Result<
do_add_to_path(&get_add_path_methods())?;
}
utils::create_rustup_home()?;
maybe_install_rust(&opts.default_toolchain, &opts.default_host_triple, verbose)?;
maybe_install_rust(
&opts.default_toolchain,
&opts.default_host_triple,
verbose,
quiet,
)?;

if cfg!(unix) {
let env_file = utils::cargo_home()?.join("env");
Expand Down Expand Up @@ -715,8 +720,13 @@ pub fn install_proxies() -> Result<()> {
Ok(())
}

fn maybe_install_rust(toolchain_str: &str, default_host_triple: &str, verbose: bool) -> Result<()> {
let cfg = common::set_globals(verbose)?;
fn maybe_install_rust(
toolchain_str: &str,
default_host_triple: &str,
verbose: bool,
quiet: bool,
) -> Result<()> {
let cfg = common::set_globals(verbose, quiet)?;

// If there is already an install, then `toolchain_str` may not be
// a toolchain the user actually wants. Don't do anything. FIXME:
Expand Down
9 changes: 8 additions & 1 deletion src/cli/setup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub fn main() -> Result<()> {
.long("verbose")
.help("Enable verbose output"),
)
.arg(
Arg::with_name("quiet")
.short("q")
.long("quiet")
.help("Disable progress output"),
)
.arg(
Arg::with_name("no-prompt")
.short("y")
Expand All @@ -57,6 +63,7 @@ pub fn main() -> Result<()> {
let matches = cli.get_matches();
let no_prompt = matches.is_present("no-prompt");
let verbose = matches.is_present("verbose");
let quiet = matches.is_present("quiet");
let default_host = matches
.value_of("default-host")
.map(std::borrow::ToOwned::to_owned)
Expand All @@ -70,7 +77,7 @@ pub fn main() -> Result<()> {
no_modify_path,
};

self_update::install(no_prompt, verbose, opts)?;
self_update::install(no_prompt, verbose, quiet, opts)?;

Ok(())
}
61 changes: 61 additions & 0 deletions tests/cli-rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ info: installing component 'rust-docs'
});
}

#[test]
fn rustup_stable_quiet() {
setup(&|config| {
set_current_dist_date(config, "2015-01-01");
expect_ok(
config,
&["rustup", "--quiet", "update", "stable", "--no-self-update"],
);
set_current_dist_date(config, "2015-01-02");
expect_ok_ex(
config,
&["rustup", "--quiet", "update", "--no-self-update"],
for_host!(
r"
stable-{0} updated - 1.1.0 (hash-s-2)
"
),
for_host!(
r"info: syncing channel updates for 'stable-{0}'
info: latest update on 2015-01-02, rust version 1.1.0
info: downloading component 'rust-std'
info: downloading component 'rustc'
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: removing previous version of component 'rust-std'
info: removing previous version of component 'rustc'
info: removing previous version of component 'cargo'
info: removing previous version of component 'rust-docs'
info: installing component 'rust-std'
info: installing component 'rustc'
info: installing component 'cargo'
info: installing component 'rust-docs'
"
),
);
});
}

#[test]
fn rustup_stable_no_change() {
setup(&|config| {
Expand Down Expand Up @@ -1067,6 +1106,28 @@ fn toolchain_install_is_like_update() {
});
}

#[test]
fn toolchain_install_is_like_update_quiet() {
setup(&|config| {
expect_ok(
config,
&[
"rustup",
"--quiet",
"toolchain",
"install",
"nightly",
"--no-self-update",
],
);
expect_stdout_ok(
config,
&["rustup", "--quiet", "run", "nightly", "rustc", "--version"],
"hash-n-2",
);
});
}

#[test]
fn toolchain_install_is_like_update_except_that_bare_install_is_an_error() {
setup(&|config| {
Expand Down

0 comments on commit d8248dc

Please sign in to comment.