|
1 | | -use anyhow::Result; |
2 | 1 | use std::io::Write; |
3 | 2 |
|
| 3 | +use anyhow::Result; |
| 4 | +use comfy_table::modifiers::UTF8_ROUND_CORNERS; |
| 5 | +use comfy_table::presets::UTF8_FULL; |
| 6 | +use comfy_table::{ContentArrangement, Table}; |
| 7 | +use termcolor::WriteColor; |
| 8 | +use wasm_metadata::{Metadata, Payload}; |
| 9 | + |
4 | 10 | /// Manipulate metadata (module name, producers) to a WebAssembly file. |
5 | 11 | #[derive(clap::Parser)] |
6 | 12 | pub enum Opts { |
@@ -44,11 +50,11 @@ impl ShowOpts { |
44 | 50 | let input = self.io.parse_input_wasm()?; |
45 | 51 | let mut output = self.io.output_writer()?; |
46 | 52 |
|
47 | | - let metadata = wasm_metadata::Payload::from_binary(&input)?; |
| 53 | + let payload = wasm_metadata::Payload::from_binary(&input)?; |
48 | 54 | if self.json { |
49 | | - write!(output, "{}", serde_json::to_string(&metadata)?)?; |
| 55 | + write!(output, "{}", serde_json::to_string(&payload)?)?; |
50 | 56 | } else { |
51 | | - write!(output, "{metadata}")?; |
| 57 | + fmt_payload(&payload, &mut output)?; |
52 | 58 | } |
53 | 59 | Ok(()) |
54 | 60 | } |
@@ -82,3 +88,75 @@ impl AddOpts { |
82 | 88 | Ok(()) |
83 | 89 | } |
84 | 90 | } |
| 91 | + |
| 92 | +fn fmt_payload(payload: &Payload, f: &mut Box<dyn WriteColor>) -> Result<()> { |
| 93 | + let mut table = Table::new(); |
| 94 | + table |
| 95 | + .load_preset(UTF8_FULL) |
| 96 | + .apply_modifier(UTF8_ROUND_CORNERS) |
| 97 | + .set_content_arrangement(ContentArrangement::Dynamic) |
| 98 | + .set_width(80) |
| 99 | + .set_header(vec!["KIND", "VALUE"]); |
| 100 | + let Metadata { |
| 101 | + name, |
| 102 | + author, |
| 103 | + description, |
| 104 | + producers, |
| 105 | + licenses, |
| 106 | + source, |
| 107 | + homepage, |
| 108 | + range, |
| 109 | + } = payload.metadata(); |
| 110 | + |
| 111 | + // Print the basic information |
| 112 | + let kind = match payload { |
| 113 | + Payload::Component { .. } => "component", |
| 114 | + Payload::Module(_) => "module", |
| 115 | + }; |
| 116 | + table.add_row(vec!["kind", &kind]); |
| 117 | + let name = name.as_deref().unwrap_or("<unknown>"); |
| 118 | + table.add_row(vec!["name", &name]); |
| 119 | + table.add_row(vec![ |
| 120 | + "range", |
| 121 | + &format!("0x{:x}..0x{:x}", range.start, range.end), |
| 122 | + ]); |
| 123 | + |
| 124 | + // Print the OCI annotations |
| 125 | + if let Some(description) = description { |
| 126 | + table.add_row(vec!["description", &description.to_string()]); |
| 127 | + } |
| 128 | + if let Some(licenses) = licenses { |
| 129 | + table.add_row(vec!["licenses", &licenses.to_string()]); |
| 130 | + } |
| 131 | + if let Some(author) = author { |
| 132 | + table.add_row(vec!["author", &author.to_string()]); |
| 133 | + } |
| 134 | + if let Some(source) = source { |
| 135 | + table.add_row(vec!["source", &source.to_string()]); |
| 136 | + } |
| 137 | + if let Some(homepage) = homepage { |
| 138 | + table.add_row(vec!["homepage", &homepage.to_string()]); |
| 139 | + } |
| 140 | + |
| 141 | + if let Some(producers) = producers { |
| 142 | + for (name, pairs) in producers.iter() { |
| 143 | + for (field, version) in pairs.iter() { |
| 144 | + match version.len() { |
| 145 | + 0 => table.add_row(vec![name, &format!("{field}")]), |
| 146 | + _ => table.add_row(vec![name, &format!("{field} [{version}]")]), |
| 147 | + }; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Write the table to the writer |
| 153 | + writeln!(f, "{table}")?; |
| 154 | + |
| 155 | + if let Payload::Component { children, .. } = payload { |
| 156 | + for payload in children { |
| 157 | + fmt_payload(payload, f)?; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + Ok(()) |
| 162 | +} |
0 commit comments