Skip to content

Commit

Permalink
fix: quiet flag regression introduced in 3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjcasey committed Aug 23, 2024
1 parent a507712 commit e1b87ea
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 31 deletions.
7 changes: 5 additions & 2 deletions hipcheck/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::target::{
use clap::{Parser as _, ValueEnum};
use hipcheck_macros as hc;
use pathbuf::pathbuf;
use std::{env, path::{Path, PathBuf}};
use std::path::{Path, PathBuf};
use url::Url;

/// Automatated supply chain risk assessment of software packages.
Expand Down Expand Up @@ -324,7 +324,10 @@ impl CliConfig {
config: dirs::home_dir().map(|dir| pathbuf![&dir, "hipcheck", "config"]),
data: dirs::home_dir().map(|dir| pathbuf![&dir, "hipcheck", "data"]),
cache: dirs::home_dir().map(|dir| pathbuf![&dir, "hipcheck", "cache"]),
policy: env::current_dir().ok().map(|dir| pathbuf![&dir, "Hipcheck.kdl"]),
// TODO: currently if this is set, then when running `hc check`, it errors out
// because policy files are not yet supported
// policy: env::current_dir().ok().map(|dir| pathbuf![&dir, "Hipcheck.kdl"]),
policy: None,
},
..Default::default()
}
Expand Down
1 change: 0 additions & 1 deletion hipcheck/src/session/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ fn load_target(seed: &TargetSeed, home: &Path) -> Result<Target> {
// Set the phase to tick steadily 10 times a second.
phase.enable_steady_tick(Duration::from_millis(100));
let target = resolve_target(seed, &phase, home)?;
println!("TARGET: {target:?}");
phase.finish_successful();

Ok(target)
Expand Down
7 changes: 6 additions & 1 deletion hipcheck/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,12 @@ impl Shell {

/// Print "Analysing {source}" with the proper color/styling.
pub fn print_prelude(source: impl AsRef<str>) {
macros::println!("{:>LEFT_COL_WIDTH$} {}", Title::Analyzing, source.as_ref());
match Shell::get_verbosity() {
Verbosity::Normal => {
macros::println!("{:>LEFT_COL_WIDTH$} {}", Title::Analyzing, source.as_ref());
}
Verbosity::Quiet | Verbosity::Silent => {}
}
}

/// Print a hipcheck [Error]. Human readable errors will go to the standard error, JSON will go to the standard output.
Expand Down
39 changes: 25 additions & 14 deletions hipcheck/src/shell/spinner_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use crate::shell::Title;

use super::{Shell, HOUR_GLASS, LEFT_COL_WIDTH, ROCKET_SHIP};
use super::{verbosity::Verbosity, Shell, HOUR_GLASS, LEFT_COL_WIDTH, ROCKET_SHIP};
use console::style;
use indicatif::{HumanDuration, ProgressBar, ProgressStyle};
use indicatif::{HumanDuration, ProgressBar, ProgressDrawTarget, ProgressStyle};
use std::{
fmt::Display,
sync::{Arc, OnceLock},
Expand Down Expand Up @@ -41,11 +41,18 @@ impl SpinnerPhase {
///
/// The phase will remain in the "starting..." state until incremented.
pub fn start(name: impl Into<Arc<str>>) -> Self {
// Create the spinner progress bar with our styling.
let bar = ProgressBar::new_spinner().with_style(spinner_style().clone());

// Add to the global shell.
Shell::progress_bars().add(bar.clone());
// Add to the global shell, only if Verbosity::Normal
let bar = match Shell::get_verbosity() {
Verbosity::Quiet | Verbosity::Silent => {
// ProgressBar::new_spinner internally assumes data will be written to stderr, which is not what is wanted for Silent/Quiet
ProgressBar::with_draw_target(None, ProgressDrawTarget::hidden())
}
Verbosity::Normal => {
let bar = ProgressBar::new_spinner().with_style(spinner_style().clone());
Shell::progress_bars().add(bar.clone());
bar
}
};

let name = name.into();

Expand Down Expand Up @@ -91,13 +98,17 @@ impl SpinnerPhase {

/// Finishes this spinner, leaving it in the terminal with an updated "done" message.
pub fn finish_successful(&self) {
super::macros::println!(
"{:>LEFT_COL_WIDTH$} {} ({})",
Title::Done,
self.name,
style(HumanDuration(self.elapsed())).bold()
);

match Shell::get_verbosity() {
Verbosity::Normal => {
super::macros::println!(
"{:>LEFT_COL_WIDTH$} {} ({})",
Title::Done,
self.name,
style(HumanDuration(self.elapsed())).bold()
);
}
Verbosity::Quiet | Verbosity::Silent => {}
}
self.bar.finish_and_clear()
}

Expand Down
33 changes: 20 additions & 13 deletions hipcheck/src/source/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Git related types and implementations for pulling/cloning source repos.

use crate::error::{Error as HcError, Result as HcResult};
use crate::shell::verbosity::Verbosity;
use crate::{
context::Context,
shell::{progress_phase::ProgressPhase, Shell},
Expand All @@ -25,27 +26,33 @@ fn make_remote_callbacks() -> RemoteCallbacks<'static> {

// Messages from the remote ("Counting objects" etc) are sent over the sideband.
// This involves clearing and replacing the line -- use console to do this effectively.
callbacks.sideband_progress(move |msg: &[u8]| {
Shell::in_suspend(|| {
// use the standard output.
let mut term = Term::stdout();

// Crash on errors here, since they should be relatively uncommon.
term.clear_line().expect("clear line on standard output");
match Shell::get_verbosity() {
Verbosity::Normal => {
callbacks.sideband_progress(move |msg: &[u8]| {
Shell::in_suspend(|| {
// use the standard output.
let mut term = Term::stdout();

write!(&mut term, "remote: {}", String::from_utf8_lossy(msg))
.expect("wrote to standard output");
// Crash on errors here, since they should be relatively uncommon.
term.clear_line().expect("clear line on standard output");

term.flush().expect("flushed standard output");
});
write!(&mut term, "remote: {}", String::from_utf8_lossy(msg))
.expect("wrote to standard output");

true
});
term.flush().expect("flushed standard output");
});

true
});
}
Verbosity::Quiet | Verbosity::Silent => {}
}

callbacks.transfer_progress(move |prog: Progress| {
if prog.received_objects() > 0 {
let phase = transfer_phase.get_or_init(|| {
ProgressPhase::start(prog.total_objects() as u64, "(git) recieving objects")
ProgressPhase::start(prog.total_objects() as u64, "(git) receiving objects")
});

phase.set_position(prog.received_objects() as u64);
Expand Down

0 comments on commit e1b87ea

Please sign in to comment.