Skip to content

Commit eb2b8ad

Browse files
Merge #859
859: Add cargo-style output diagnostics. r=Emilgardis a=Alexhuszagh 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. A few of these settings aren't currently used (such as `info` and `status`, but they're a very common pattern), so we can ensure we have the necessary utilities to ensure we can properly format messages in the future. A few like `shell::print` are practically identical to `println!`, but this allows us to customize it if need be in the future. Solves parts of #797. Co-authored-by: Alex Huszagh <[email protected]>
2 parents 021a103 + eef20f9 commit eb2b8ad

32 files changed

+1236
-483
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111

1212
- #869 - ensure cargo configuration environment variable flags are passed to the docker container.
13+
- #859 - added color diagnostic output and error messages.
1314

1415
### Fixed
1516

Cargo.lock

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ctrlc = { version = "3.2.2", features = ["termination"] }
4747
directories = "4.0.1"
4848
walkdir = { version = "2", optional = true }
4949
tempfile = "3.3.0"
50+
owo-colors = { version = "3.4.0", features = ["supports-colors"] }
5051

5152
[target.'cfg(not(windows))'.dependencies]
5253
nix = { version = "0.24", default-features = false, features = ["user"] }

deny.toml

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ unknown-git = "deny"
2424
allow-git = []
2525

2626
[licenses]
27+
# need this since to suppress errors in case we add crates with these allowed licenses
28+
unused-allowed-license = "allow"
2729
unlicensed = "deny"
2830
allow-osi-fsf-free = "neither"
2931
copyleft = "deny"
3032
confidence-threshold = 0.93
31-
allow = ["Apache-2.0", "MIT", "CC0-1.0"]
33+
allow = [
34+
"Apache-2.0",
35+
"MIT",
36+
"CC0-1.0",
37+
"ISC",
38+
"0BSD",
39+
"BSD-2-Clause",
40+
"BSD-3-Clause",
41+
"Unlicense"
42+
]
3243

3344
[licenses.private]
3445
ignore = true

src/bin/commands/clean.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@ use std::fs;
33
use super::containers::*;
44
use super::images::*;
55
use clap::Args;
6+
use cross::shell::{self, MessageInfo};
67

78
#[derive(Args, Debug)]
89
pub struct Clean {
910
/// Provide verbose diagnostic output.
1011
#[clap(short, long)]
1112
pub verbose: bool,
13+
/// Do not print cross log messages.
14+
#[clap(short, long)]
15+
pub quiet: bool,
16+
/// Whether messages should use color output.
17+
#[clap(long)]
18+
pub color: Option<String>,
1219
/// Force removal of images.
1320
#[clap(short, long)]
1421
pub force: bool,
@@ -25,22 +32,28 @@ pub struct Clean {
2532

2633
impl Clean {
2734
pub fn run(self, engine: cross::docker::Engine) -> cross::Result<()> {
35+
let msg_info = MessageInfo::create(self.verbose, self.quiet, self.color.as_deref())?;
2836
let tempdir = cross::temp::dir()?;
2937
match self.execute {
3038
true => {
3139
if tempdir.exists() {
3240
fs::remove_dir_all(tempdir)?;
3341
}
3442
}
35-
false => println!(
36-
"fs::remove_dir_all({})",
37-
cross::pretty_path(&tempdir, |_| false)
38-
),
43+
false => shell::print(
44+
format!(
45+
"fs::remove_dir_all({})",
46+
cross::pretty_path(&tempdir, |_| false)
47+
),
48+
msg_info,
49+
)?,
3950
}
4051

4152
// containers -> images -> volumes -> prune to ensure no conflicts.
4253
let remove_containers = RemoveAllContainers {
4354
verbose: self.verbose,
55+
quiet: self.quiet,
56+
color: self.color.clone(),
4457
force: self.force,
4558
execute: self.execute,
4659
engine: None,
@@ -50,6 +63,8 @@ impl Clean {
5063
let remove_images = RemoveImages {
5164
targets: vec![],
5265
verbose: self.verbose,
66+
quiet: self.quiet,
67+
color: self.color.clone(),
5368
force: self.force,
5469
local: self.local,
5570
execute: self.execute,
@@ -59,6 +74,8 @@ impl Clean {
5974

6075
let remove_volumes = RemoveAllVolumes {
6176
verbose: self.verbose,
77+
quiet: self.quiet,
78+
color: self.color.clone(),
6279
force: self.force,
6380
execute: self.execute,
6481
engine: None,
@@ -67,6 +84,8 @@ impl Clean {
6784

6885
let prune_volumes = PruneVolumes {
6986
verbose: self.verbose,
87+
quiet: self.quiet,
88+
color: self.color.clone(),
7089
execute: self.execute,
7190
engine: None,
7291
};

0 commit comments

Comments
 (0)