Skip to content

Instantly share code, notes, and snippets.

@heaths
Last active October 3, 2022 17:47
Show Gist options
  • Save heaths/cad76bc83752b21886169b546210acf7 to your computer and use it in GitHub Desktop.
Save heaths/cad76bc83752b21886169b546210acf7 to your computer and use it in GitHub Desktop.
Shows how to read a property value from an MSI on any platform using Rust
use std::path::PathBuf;
use std::result::Result;
use std::{error::Error, ops::Index};
use clap::{arg, command, value_parser, Arg};
use msi::{self, Expr, Select, Value};
fn main() -> Result<(), Box<dyn Error>> {
let matches = command!()
.arg(
Arg::new("path")
.help("Path to a Windows Installer package (MSI)")
.value_parser(value_parser!(PathBuf))
.required(true),
)
.arg(arg!(-p --property <NAME> "The property name to get").default_value("ProductCode"))
.get_matches();
let path = matches.get_one::<PathBuf>("path").expect("path required");
let property = matches
.get_one::<String>("property")
.expect("property name required");
let mut package = msi::open(path)?;
let columns = vec!["Value"];
let query = Select::table("Property")
.columns(&columns)
.with(Expr::col("Property").eq(Expr::string(property)));
let rows = package.select_rows(query)?;
for row in rows {
if let Value::Str(value) = row.index(0) {
println!("{}", value);
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment