Skip to content

Commit bec15db

Browse files
committed
Better error handling, Readme, rename pretty to default and JSON pretty
1 parent 1e28a73 commit bec15db

8 files changed

+42
-27
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ ignore-packages = [
7878

7979
After installing `conda-deny`, you can run `conda-deny check` in your project.
8080
This then checks `pixi.lock` to determine the packages (and their versions) used in your project.
81+
82+
### ✨ Output Formats
83+
84+
`conda-deny` supports different output formats via the `--output` (or `-o`) flag.
85+
Output formatting works for both, the `list` and the `check` command.
86+
To get an overview of the different format options, try:
87+
88+
```bash
89+
conda-deny check --help
90+
# Or:
91+
conda-deny list --help
92+
```

src/check.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn check<W: Write>(check_config: CondaDenyCheckConfig, mut out: W) -> Result
2727
let (safe_dependencies, unsafe_dependencies) = check_license_infos(&check_config)?;
2828

2929
match check_config.output_format {
30-
OutputFormat::Pretty => {
30+
OutputFormat::Default => {
3131
writeln!(
3232
out,
3333
"{}",
@@ -41,6 +41,13 @@ pub fn check<W: Write>(check_config: CondaDenyCheckConfig, mut out: W) -> Result
4141
});
4242
writeln!(out, "{}", json_output)?;
4343
}
44+
OutputFormat::JsonPretty => {
45+
let json_output = json!({
46+
"safe": safe_dependencies,
47+
"unsafe": unsafe_dependencies,
48+
});
49+
writeln!(out, "{}", serde_json::to_string_pretty(&json_output)?)?;
50+
}
4451
OutputFormat::Csv => {
4552
#[derive(Debug, Clone, Serialize)]
4653
struct LicenseInfoWithSafety {

src/cli.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub enum CondaDenyCliConfig {
5050
ignore_pypi: Option<bool>,
5151

5252
/// Output format
53-
#[arg(short, long, global = true)]
53+
#[arg(short, long)]
5454
output: Option<OutputFormat>,
5555
},
5656
/// List all packages and their licenses in your conda or pixi environment
@@ -76,7 +76,7 @@ pub enum CondaDenyCliConfig {
7676
ignore_pypi: Option<bool>,
7777

7878
/// Output format
79-
#[arg(short, long, global = true)]
79+
#[arg(short, long)]
8080
output: Option<OutputFormat>,
8181
},
8282
}

src/conda_deny_config.rs

-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::vec;
77
use std::{fs::File, io::Read};
88

99
use crate::license_whitelist::IgnorePackage;
10-
use crate::OutputFormat;
1110

1211
#[derive(Debug, Deserialize)]
1312
pub struct CondaDenyTomlConfig {
@@ -66,8 +65,6 @@ pub struct CondaDeny {
6665
pub safe_licenses: Option<Vec<String>>,
6766
#[serde(rename = "ignore-packages")]
6867
pub ignore_packages: Option<Vec<IgnorePackage>>,
69-
#[serde(rename = "output-format")]
70-
pub output_format: Option<OutputFormat>,
7168
}
7269

7370
impl CondaDenyTomlConfig {
@@ -129,10 +126,6 @@ impl CondaDenyTomlConfig {
129126
self.tool.conda_deny.ignore_pypi
130127
}
131128

132-
pub fn get_output_format(&self) -> Option<OutputFormat> {
133-
self.tool.conda_deny.output_format
134-
}
135-
136129
pub fn empty() -> Self {
137130
CondaDenyTomlConfig {
138131
tool: Tool {
@@ -145,7 +138,6 @@ impl CondaDenyTomlConfig {
145138
ignore_pypi: None,
146139
safe_licenses: None,
147140
ignore_packages: None,
148-
output_format: None,
149141
},
150142
},
151143
}

src/lib.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ pub enum CondaDenyConfig {
3333
#[derive(Debug, Clone, clap::ValueEnum, Default, Deserialize, Copy)]
3434
#[serde(rename_all = "kebab-case")]
3535
pub enum OutputFormat {
36-
#[serde(rename = "pretty")]
36+
#[serde(rename = "default")]
3737
#[default]
38-
Pretty,
38+
Default,
3939
#[serde(rename = "json")]
4040
Json,
41+
#[serde(rename = "json-pretty")]
42+
JsonPretty,
4143
#[serde(rename = "csv")]
4244
Csv,
4345
}
@@ -218,11 +220,7 @@ pub fn get_config_options(
218220
toml_config.get_ignore_pypi()
219221
};
220222

221-
let output_format = if cli_config.output_format().is_some() {
222-
cli_config.output_format()
223-
} else {
224-
toml_config.get_output_format()
225-
};
223+
let output_format = cli_config.output_format().unwrap_or(OutputFormat::Default);
226224

227225
let lockfile_or_prefix =
228226
get_lockfile_or_prefix(lockfile, prefix, platforms, environments, ignore_pypi)?;
@@ -254,12 +252,12 @@ pub fn get_config_options(
254252
osi,
255253
safe_licenses,
256254
ignore_packages,
257-
output_format: output_format.unwrap_or(OutputFormat::Pretty),
255+
output_format,
258256
})
259257
}
260258
CondaDenyCliConfig::List { .. } => CondaDenyConfig::List(CondaDenyListConfig {
261259
lockfile_or_prefix,
262-
output_format: output_format.unwrap_or(OutputFormat::Pretty),
260+
output_format,
263261
}),
264262
};
265263

src/license_info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ mod tests {
339339
osi: false,
340340
safe_licenses,
341341
ignore_packages,
342-
output_format: OutputFormat::Pretty,
342+
output_format: OutputFormat::Default,
343343
};
344344

345345
let (_, unsafe_dependencies) = unsafe_license_infos.check(&config).unwrap();

src/list.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub fn list<W: Write>(config: CondaDenyListConfig, mut out: W) -> Result<()> {
88
.with_context(|| "Fetching license information failed.")?;
99

1010
match config.output_format {
11-
OutputFormat::Pretty => {
11+
OutputFormat::Default => {
1212
let mut output = String::new();
1313
for license_info in &license_infos.license_infos {
1414
output.push_str(&license_info.pretty_print());
@@ -18,13 +18,19 @@ pub fn list<W: Write>(config: CondaDenyListConfig, mut out: W) -> Result<()> {
1818
OutputFormat::Json => {
1919
serde_json::to_writer(&mut out, &license_infos)?;
2020
}
21+
OutputFormat::JsonPretty => {
22+
serde_json::to_writer_pretty(&mut out, &license_infos)?;
23+
}
2124
OutputFormat::Csv => {
2225
let mut writer = csv::WriterBuilder::new().from_writer(vec![]);
2326

2427
for license_info in &license_infos.license_infos {
25-
writer
26-
.serialize(license_info)
27-
.expect("Failed to serialize license info");
28+
writer.serialize(license_info).with_context(|| {
29+
format!(
30+
"Failed to serialize the following license info: {:?}",
31+
license_info
32+
)
33+
})?;
2834
}
2935

3036
out.write_all(&writer.into_inner()?)?;

tests/integration_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn list_config(
2020
#[default(None)] platform: Option<Vec<Platform>>,
2121
#[default(None)] environment: Option<Vec<String>>,
2222
#[default(None)] ignore_pypi: Option<bool>,
23-
#[default(Some(OutputFormat::Pretty))] output: Option<OutputFormat>,
23+
#[default(Some(OutputFormat::Default))] output: Option<OutputFormat>,
2424
) -> CondaDenyListConfig {
2525
let cli = CondaDenyCliConfig::List {
2626
lockfile,
@@ -48,7 +48,7 @@ fn check_config(
4848
#[default(None)] environment: Option<Vec<String>>,
4949
#[default(None)] osi: Option<bool>,
5050
#[default(None)] ignore_pypi: Option<bool>,
51-
#[default(Some(OutputFormat::Pretty))] output: Option<OutputFormat>,
51+
#[default(Some(OutputFormat::Default))] output: Option<OutputFormat>,
5252
) -> CondaDenyCheckConfig {
5353
let cli = CondaDenyCliConfig::Check {
5454
lockfile,

0 commit comments

Comments
 (0)