dtmt: Add option to compile file when injecting

This commit is contained in:
Lucas Schwiderski 2023-09-22 15:42:16 +02:00
parent ea7886b08f
commit 126d3c743d
Signed by: lucas
GPG key ID: AA12679AAA6DF4D8

View file

@ -3,19 +3,28 @@ use std::path::PathBuf;
use clap::{value_parser, Arg, ArgMatches, Command};
use color_eyre::eyre::{self, Context, Result};
use color_eyre::Help;
use sdk::murmur::IdString64;
use sdk::Bundle;
use tokio::fs::{self, File};
use tokio::io::AsyncReadExt;
pub(crate) fn command_definition() -> Command {
Command::new("inject")
.about("Inject a file into a bundle.")
.about("Inject a file into a bundle.\n\
Raw binary data can be used to directly replace the file's variant data blob without affecting the metadata.\n\
Alternatively, a compiler format may be specified, and a complete bundle file is created.")
.arg(
Arg::new("replace")
.help("The name of a file in the bundle whos content should be replaced.")
.help("The name of a file in the bundle whose content should be replaced.")
.short('r')
.long("replace"),
)
.arg(
Arg::new("compile")
.help("Compile the file with the given data format before injecting.")
.long("compile")
.short('c')
)
.arg(
Arg::new("output")
.help(
@ -58,7 +67,14 @@ pub(crate) async fn run(ctx: sdk::Context, matches: &ArgMatches) -> Result<()> {
Bundle::from_binary(&ctx, name, binary).wrap_err("Failed to open bundle file")?
};
if let Some(name) = matches.get_one::<String>("replace") {
let name = match matches.get_one::<String>("replace") {
Some(name) => match u64::from_str_radix(name, 16) {
Ok(id) => IdString64::from(id),
Err(_) => IdString64::String(name.clone()),
},
None => eyre::bail!("Currently, only the '--replace' operation is supported."),
};
let mut file = File::open(&file_path)
.await
.wrap_err_with(|| format!("Failed to open '{}'", file_path.display()))?;
@ -75,22 +91,23 @@ pub(crate) async fn run(ctx: sdk::Context, matches: &ArgMatches) -> Result<()> {
.wrap_err("Failed to read input file")?;
variant.set_data(data);
} else {
let err = eyre::eyre!("No file '{}' in this bundle.", name)
.with_suggestion(|| {
let err =
eyre::eyre!("No file '{}' in this bundle.", name.display()).with_suggestion(|| {
format!(
"Run '{} bundle list {}' to list the files in this bundle.",
clap::crate_name!(),
bundle_path.display()
)
})
.with_suggestion(|| {
format!(
"Use '{} bundle inject --add {} {} {}' to add it as a new file",
clap::crate_name!(),
name,
bundle_path.display(),
file_path.display()
)
// Not yet supported.
// })
// .with_suggestion(|| {
// format!(
// "Use '{} bundle inject --add {} {} {}' to add it as a new file",
// clap::crate_name!(),
// name.display(),
// bundle_path.display(),
// file_path.display()
// )
});
return Err(err);
@ -106,7 +123,4 @@ pub(crate) async fn run(ctx: sdk::Context, matches: &ArgMatches) -> Result<()> {
.wrap_err("Failed to write data to output file")?;
Ok(())
} else {
eyre::bail!("Currently, only the '--replace' operation is supported.");
}
}