Skip to main content

greentic_component/cmd/info/
command.rs

1use anyhow::Result;
2use clap::Args;
3use std::path::{Path, PathBuf};
4
5use super::human;
6use super::reader;
7
8#[derive(Debug, Args)]
9pub struct InfoArgs {
10    /// Path to a compiled component .wasm file.
11    #[arg(value_name = "PATH")]
12    pub path: PathBuf,
13
14    /// Emit the info report as JSON.
15    #[arg(long, default_value_t = false)]
16    pub json: bool,
17}
18
19pub fn run(args: &InfoArgs) -> Result<()> {
20    validate_path(&args.path)?;
21    let report = match reader::read(&args.path) {
22        Ok(r) => r,
23        Err(e) => {
24            eprintln!(
25                "{}: not a valid wasm32-wasip2 component: {e}",
26                args.path.display()
27            );
28            std::process::exit(5);
29        }
30    };
31    if args.json {
32        println!("{}", serde_json::to_string_pretty(&report)?);
33    } else {
34        print!("{}", human::render(&report));
35    }
36    Ok(())
37}
38
39fn validate_path(path: &Path) -> Result<()> {
40    if !path.exists() {
41        eprintln!("{}: not a .wasm file (file not found)", path.display());
42        std::process::exit(2);
43    }
44    if path.is_dir() {
45        eprintln!("{}: not a .wasm file (is a directory)", path.display());
46        std::process::exit(2);
47    }
48    if path.extension().and_then(|s| s.to_str()) != Some("wasm") {
49        eprintln!("{}: not a .wasm file (wrong extension)", path.display());
50        std::process::exit(2);
51    }
52    Ok(())
53}