Skip to main content

greentic_bundle/cli/
inspect.rs

1use std::path::PathBuf;
2
3use anyhow::Result;
4use clap::Args;
5
6#[derive(Debug, Args)]
7pub struct InspectArgs {
8    #[arg(value_name = "TARGET")]
9    pub target: Option<PathBuf>,
10
11    #[arg(long, default_value = ".", help = "cli.inspect.root.option")]
12    pub root: PathBuf,
13
14    #[arg(long, value_name = "FILE", help = "cli.inspect.artifact.option")]
15    pub artifact: Option<PathBuf>,
16
17    #[arg(long, default_value_t = false, help = "cli.inspect.json.option")]
18    pub json: bool,
19}
20
21impl Default for InspectArgs {
22    fn default() -> Self {
23        Self {
24            target: None,
25            root: PathBuf::from("."),
26            artifact: None,
27            json: false,
28        }
29    }
30}
31
32pub fn run(args: InspectArgs) -> Result<()> {
33    let report = if let Some(artifact) = args.artifact.as_deref().or_else(|| {
34        args.target
35            .as_deref()
36            .filter(|path| is_bundle_artifact(path))
37    }) {
38        crate::build::inspect_target(None, Some(artifact))?
39    } else if let Some(root) = args.target.as_deref() {
40        crate::build::inspect_target(Some(root), None)?
41    } else {
42        crate::build::inspect_target(Some(&args.root), None)?
43    };
44    if args.json {
45        println!("{}", serde_json::to_string_pretty(&report)?);
46    } else if report.kind == "artifact" {
47        for entry in report.contents.as_deref().unwrap_or(&[]) {
48            println!("{entry}");
49        }
50    } else {
51        println!("{}", serde_json::to_string_pretty(&report)?);
52    }
53    Ok(())
54}
55
56fn is_bundle_artifact(path: &std::path::Path) -> bool {
57    path.extension()
58        .and_then(|value| value.to_str())
59        .is_some_and(|value| value.eq_ignore_ascii_case("gtbundle"))
60}