use std::fs;
use std::path::PathBuf;
use anyhow::Result;
use clap::Parser;
use crate::config::Config;
use crate::context::Context;
use crate::lockfile::Digest;
use crate::metadata::Cargo;
use crate::splicing::SplicingManifest;
#[derive(Parser, Debug)]
#[clap(about = "Command line options for the `query` subcommand", version)]
pub struct QueryOptions {
#[clap(long)]
pub lockfile: PathBuf,
#[clap(long)]
pub config: PathBuf,
#[clap(long)]
pub splicing_manifest: PathBuf,
#[clap(long, env = "CARGO")]
pub cargo: PathBuf,
#[clap(long, env = "RUSTC")]
pub rustc: PathBuf,
}
pub fn query(opt: QueryOptions) -> Result<()> {
let content = match fs::read_to_string(&opt.lockfile) {
Ok(c) => c,
Err(_) => return announce_repin("Unable to read lockfile"),
};
let lockfile: Context = match serde_json::from_str(&content) {
Ok(ctx) => ctx,
Err(_) => return announce_repin("Could not load lockfile"),
};
let digest = match &lockfile.checksum {
Some(d) => d.clone(),
None => return announce_repin("No digest provided in lockfile"),
};
let config = Config::try_from_path(&opt.config)?;
let splicing_manifest = SplicingManifest::try_from_path(&opt.splicing_manifest)?;
let expected = Digest::new(
&lockfile,
&config,
&splicing_manifest,
&Cargo::new(opt.cargo),
&opt.rustc,
)?;
if digest != expected {
return announce_repin(&format!("Digests do not match: {digest:?} != {expected:?}",));
}
Ok(())
}
fn announce_repin(reason: &str) -> Result<()> {
eprintln!("{reason}");
println!("repin");
Ok(())
}