-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from kpcyrd/json
Add --json option for machine-readable output
- Loading branch information
Showing
8 changed files
with
189 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pub use anyhow::{bail, Context, Error, Result}; | ||
pub use anyhow::{anyhow, bail, Context as _, Error, Result}; | ||
pub use log::{debug, info}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use crate::errors::*; | ||
use crate::format::{Chunk, Pattern, Pkg}; | ||
use colored::Colorize; | ||
use std::fmt::Write; | ||
|
||
pub fn display(pattern: &Pattern, package: &Pkg) -> Result<String, Error> { | ||
let mut fmt = String::new(); | ||
|
||
for chunk in &pattern.0 { | ||
match *chunk { | ||
Chunk::Raw(ref s) => fmt.write_str(s)?, | ||
Chunk::Package => { | ||
let pkg = format!("{} v{}", package.name, package.version); | ||
if let Some(deb) = &package.debinfo { | ||
if deb.in_unstable { | ||
if deb.compatible { | ||
write!(fmt, "{} ({} in debian)", pkg.green(), deb.version.yellow())?; | ||
} else if deb.outdated { | ||
write!( | ||
fmt, | ||
"{} (outdated, {} in debian)", | ||
pkg.yellow(), | ||
deb.version.red() | ||
)?; | ||
} else { | ||
write!(fmt, "{} (in debian)", pkg.green())?; | ||
} | ||
} else if deb.in_new { | ||
if deb.compatible { | ||
write!( | ||
fmt, | ||
"{} ({} in debian NEW queue)", | ||
pkg.blue(), | ||
deb.version.yellow() | ||
)?; | ||
} else if deb.outdated { | ||
write!( | ||
fmt, | ||
"{}, (outdated, {} in debian NEW queue)", | ||
pkg.blue(), | ||
deb.version.red() | ||
)?; | ||
} else { | ||
write!(fmt, "{} (in debian NEW queue)", pkg.blue())?; | ||
} | ||
} else if deb.outdated { | ||
write!(fmt, "{} (outdated, {})", pkg.red(), deb.version.red())?; | ||
} else { | ||
write!(fmt, "{pkg}")?; | ||
} | ||
} else { | ||
write!(fmt, "{pkg}")?; | ||
} | ||
|
||
match &package.source { | ||
Some(source) if !source.is_crates_io() => write!(fmt, " ({source})")?, | ||
// https://github.com/rust-lang/cargo/issues/7483 | ||
None => write!( | ||
fmt, | ||
" ({})", | ||
package.manifest_path.parent().unwrap().display() | ||
)?, | ||
_ => {} | ||
} | ||
} | ||
Chunk::License => { | ||
if let Some(license) = &package.license { | ||
write!(fmt, "{license}")? | ||
} | ||
} | ||
Chunk::Repository => { | ||
if let Some(repository) = &package.repository { | ||
write!(fmt, "{repository}")? | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(fmt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::errors::*; | ||
use crate::format::Pkg; | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct Json { | ||
name: String, | ||
cargo_lock_version: String, | ||
repository: Option<String>, | ||
license: Option<String>, | ||
debian: Option<DebianJson>, | ||
depth: usize, | ||
} | ||
|
||
#[derive(Debug, serde::Serialize)] | ||
pub struct DebianJson { | ||
version: String, | ||
compatible: bool, | ||
exact_match: bool, | ||
in_new: bool, | ||
in_unstable: bool, | ||
outdated: bool, | ||
} | ||
|
||
impl Json { | ||
pub fn new(pkg: &Pkg, depth: usize) -> Self { | ||
let debian = pkg.debinfo.as_ref().map(|deb| DebianJson { | ||
version: deb.version.clone(), | ||
compatible: deb.compatible, | ||
exact_match: deb.exact_match, | ||
in_new: deb.in_new, | ||
in_unstable: deb.in_unstable, | ||
outdated: deb.outdated, | ||
}); | ||
|
||
Json { | ||
name: pkg.name.clone(), | ||
cargo_lock_version: pkg.version.to_string(), | ||
repository: pkg.repository.clone(), | ||
license: pkg.license.clone(), | ||
debian, | ||
depth, | ||
} | ||
} | ||
} | ||
|
||
pub fn display(package: &Pkg, depth: usize) -> Result<String, Error> { | ||
let json = serde_json::to_string(&Json::new(package, depth))?; | ||
Ok(json) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.