malwaredb-client 0.3.4

Client application and library for connecting to MalwareDB.
Documentation
// SPDX-License-Identifier: Apache-2.0

use crate::MdbClient;

use std::path::PathBuf;
use std::process::ExitCode;

use anyhow::{Context, Result};
use clap::Parser;

/// Retrieve a report for a sample from the server
/// The server will check that the user is part of a group
/// which is able to access the file's originating source.
#[derive(Parser, Clone, Debug, PartialEq)]
pub struct SampleReport {
    /// The hash of the file to retrieve
    pub hash: String,

    /// Save the report to disk, optional.
    pub output: Option<PathBuf>,
}

impl SampleReport {
    pub async fn exec(&self, config: &MdbClient) -> Result<ExitCode> {
        let _bytes = hex::decode(&self.hash).context("Provided hash wasn't valid hexidecimal")?;

        let report = config
            .report(&self.hash)
            .await
            .context("failed to get report")?;

        if let Some(output) = &self.output {
            let report = serde_json::to_string(&report)?;
            std::fs::write(output, report).context("failed to write report")?;
        } else {
            println!("{report}");
        }

        Ok(ExitCode::SUCCESS)
    }
}

#[test]
fn verify_cli() {
    use clap::CommandFactory;

    SampleReport::command().debug_assert();
}