Skip to content

Commit

Permalink
Add cargo-style output diagnostics.
Browse files Browse the repository at this point in the history
Adds the quiet and color command-line flags, where color supports `auto`, always`, and `never`. These command-line flags are parsed to a verbosity which can be quiet, normal, or verbose.

With these, we then have the stderr message formatters:
- `fatal_usage`: print a fatal error message with the failing argument, and add a help context menu for how to use cross.
- `fatal`: print red 'error' message and exit with an error code
- `error`: print red 'error' message
- `warn`: print amber 'warning' message
- `note`: print cyan 'note' message
- `status`: print an uncolored and unprefixed 'status' message

We have the stdout message formatters:
- `print`: always print the message
- `info`: print the message as long as the verbosity is not quiet
- `debug`: only print the message if the output is not quiet

We also have a few specialized error handlers, and methods to help ensure we can have flexible error reporting in the future:
- `status_stderr`
- `status_stdout`

The command extensions now have, `print`, `info`, and `debug`, which formats the command and sends it to the shell. This allows us to avoid using `print_verbose` where we sometimes manually override the default setting.

Closes #797.
  • Loading branch information
Alexhuszagh committed Jun 25, 2022
1 parent 345f5fc commit 085378b
Show file tree
Hide file tree
Showing 31 changed files with 1,192 additions and 451 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- #859 - added color diagnostic output and error messages.
- #838 - re-enabled the solaris targets.
- #807 - update Qemu to 6.1.0 on images using Ubuntu 18.04+ with python3.6+.
- #775 - forward Cargo exit code to host
Expand Down
1 change: 1 addition & 0 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 @@ -47,6 +47,7 @@ ctrlc = { version = "3.2.2", features = ["termination"] }
directories = "4.0.1"
walkdir = { version = "2", optional = true }
tempfile = "3.3.0"
termcolor = "1.1.3"

[target.'cfg(not(windows))'.dependencies]
nix = { version = "0.24", default-features = false, features = ["user"] }
Expand Down
27 changes: 23 additions & 4 deletions src/bin/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ use std::fs;
use super::containers::*;
use super::images::*;
use clap::Args;
use cross::shell::{self, MessageInfo};

#[derive(Args, Debug)]
pub struct Clean {
/// Provide verbose diagnostic output.
#[clap(short, long)]
pub verbose: bool,
/// Do not print cross log messages.
#[clap(short, long)]
pub quiet: bool,
/// Whether messages should use color output.
#[clap(long)]
pub color: Option<String>,
/// Force removal of images.
#[clap(short, long)]
pub force: bool,
Expand All @@ -25,22 +32,28 @@ pub struct Clean {

impl Clean {
pub fn run(self, engine: cross::docker::Engine) -> cross::Result<()> {
let msg_info = MessageInfo::create(self.verbose, self.quiet, self.color.as_deref())?;
let tempdir = cross::temp::dir()?;
match self.execute {
true => {
if tempdir.exists() {
fs::remove_dir_all(tempdir)?;
}
}
false => println!(
"fs::remove_dir_all({})",
cross::pretty_path(&tempdir, |_| false)
),
false => shell::print(
format!(
"fs::remove_dir_all({})",
cross::pretty_path(&tempdir, |_| false)
),
msg_info,
)?,
}

// containers -> images -> volumes -> prune to ensure no conflicts.
let remove_containers = RemoveAllContainers {
verbose: self.verbose,
quiet: self.quiet,
color: self.color.clone(),
force: self.force,
execute: self.execute,
engine: None,
Expand All @@ -50,6 +63,8 @@ impl Clean {
let remove_images = RemoveImages {
targets: vec![],
verbose: self.verbose,
quiet: self.quiet,
color: self.color.clone(),
force: self.force,
local: self.local,
execute: self.execute,
Expand All @@ -59,6 +74,8 @@ impl Clean {

let remove_volumes = RemoveAllVolumes {
verbose: self.verbose,
quiet: self.quiet,
color: self.color.clone(),
force: self.force,
execute: self.execute,
engine: None,
Expand All @@ -67,6 +84,8 @@ impl Clean {

let prune_volumes = PruneVolumes {
verbose: self.verbose,
quiet: self.quiet,
color: self.color.clone(),
execute: self.execute,
engine: None,
};
Expand Down
Loading

0 comments on commit 085378b

Please sign in to comment.